From 9e950b1ba05d875fc3d05c58ee8c8601a17c01bc Mon Sep 17 00:00:00 2001 From: gearnode Date: Wed, 12 Mar 2025 14:31:15 +0100 Subject: [PATCH] Add SMTP auth support Signed-off-by: gearnode --- pkg/mailer/mailer.go | 21 +++++++++++++++------ pkg/probod/probod.go | 2 ++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pkg/mailer/mailer.go b/pkg/mailer/mailer.go index c792fde23..843c0078b 100644 --- a/pkg/mailer/mailer.go +++ b/pkg/mailer/mailer.go @@ -40,7 +40,9 @@ type ( SenderName string SenderEmail string Addr string - Timeout time.Duration // Timeout for SMTP operations + Timeout time.Duration + User string + Password string } ) @@ -67,8 +69,8 @@ LOOP: } } -func sendMailWithTimeout(ctx context.Context, addr string, a smtp.Auth, from string, to []string, msg []byte) error { - host, _, err := net.SplitHostPort(addr) +func (m *Mailer) sendMailWithTimeout(ctx context.Context, to []string, msg []byte) error { + host, _, err := net.SplitHostPort(m.cfg.Addr) if err != nil { return fmt.Errorf("invalid address: %w", err) } @@ -76,7 +78,7 @@ func sendMailWithTimeout(ctx context.Context, addr string, a smtp.Auth, from str var d net.Dialer d.Timeout = 5 * time.Second - conn, err := d.DialContext(ctx, "tcp", addr) + conn, err := d.DialContext(ctx, "tcp", m.cfg.Addr) if err != nil { return fmt.Errorf("connection error: %w", err) } @@ -88,7 +90,14 @@ func sendMailWithTimeout(ctx context.Context, addr string, a smtp.Auth, from str } defer c.Quit() - if err = c.Mail(from); err != nil { + if m.cfg.User != "" && m.cfg.Password != "" { + auth := smtp.PlainAuth("", m.cfg.User, m.cfg.Password, host) + if err = c.Auth(auth); err != nil { + return fmt.Errorf("SMTP authentication error: %w", err) + } + } + + if err = c.Mail(m.cfg.SenderEmail); err != nil { return fmt.Errorf("MAIL FROM error: %w", err) } @@ -145,7 +154,7 @@ func (m *Mailer) batchSendEmails(ctx context.Context) error { sendCtx, cancel := context.WithTimeout(ctx, m.cfg.Timeout) defer cancel() - if err := sendMailWithTimeout(sendCtx, m.cfg.Addr, nil, m.cfg.SenderEmail, []string{email.RecipientEmail}, buf.Bytes()); err != nil { + if err := m.sendMailWithTimeout(sendCtx, []string{email.RecipientEmail}, buf.Bytes()); err != nil { if errors.Is(err, context.DeadlineExceeded) { return fmt.Errorf("email sending timed out after %s: %w", m.cfg.Timeout, err) } diff --git a/pkg/probod/probod.go b/pkg/probod/probod.go index c133f657c..145212c2f 100644 --- a/pkg/probod/probod.go +++ b/pkg/probod/probod.go @@ -220,6 +220,8 @@ func (impl *Implm) Run( SenderEmail: impl.cfg.Mailer.SenderEmail, SenderName: impl.cfg.Mailer.SenderName, Addr: impl.cfg.Mailer.SMTP.Addr, + User: impl.cfg.Mailer.SMTP.User, + Password: impl.cfg.Mailer.SMTP.Password, Timeout: time.Second * 10, }) wg.Add(1)