Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: correctly close SMTP message and await response#14495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
dannykopping merged 1 commit intomainfromdk/smtp-close
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletioncoderd/notifications/dispatch/smtp.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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{}
Expand DownExpand Up@@ -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
}
Expand Down
18 changes: 18 additions & 0 deletionscoderd/notifications/dispatch/smtp_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -62,6 +62,7 @@ func TestSMTP(t *testing.T) {
expectedErr string
retryable bool
useTLS bool
failOnDataFn func() error
}{
/**
* LOGIN auth mechanism
Expand DownExpand Up@@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Isn't this error non-retryable?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Not apriori; it could be a temporary failure.

},
}

// nolint:paralleltest // Reinitialization is not required as of Go v1.22.
Expand All@@ -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.
Expand Down
5 changes: 5 additions & 0 deletionscoderd/notifications/dispatch/smtp_util_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,6 +24,7 @@ var (
type Config struct {
AuthMechanisms []string
AcceptedIdentity, AcceptedUsername, AcceptedPassword string
FailOnDataFn func() error
}

type Message struct {
Expand DownExpand Up@@ -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()
}
Comment on lines +151 to +153
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

At some point in the future, would it make sense to test with something likemocktools/go-smtp-mock instead?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I evaluated it, but it does not support authentication.


s.backend.lastMsg.Contents = string(b)

return nil
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp