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