From f659568f4c47085dd902ef4036b11ceb34cc6f63 Mon Sep 17 00:00:00 2001 From: Danny Kopping Date: Fri, 30 Aug 2024 10:50:44 +0200 Subject: [PATCH] fix: correctly close SMTP message and await response Signed-off-by: Danny Kopping --- coderd/notifications/dispatch/smtp.go | 5 ++++- coderd/notifications/dispatch/smtp_test.go | 18 ++++++++++++++++++ .../notifications/dispatch/smtp_util_test.go | 5 +++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/coderd/notifications/dispatch/smtp.go b/coderd/notifications/dispatch/smtp.go index e0c1b89f6154e..7bbff3fcb8879 100644 --- a/coderd/notifications/dispatch/smtp.go +++ b/coderd/notifications/dispatch/smtp.go @@ -183,7 +183,6 @@ func (s *SMTPHandler) dispatch(subject, htmlBody, plainBody, to string) Delivery if err != nil { return true, xerrors.Errorf("message transmission: %w", err) } - defer message.Close() // Create message headers. msg := &bytes.Buffer{} @@ -251,6 +250,10 @@ func (s *SMTPHandler) dispatch(subject, htmlBody, plainBody, to string) Delivery return false, xerrors.Errorf("write body buffer: %w", err) } + if err = message.Close(); err != nil { + return true, xerrors.Errorf("delivery failure: %w", err) + } + // Returning false, nil indicates successful send (i.e. non-retryable non-error) return false, nil } diff --git a/coderd/notifications/dispatch/smtp_test.go b/coderd/notifications/dispatch/smtp_test.go index dbafabc969bc4..7576c4c936fa4 100644 --- a/coderd/notifications/dispatch/smtp_test.go +++ b/coderd/notifications/dispatch/smtp_test.go @@ -62,6 +62,7 @@ func TestSMTP(t *testing.T) { expectedErr string retryable bool useTLS bool + failOnDataFn func() error }{ /** * LOGIN auth mechanism @@ -381,6 +382,21 @@ func TestSMTP(t *testing.T) { toAddrs: []string{to}, expectedAuthMeth: sasl.Plain, }, + /** + * Other errors + */ + { + name: "Rejected on DATA", + cfg: codersdk.NotificationsEmailConfig{ + Hello: hello, + From: from, + }, + failOnDataFn: func() error { + return &smtp.SMTPError{Code: 501, EnhancedCode: smtp.EnhancedCode{5, 5, 4}, Message: "Rejected!"} + }, + expectedErr: "SMTP error 501: Rejected!", + retryable: true, + }, } // nolint:paralleltest // Reinitialization is not required as of Go v1.22. @@ -398,6 +414,8 @@ func TestSMTP(t *testing.T) { AcceptedIdentity: tc.cfg.Auth.Identity.String(), AcceptedUsername: username, AcceptedPassword: password, + + FailOnDataFn: tc.failOnDataFn, }) // Create a mock SMTP server which conditionally listens for plain or TLS connections. diff --git a/coderd/notifications/dispatch/smtp_util_test.go b/coderd/notifications/dispatch/smtp_util_test.go index 659a17bec4a08..44cb8725c5d8c 100644 --- a/coderd/notifications/dispatch/smtp_util_test.go +++ b/coderd/notifications/dispatch/smtp_util_test.go @@ -24,6 +24,7 @@ var ( type Config struct { AuthMechanisms []string AcceptedIdentity, AcceptedUsername, AcceptedPassword string + FailOnDataFn func() error } type Message struct { @@ -147,6 +148,10 @@ func (s *Session) Data(r io.Reader) error { return err } + if s.backend.cfg.FailOnDataFn != nil { + return s.backend.cfg.FailOnDataFn() + } + s.backend.lastMsg.Contents = string(b) return nil pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy