Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.
Go email-relayemail-routinghttp-to-smtpmail-deliverymailchimp-alternativemailgun-alternativemailsendermailservermailtemplatemailtestpostmark-alternativesend-emailsendgrid-alternativesendmailtransactional-emailtransactional-outbox
feat(email): use AUTH LOGIN when the SMTP server does not offer PLAIN
Thomas Sauterc25200cfc4f4
diff --git a/internal/services/email/smtp_sender.go b/internal/services/email/smtp_sender.goindex b5d15e5..50ef8ba 100644--- a/internal/services/email/smtp_sender.go+++ b/internal/services/email/smtp_sender.go@@ -13,6 +13,7 @@ import ("net/mail""net/smtp""net/textproto"+ "slices""strings""github.com/goposta/posta/internal/models"@@ -92,19 +93,64 @@ func (s *SMTPSender) TestConnection(server *models.SMTPServer) error {defer func() { _ = client.Close() }()if server.Username != "" {- if err := client.Auth(smtp.PlainAuth("", server.Username, server.Password, server.Host)); err != nil {+ if err := client.Auth(newAuth(server.Host, server.Username, server.Password)); err != nil {return fmt.Errorf("SMTP auth failed: %w", err)}}return client.Quit()}+// autoAuth uses PLAIN when the server offers it, otherwise LOGIN (Exchange Online).+type autoAuth struct {+ plain smtp.Auth+ host, user, pass string+ login bool+}++func newAuth(host, username, password string) smtp.Auth {+ return &autoAuth{plain: smtp.PlainAuth("", username, password, host), host: host, user: username, pass: password}+}++func (a *autoAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {+ if !offers(server.Auth, "LOGIN") || offers(server.Auth, "PLAIN") {+ return a.plain.Start(server)+ }+ if !server.TLS && server.Name != "localhost" && server.Name != "127.0.0.1" && server.Name != "::1" {+ return "", nil, errors.New("unencrypted connection")+ }+ if server.Name != a.host {+ return "", nil, errors.New("wrong host name")+ }+ a.login = true+ return "LOGIN", nil, nil+}++func (a *autoAuth) Next(fromServer []byte, more bool) ([]byte, error) {+ if !a.login {+ return a.plain.Next(fromServer, more)+ }+ if !more {+ return nil, nil+ }+ switch strings.ToLower(strings.TrimSpace(string(fromServer))) {+ case "username:":+ return []byte(a.user), nil+ case "password:":+ return []byte(a.pass), nil+ }+ return nil, fmt.Errorf("unexpected LOGIN challenge %q", fromServer)+}++func offers(mechanisms []string, name string) bool {+ return slices.ContainsFunc(mechanisms, func(m string) bool { return strings.EqualFold(m, name) })+}+func (s *SMTPSender) Send(server *models.SMTPServer, from string, to []string, subject, htmlBody, textBody string, attachments []models.Attachment, headers map[string]string, listUnsubscribeURL, listUnsubscribeMailto string, listUnsubscribePost bool) error {addr := fmt.Sprintf("%s:%d", server.Host, server.Port)var auth smtp.Authif server.Username != "" {- auth = smtp.PlainAuth("", server.Username, server.Password, server.Host)+ auth = newAuth(server.Host, server.Username, server.Password)}msg := buildMessage(from, to, subject, htmlBody, textBody, attachments, headers, listUnsubscribeURL, listUnsubscribeMailto, listUnsubscribePost)diff --git a/internal/services/email/smtp_sender_test.go b/internal/services/email/smtp_sender_test.goindex 2ceff22..4a6846e 100644--- a/internal/services/email/smtp_sender_test.go+++ b/internal/services/email/smtp_sender_test.go@@ -6,6 +6,7 @@ package emailimport ("errors""fmt"+ "net/smtp""net/textproto""testing")@@ -70,3 +71,43 @@ func TestWrapSendError(t *testing.T) {t.Fatalf("Error() = %q, want %q", se.Error(), orig.Error())}}++func TestAuthPrefersPlainWhenOffered(t *testing.T) {+ a := newAuth("mail.example.com", "user", "secret")+ mech, _, err := a.Start(&smtp.ServerInfo{Name: "mail.example.com", TLS: true, Auth: []string{"LOGIN", "PLAIN"}})+ if err != nil || mech != "PLAIN" {+ t.Fatalf("mech=%q err=%v, want PLAIN", mech, err)+ }+}++func TestAuthFallsBackToPlainWhenNothingAdvertised(t *testing.T) {+ a := newAuth("mail.example.com", "user", "secret")+ mech, _, err := a.Start(&smtp.ServerInfo{Name: "mail.example.com", TLS: true})+ if err != nil || mech != "PLAIN" {+ t.Fatalf("mech=%q err=%v, want PLAIN", mech, err)+ }+}++func TestAuthUsesLoginWhenPlainNotOffered(t *testing.T) {+ a := newAuth("smtp.office365.com", "user", "secret")+ mech, _, err := a.Start(&smtp.ServerInfo{Name: "smtp.office365.com", TLS: true, Auth: []string{"LOGIN", "XOAUTH2"}})+ if err != nil || mech != "LOGIN" {+ t.Fatalf("mech=%q err=%v, want LOGIN", mech, err)+ }+ if got, _ := a.Next([]byte("Username:"), true); string(got) != "user" {+ t.Fatalf("username challenge answered with %q", got)+ }+ if got, _ := a.Next([]byte("Password:"), true); string(got) != "secret" {+ t.Fatalf("password challenge answered with %q", got)+ }+ if _, err := a.Next([]byte("Nonce:"), true); err == nil {+ t.Fatal("unexpected challenge must fail")+ }+}++func TestAuthLoginRefusesUnencryptedConnection(t *testing.T) {+ a := newAuth("smtp.office365.com", "user", "secret")+ if _, _, err := a.Start(&smtp.ServerInfo{Name: "smtp.office365.com", Auth: []string{"LOGIN"}}); err == nil {+ t.Fatal("LOGIN over plaintext must be refused")+ }+}