change mailing library dependency and add options management

This commit is contained in:
2024-11-19 15:19:13 +01:00
parent f6c3f09400
commit 9836b30191
3 changed files with 135 additions and 15 deletions

View File

@@ -6,7 +6,7 @@ import (
"html/template"
"github.com/spf13/viper"
gomail "gopkg.in/mail.v2"
"github.com/wneessen/go-mail"
)
type Mailer struct {
@@ -22,6 +22,8 @@ type SMTPConfig struct {
Password string
}
type Option func(*mail.Msg, []mail.Option) ([]mail.Option, error)
func NewMailer(templates_dir string, tplcfg *viper.Viper, smtpconfig *viper.Viper) (*Mailer, error) {
return &Mailer{
TemplatesDir: templates_dir,
@@ -35,7 +37,7 @@ func NewMailer(templates_dir string, tplcfg *viper.Viper, smtpconfig *viper.Vipe
}, nil
}
func (m *Mailer) Send(emailcfg string, to string, data any) error {
func (m *Mailer) Send(emailcfg string, to string, data any, opts ...Option) error {
cfg := m.TemplatesConfig.Sub(emailcfg)
files := cfg.GetStringSlice("files")
@@ -43,27 +45,74 @@ func (m *Mailer) Send(emailcfg string, to string, data any) error {
for _, f := range files {
prefixed_files = append(prefixed_files, m.TemplatesDir+f)
}
t := template.Must(template.ParseFiles(prefixed_files...))
t := template.New("email").Funcs(
template.FuncMap{
"unescapeHTML": UnescapeHTML,
},
)
t = template.Must(t.ParseFiles(prefixed_files...))
buf := new(bytes.Buffer)
if err := t.ExecuteTemplate(buf, "main", data); err != nil {
return err
return fmt.Errorf("failed execute mail template : %w", err)
}
body := buf.String()
fmt.Println(body)
message := mail.NewMsg()
if err := message.From(m.SMTPConfig.Username); err != nil {
return fmt.Errorf("failed to set From header : %w", err)
}
if err := message.To(to); err != nil {
return fmt.Errorf("failed to set To header : %w", err)
}
message.Subject(cfg.GetString("subject"))
message.SetBodyString(mail.TypeTextHTML, body)
message := gomail.NewMessage()
dialOptions := []mail.Option{
//mail.WithSMTPAuth(mail.SMTPAuthAutoDiscover),
mail.WithSMTPAuth(mail.SMTPAuthNoAuth),
mail.WithUsername(m.SMTPConfig.Username),
mail.WithPassword(m.SMTPConfig.Password),
}
message.SetHeader("From", m.SMTPConfig.Username)
message.SetHeader("To", to)
message.SetHeader("Subject", cfg.GetString("subject"))
message.SetBody("text/html", body)
fmt.Println(dialOptions)
d := gomail.NewDialer(m.SMTPConfig.Host, m.SMTPConfig.Port, m.SMTPConfig.Username, m.SMTPConfig.Password)
if err := d.DialAndSend(message); err != nil {
return err
for _, opt := range opts {
no, err := opt(message, dialOptions)
if err != nil {
return fmt.Errorf("failed to set option : %w", err)
}
dialOptions = no
}
fmt.Println(dialOptions)
client, err := mail.NewClient(m.SMTPConfig.Host, dialOptions...)
if err != nil {
return fmt.Errorf("failed to create mail client : %w", err)
}
if err := client.DialAndSend(message); err != nil {
return fmt.Errorf("failed to send message : %w", err)
}
return nil
}
func WithReplyTo(email string) Option {
return func(m *mail.Msg, opts []mail.Option) ([]mail.Option, error) {
return opts, m.ReplyTo(email)
}
}
func WithTLSOpportunistic() Option {
return func(m *mail.Msg, opts []mail.Option) ([]mail.Option, error) {
opts = append(opts, mail.WithTLSPolicy(mail.TLSOpportunistic))
return opts, nil
}
}
func UnescapeHTML(s string) template.HTML {
return template.HTML(s)
}