From 3e0c748fc9aa2307241985b5c07d4c62d5a27492 Mon Sep 17 00:00:00 2001 From: gearnode Date: Wed, 12 Mar 2025 14:41:16 +0100 Subject: [PATCH] Add smtp tls support Signed-off-by: gearnode --- pkg/mailer/mailer.go | 7 +++++++ pkg/probod/mailer_config.go | 7 ++++--- pkg/probod/probod.go | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/mailer/mailer.go b/pkg/mailer/mailer.go index 843c0078b..12a768b85 100644 --- a/pkg/mailer/mailer.go +++ b/pkg/mailer/mailer.go @@ -43,6 +43,7 @@ type ( Timeout time.Duration User string Password string + TLSRequired bool } ) @@ -90,6 +91,12 @@ func (m *Mailer) sendMailWithTimeout(ctx context.Context, to []string, msg []byt } 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 != "" { auth := smtp.PlainAuth("", m.cfg.User, m.cfg.Password, host) if err = c.Auth(auth); err != nil { diff --git a/pkg/probod/mailer_config.go b/pkg/probod/mailer_config.go index 20903b1b7..10e3fa164 100644 --- a/pkg/probod/mailer_config.go +++ b/pkg/probod/mailer_config.go @@ -22,8 +22,9 @@ type ( } smtpConfig struct { - Addr string `json:"addr"` - User string `json:"user"` - Password string `json:"password"` + Addr string `json:"addr"` + User string `json:"user"` + Password string `json:"password"` + TLSRequired bool `json:"tls-required"` } ) diff --git a/pkg/probod/probod.go b/pkg/probod/probod.go index 145212c2f..4040ed9d5 100644 --- a/pkg/probod/probod.go +++ b/pkg/probod/probod.go @@ -222,6 +222,7 @@ func (impl *Implm) Run( Addr: impl.cfg.Mailer.SMTP.Addr, User: impl.cfg.Mailer.SMTP.User, Password: impl.cfg.Mailer.SMTP.Password, + TLSRequired: impl.cfg.Mailer.SMTP.TLSRequired, Timeout: time.Second * 10, }) wg.Add(1)