Files
LambdaMail/services/protocols/internal/presentation/smtp/submission_backend.go
T
Lucas Rayan 9c8b9c00fc fix(smtp): keep a copy of every submitted message in the sender's Sent folder
A message sent over SMTP submission was delivered but left nothing behind: the
account's Sent folder stayed empty. Webmail files its own copy after sending,
and a mail client cannot do it either, since IMAP APPEND is not implemented, so
nothing ever filled Sent for SMTP clients.

- ProcessOutboundEmailInput.FileSentCopy asks Submit to file the signed message
  in the sender's Sent folder, as read (not unread), sharing the blob the
  delivery jobs already reference; one copy per submission, not per recipient
- the SMTP submission session sets it; the webmail leaves it off because it
  files its own copy without the Bcc line, and asking twice would duplicate it
- filed only after the message is durably queued, so a submission refused by the
  send limit leaves nothing behind; a failure to file is logged, never returned,
  because the mail is already on its way and the client would send it again

Tests: unit cases (asked/not asked, shared blob, failure swallowed, limit
refusal, no repository) and an end-to-end test on the real submission server
that fails with "Sent holds 0 messages" without the fix. Full go test ./...
(25 packages) with TEST_DATABASE_URL, go vet and lang-lint are clean.
2026-10-16 12:51:46 -03:00

162 lines
4.5 KiB
Go

package smtppresentation
import (
"context"
"errors"
"io"
"github.com/emersion/go-sasl"
gosmtp "github.com/emersion/go-smtp"
"lambdamail/protocols/internal/application/port"
"lambdamail/protocols/internal/application/usecase"
)
// SubmissionBackend serves the submission ports (587 and 465). It is a
// separate backend from the MX one on purpose: submission always requires
// authentication and always relays outward, while port 25 never authenticates
// and only accepts mail for local mailboxes (PLAN.md section 4).
type SubmissionBackend struct {
useCase *usecase.ProcessOutboundEmailUseCase
}
func NewSubmissionBackend(uc *usecase.ProcessOutboundEmailUseCase) *SubmissionBackend {
return &SubmissionBackend{useCase: uc}
}
func (b *SubmissionBackend) NewSession(_ *gosmtp.Conn) (gosmtp.Session, error) {
return &submissionSession{useCase: b.useCase}, nil
}
type submissionSession struct {
useCase *usecase.ProcessOutboundEmailUseCase
// authenticated is nil until AUTH succeeds; every other command checks it.
authenticated *port.MailboxAuth
from string
recipients []string
}
// AuthMechanisms advertises PLAIN and LOGIN. The server only offers them once
// the connection is encrypted - go-smtp withholds AUTH before TLS unless
// AllowInsecureAuth is set, which this deployment never sets (PLAN.md
// section 4: credentials never travel in the clear).
func (s *submissionSession) AuthMechanisms() []string {
return []string{sasl.Plain, loginMechanism}
}
func (s *submissionSession) Auth(mech string) (sasl.Server, error) {
switch mech {
case sasl.Plain:
return sasl.NewPlainServer(func(identity, username, password string) error {
return s.authenticate(username, password)
}), nil
case loginMechanism:
return newLoginServer(s.authenticate), nil
default:
return nil, gosmtp.ErrAuthUnsupported
}
}
func (s *submissionSession) authenticate(username, password string) error {
rec, err := s.useCase.Authenticate(context.Background(), username, password)
if err != nil {
return &gosmtp.SMTPError{
Code: 535,
EnhancedCode: gosmtp.EnhancedCode{5, 7, 8},
Message: "Authentication credentials invalid",
}
}
s.authenticated = rec
return nil
}
func (s *submissionSession) Reset() {
s.from = ""
s.recipients = nil
}
func (s *submissionSession) Logout() error {
s.authenticated = nil
return nil
}
func (s *submissionSession) Mail(from string, _ *gosmtp.MailOptions) error {
if s.authenticated == nil {
return errAuthRequired()
}
if err := usecase.AuthorizeSender(s.authenticated, from); err != nil {
return &gosmtp.SMTPError{
Code: 550,
EnhancedCode: gosmtp.EnhancedCode{5, 7, 1},
Message: "Sender address not owned by the authenticated account",
}
}
s.from = from
return nil
}
func (s *submissionSession) Rcpt(to string, _ *gosmtp.RcptOptions) error {
if s.authenticated == nil {
return errAuthRequired()
}
s.recipients = append(s.recipients, to)
return nil
}
func (s *submissionSession) Data(r io.Reader) error {
if s.authenticated == nil {
return errAuthRequired()
}
if len(s.recipients) == 0 {
return &gosmtp.SMTPError{
Code: 503,
EnhancedCode: gosmtp.EnhancedCode{5, 5, 1},
Message: "RCPT TO required before DATA",
}
}
err := s.useCase.Submit(context.Background(), usecase.ProcessOutboundEmailInput{
MailboxID: s.authenticated.ID,
SenderAddr: s.from,
DomainName: s.authenticated.DomainName,
Recipients: s.recipients,
Body: r,
MaxRecipientsPerHour: s.authenticated.MaxRecipientsPerHour,
// A client cannot file its own copy over IMAP, so the server keeps it
FileSentCopy: true,
})
if err == nil {
return nil
}
// A rate-limited sender gets a 4xx: the message is legitimate, the
// account has simply run out of budget for this hour and may retry.
if errors.Is(err, usecase.ErrSendLimitExceeded) {
return &gosmtp.SMTPError{
Code: 451,
EnhancedCode: gosmtp.EnhancedCode{4, 7, 0},
Message: "Hourly recipient limit reached, try again later",
}
}
return &gosmtp.SMTPError{
Code: 451,
EnhancedCode: gosmtp.EnhancedCode{4, 3, 0},
Message: "Temporary error accepting message for delivery",
}
}
func errAuthRequired() error {
return &gosmtp.SMTPError{
Code: 530,
EnhancedCode: gosmtp.EnhancedCode{5, 7, 0},
Message: "Authentication required",
}
}
// Ensure the session satisfies both the base and the auth add-on interface.
var (
_ gosmtp.Session = (*submissionSession)(nil)
_ gosmtp.AuthSession = (*submissionSession)(nil)
)