Skip to content

Commit 1470013

Browse files
FIX (email): Add login auth in case if plain fails
1 parent 06282bb commit 1470013

File tree

2 files changed

+48
-6
lines changed
  • backend/internal/features/notifiers/models/email_notifier

2 files changed

+48
-6
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package email_notifier
2+
3+
import (
4+
"errors"
5+
"net/smtp"
6+
)
7+
8+
type loginAuth struct {
9+
username, password string
10+
}
11+
12+
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
13+
return "LOGIN", []byte{}, nil
14+
}
15+
16+
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
17+
if more {
18+
switch string(fromServer) {
19+
case "Username:":
20+
return []byte(a.username), nil
21+
case "Password:":
22+
return []byte(a.password), nil
23+
default:
24+
return nil, errors.New("unknown LOGIN challenge: " + string(fromServer))
25+
}
26+
}
27+
return nil, nil
28+
}

backend/internal/features/notifiers/models/email_notifier/model.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ func (e *EmailNotifier) Send(
132132

133133
// Set up authentication only if credentials are provided
134134
if isAuthRequired {
135-
auth := smtp.PlainAuth("", e.SMTPUser, smtpPassword, e.SMTPHost)
136-
if err := client.Auth(auth); err != nil {
137-
return fmt.Errorf("SMTP authentication failed: %w", err)
135+
if err := e.authenticate(client, smtpPassword); err != nil {
136+
return err
138137
}
139138
}
140139

@@ -195,9 +194,8 @@ func (e *EmailNotifier) Send(
195194

196195
// Authenticate only if credentials are provided
197196
if isAuthRequired {
198-
auth := smtp.PlainAuth("", e.SMTPUser, smtpPassword, e.SMTPHost)
199-
if err := client.Auth(auth); err != nil {
200-
return fmt.Errorf("SMTP authentication failed: %w", err)
197+
if err := e.authenticate(client, smtpPassword); err != nil {
198+
return err
201199
}
202200
}
203201

@@ -256,3 +254,19 @@ func (e *EmailNotifier) EncryptSensitiveData(encryptor encryption.FieldEncryptor
256254
}
257255
return nil
258256
}
257+
258+
func (e *EmailNotifier) authenticate(client *smtp.Client, password string) error {
259+
// Try PLAIN auth first (most common)
260+
plainAuth := smtp.PlainAuth("", e.SMTPUser, password, e.SMTPHost)
261+
if err := client.Auth(plainAuth); err == nil {
262+
return nil
263+
}
264+
265+
// If PLAIN fails, try LOGIN auth (required by Office 365 and some providers)
266+
loginAuth := &loginAuth{e.SMTPUser, password}
267+
if err := client.Auth(loginAuth); err != nil {
268+
return fmt.Errorf("SMTP authentication failed: %w", err)
269+
}
270+
271+
return nil
272+
}

0 commit comments

Comments
 (0)