From e47d3abbef4d1eb0da234f5846e5bc3173d84691 Mon Sep 17 00:00:00 2001 From: Arnaud Delcasse Date: Mon, 10 Feb 2025 06:54:58 +0100 Subject: [PATCH] add from config --- mailer.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/mailer.go b/mailer.go index f36ddbc..77f9b3c 100644 --- a/mailer.go +++ b/mailer.go @@ -16,6 +16,7 @@ type Mailer struct { } type SMTPConfig struct { + From string Host string Port int Username string @@ -29,6 +30,7 @@ func NewMailer(templates_dir string, tplcfg *viper.Viper, smtpconfig *viper.Vipe TemplatesDir: templates_dir, TemplatesConfig: tplcfg, SMTPConfig: SMTPConfig{ + From: smtpconfig.GetString("from"), Host: smtpconfig.GetString("host"), Port: smtpconfig.GetInt("port"), Username: smtpconfig.GetString("username"), @@ -47,7 +49,7 @@ func (m *Mailer) Send(emailcfg string, to string, data any, opts ...Option) erro } t := template.New("email").Funcs( template.FuncMap{ - "unescapeHTML": UnescapeHTML, + "unescapeHTML": UnescapeHTML, }, ) t = template.Must(t.ParseFiles(prefixed_files...)) @@ -58,6 +60,11 @@ func (m *Mailer) Send(emailcfg string, to string, data any, opts ...Option) erro body := buf.String() message := mail.NewMsg() + fromAddress := m.SMTPConfig.From + if fromAddress == "" { + fromAddress = m.SMTPConfig.Username + } + if err := message.From(m.SMTPConfig.Username); err != nil { return fmt.Errorf("failed to set From header : %w", err) } @@ -68,21 +75,21 @@ func (m *Mailer) Send(emailcfg string, to string, data any, opts ...Option) erro message.SetBodyString(mail.TypeTextHTML, body) dialOptions := []mail.Option{ - //mail.WithSMTPAuth(mail.SMTPAuthAutoDiscover), - mail.WithSMTPAuth(mail.SMTPAuthNoAuth), - mail.WithUsername(m.SMTPConfig.Username), + mail.WithSMTPAuth(mail.SMTPAuthAutoDiscover), + // mail.WithSMTPAuth(mail.SMTPAuthNoAuth), + mail.WithUsername(m.SMTPConfig.Username), mail.WithPassword(m.SMTPConfig.Password), } fmt.Println(dialOptions) for _, opt := range opts { - no, err := opt(message, dialOptions) + no, err := opt(message, dialOptions) if err != nil { return fmt.Errorf("failed to set option : %w", err) } dialOptions = no - } + } fmt.Println(dialOptions) @@ -96,7 +103,6 @@ func (m *Mailer) Send(emailcfg string, to string, data any, opts ...Option) erro } return nil - } func WithReplyTo(email string) Option { @@ -115,4 +121,3 @@ func WithTLSOpportunistic() Option { func UnescapeHTML(s string) template.HTML { return template.HTML(s) } -