Skip to content

Commit 87bee98

Browse files
Switched auth plain to auth login in smtp login to meet server's demands
1 parent 9af5683 commit 87bee98

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

internal/mailer/mailer.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package mailer
22

3-
import "context"
3+
import (
4+
"context"
5+
"fmt"
6+
"net/smtp"
7+
)
48

59
// Attachment is a file attached to an outbound email.
610
type Attachment struct {
@@ -27,3 +31,32 @@ type Mailer interface {
2731
type Noop struct{}
2832

2933
func (n *Noop) Send(_ context.Context, _ Message) error { return nil }
34+
35+
36+
type loginAuth struct {
37+
username, password string
38+
}
39+
40+
func LoginAuth(username, password string) smtp.Auth {
41+
return &loginAuth{username, password}
42+
}
43+
44+
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
45+
// This sends: AUTH LOGIN
46+
// The server will then prompt for the username, which your Next() method will catch.
47+
return "LOGIN", nil, nil
48+
}
49+
50+
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
51+
if more {
52+
switch string(fromServer) {
53+
case "Username:", "VXNlcm5hbWU6":
54+
return []byte(a.username), nil
55+
case "Password:", "UGFzc3dvcmQ6":
56+
return []byte(a.password), nil
57+
default:
58+
return nil, fmt.Errorf("unknown challenge: %s", string(fromServer))
59+
}
60+
}
61+
return nil, nil
62+
}

internal/mailer/smtp.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,10 @@ func (s *SMTP) Send(ctx context.Context, msg Message) error {
136136
defer c.Close()
137137

138138
if s.username != "" {
139-
auth := smtp.PlainAuth("", s.username, s.password, s.host)
139+
// Our specific server refuses PLAIN and requires LOGIN
140+
auth := LoginAuth(s.username, s.password)
140141
if err := c.Auth(auth); err != nil {
141-
// Don't wrap err — SMTP auth responses can contain server-side
142-
// detail that may expose credential information in logs.
143-
return fmt.Errorf("mailer: SMTP authentication failed")
142+
return fmt.Errorf("mailer: SMTP authentication failed: %w", err)
144143
}
145144
}
146145

0 commit comments

Comments
 (0)