diff --git a/go.mod b/go.mod index 61de0eb..357dd1a 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,13 @@ go 1.27.1 require ( github.com/emersion/go-message v0.18.2 + github.com/emersion/go-smtp v0.25.1 modernc.org/sqlite v1.62.0 ) require ( github.com/dustin/go-humanize v1.1.0 // indirect + github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 // indirect github.com/google/uuid v1.6.0 // indirect github.com/mattn/go-isatty v0.0.24 // indirect github.com/ncruces/go-strftime v1.1.0 // indirect diff --git a/go.sum b/go.sum index c28e7d8..ad6caee 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,10 @@ github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2 github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= github.com/emersion/go-message v0.18.2 h1:rl55SQdjd9oJcIoQNhubD2Acs1E6IzlZISRTK7x/Lpg= github.com/emersion/go-message v0.18.2/go.mod h1:XpJyL70LwRvq2a8rVbHXikPgKj8+aI0kGdHlg16ibYA= +github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 h1:oP4q0fw+fOSWn3DfFi4EXdT+B+gTtzx8GC9xsc26Znk= +github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ= +github.com/emersion/go-smtp v0.25.1 h1:y6nzefT6PtFeMItFw97GX/ZCkVbh0rqSXG8b3ang8/w= +github.com/emersion/go-smtp v0.25.1/go.mod h1:ZtRRkbTyp2XTHCA+BmyTFTrj8xY4I+b4McvHxCU2gsQ= github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50 h1:PlNWJRxu5APSV0pWqfIsZ0qt9OOGHSWfZQ4zvx+WgWs= github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50/go.mod h1:yUo1AMMpsPIDFGuwDK8nLcf8+vvAd5l2O6JDZ6hpwrM= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= diff --git a/internal/outbound/outbound.go b/internal/outbound/outbound.go new file mode 100644 index 0000000..9ca977a --- /dev/null +++ b/internal/outbound/outbound.go @@ -0,0 +1,81 @@ +// Package outbound delivers messages to the SMTP relay. +package outbound + +import ( + "bytes" + "context" + "fmt" + "net" + "sync" + "time" + + "github.com/emersion/go-smtp" +) + +// Sender delivers one message to a set of recipients. +type Sender interface { + // Send submits data with the given envelope sender to every address in to. + Send(ctx context.Context, from string, to []string, data []byte) error +} + +// DefaultBatchSize is the number of recipients per SMTP transaction. +const DefaultBatchSize = 50 + +// SMTPRelay sends through a plain SMTP relay such as a local Postfix. +type SMTPRelay struct { + Addr string // host:port + BatchSize int // recipients per transaction; DefaultBatchSize if zero +} + +// Send splits recipients in batches and sends one transaction per batch. +func (r *SMTPRelay) Send(ctx context.Context, from string, to []string, data []byte) error { + size := r.BatchSize + if size <= 0 { + size = DefaultBatchSize + } + for start := 0; start < len(to); start += size { + end := min(start+size, len(to)) + if err := r.sendBatch(ctx, from, to[start:end], data); err != nil { + return fmt.Errorf("batch %d-%d: %w", start, end, err) + } + } + return nil +} + +func (r *SMTPRelay) sendBatch(ctx context.Context, from string, to []string, data []byte) error { + d := net.Dialer{Timeout: 10 * time.Second} + conn, err := d.DialContext(ctx, "tcp", r.Addr) + if err != nil { + return err + } + c := smtp.NewClient(conn) + defer c.Close() + if err := c.Hello("localhost"); err != nil { + return err + } + if err := c.SendMail(from, to, bytes.NewReader(data)); err != nil { + return err + } + return c.Quit() +} + +// Sent is a message recorded by Fake. +type Sent struct { + From string + To []string + Data []byte +} + +// Fake records messages instead of sending them. For tests. +type Fake struct { + mu sync.Mutex + Sent []Sent +} + +// Send records the message. +func (f *Fake) Send(_ context.Context, from string, to []string, data []byte) error { + f.mu.Lock() + defer f.mu.Unlock() + f.Sent = append(f.Sent, Sent{from, append([]string(nil), to...), append([]byte(nil), data...)}) + return nil +} diff --git a/internal/outbound/outbound_test.go b/internal/outbound/outbound_test.go new file mode 100644 index 0000000..fdbda0d --- /dev/null +++ b/internal/outbound/outbound_test.go @@ -0,0 +1,115 @@ +package outbound + +import ( + "context" + "fmt" + "io" + "net" + "sync" + "testing" + + "github.com/emersion/go-smtp" +) + +type captured struct { + from string + to []string + data string +} + +type capture struct { + mu sync.Mutex + msgs []captured +} + +func (c *capture) NewSession(*smtp.Conn) (smtp.Session, error) { return &session{c: c}, nil } + +type session struct { + c *capture + from string + to []string +} + +func (s *session) Reset() { s.from, s.to = "", nil } +func (s *session) Logout() error { return nil } +func (s *session) Mail(from string, _ *smtp.MailOptions) error { + s.from = from + return nil +} +func (s *session) Rcpt(to string, _ *smtp.RcptOptions) error { + s.to = append(s.to, to) + return nil +} +func (s *session) Data(r io.Reader) error { + b, _ := io.ReadAll(r) + s.c.mu.Lock() + s.c.msgs = append(s.c.msgs, captured{s.from, s.to, string(b)}) + s.c.mu.Unlock() + return nil +} + +func startServer(t *testing.T) (string, *capture) { + t.Helper() + cap := &capture{} + srv := smtp.NewServer(cap) + srv.AllowInsecureAuth = true + l, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + go srv.Serve(l) + t.Cleanup(func() { srv.Close() }) + return l.Addr().String(), cap +} + +func TestSMTPRelaySend(t *testing.T) { + addr, cap := startServer(t) + r := &SMTPRelay{Addr: addr} + err := r.Send(context.Background(), "bounce@example.org", []string{"a@x.com", "b@x.com"}, []byte("Subject: hi\r\n\r\nbody\r\n")) + if err != nil { + t.Fatalf("send: %v", err) + } + if len(cap.msgs) != 1 { + t.Fatalf("want 1 transaction, got %d", len(cap.msgs)) + } + m := cap.msgs[0] + if m.from != "bounce@example.org" || len(m.to) != 2 { + t.Fatalf("unexpected envelope: %+v", m) + } + if m.data != "Subject: hi\r\n\r\nbody\r\n" { + t.Fatalf("data = %q", m.data) + } +} + +func TestSMTPRelayBatches(t *testing.T) { + addr, cap := startServer(t) + r := &SMTPRelay{Addr: addr, BatchSize: 50} + var to []string + for i := 0; i < 120; i++ { + to = append(to, fmt.Sprintf("u%d@x.com", i)) + } + if err := r.Send(context.Background(), "b@example.org", to, []byte("Subject: x\r\n\r\n.\r\n")); err != nil { + t.Fatal(err) + } + if len(cap.msgs) != 3 { + t.Fatalf("want 3 transactions, got %d", len(cap.msgs)) + } + if len(cap.msgs[0].to) != 50 || len(cap.msgs[2].to) != 20 { + t.Fatalf("batch sizes: %d %d", len(cap.msgs[0].to), len(cap.msgs[2].to)) + } +} + +func TestSMTPRelayUnreachable(t *testing.T) { + r := &SMTPRelay{Addr: "127.0.0.1:1"} + if err := r.Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("x")); err == nil { + t.Fatal("want error") + } +} + +func TestFakeRecords(t *testing.T) { + f := &Fake{} + f.Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("m")) + if len(f.Sent) != 1 || f.Sent[0].From != "b@x.org" || string(f.Sent[0].Data) != "m" { + t.Fatalf("fake: %+v", f.Sent) + } +}