Add smtp tls support

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-03-12 14:41:16 +01:00
parent 9e950b1ba0
commit 3e0c748fc9
3 changed files with 12 additions and 3 deletions

View File

@@ -43,6 +43,7 @@ type (
Timeout time.Duration Timeout time.Duration
User string User string
Password string Password string
TLSRequired bool
} }
) )
@@ -90,6 +91,12 @@ func (m *Mailer) sendMailWithTimeout(ctx context.Context, to []string, msg []byt
} }
defer c.Quit() defer c.Quit()
if m.cfg.TLSRequired {
if err := c.StartTLS(nil); err != nil {
return fmt.Errorf("TLS negotiation error: %w", err)
}
}
if m.cfg.User != "" && m.cfg.Password != "" { if m.cfg.User != "" && m.cfg.Password != "" {
auth := smtp.PlainAuth("", m.cfg.User, m.cfg.Password, host) auth := smtp.PlainAuth("", m.cfg.User, m.cfg.Password, host)
if err = c.Auth(auth); err != nil { if err = c.Auth(auth); err != nil {

View File

@@ -22,8 +22,9 @@ type (
} }
smtpConfig struct { smtpConfig struct {
Addr string `json:"addr"` Addr string `json:"addr"`
User string `json:"user"` User string `json:"user"`
Password string `json:"password"` Password string `json:"password"`
TLSRequired bool `json:"tls-required"`
} }
) )

View File

@@ -222,6 +222,7 @@ func (impl *Implm) Run(
Addr: impl.cfg.Mailer.SMTP.Addr, Addr: impl.cfg.Mailer.SMTP.Addr,
User: impl.cfg.Mailer.SMTP.User, User: impl.cfg.Mailer.SMTP.User,
Password: impl.cfg.Mailer.SMTP.Password, Password: impl.cfg.Mailer.SMTP.Password,
TLSRequired: impl.cfg.Mailer.SMTP.TLSRequired,
Timeout: time.Second * 10, Timeout: time.Second * 10,
}) })
wg.Add(1) wg.Add(1)