Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
35 changes: 34 additions & 1 deletion internal/mailer/mailer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package mailer

import "context"
import (
"context"
"fmt"
"net/smtp"
)

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

func (n *Noop) Send(_ context.Context, _ Message) error { return nil }


type loginAuth struct {
username, password string
}

func LoginAuth(username, password string) smtp.Auth {
return &loginAuth{username, password}
}

func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
// This sends: AUTH LOGIN
// The server will then prompt for the username, which your Next() method will catch.
return "LOGIN", nil, nil
}

func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if more {
switch string(fromServer) {
case "Username:", "VXNlcm5hbWU6":
return []byte(a.username), nil
case "Password:", "UGFzc3dvcmQ6":
return []byte(a.password), nil
default:
return nil, fmt.Errorf("unknown challenge: %s", string(fromServer))
}
}
return nil, nil
}
7 changes: 3 additions & 4 deletions internal/mailer/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@ func (s *SMTP) Send(ctx context.Context, msg Message) error {
defer c.Close()

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

Expand Down
Loading