Files
Mateusz PstruchaandClaude Opus 5.5 fd8ee8311c
CI / test (push) Successful in 2m28s
Add sending: outbox queue, SMTP worker, drafts
Drafts and queued sends are one outbox row in different states, drained
by a background worker so SMTP never runs in the request path (§3.3).
Retries back off 1-16 min; 5xx, auth and TLS failures fail fast; items
a crashed worker left mid-send are reclaimed. Each message keeps its
Message-ID across retries, so an at-least-once duplicate is recognisable.
Reply headers are derived server-side from the parent, and sent mail is
filed into its thread alongside received mail.

Three things the libraries would have got wrong:
- mail-builder's .bcc() writes an ordinary Bcc header every recipient
  sees. Bcc goes in the envelope only; a test checks it never reaches the
  rendered bytes, and was confirmed to fail if .bcc() is reintroduced.
- mail-send trusts only compiled-in public CAs, while IMAP (native-tls)
  uses the OS store — a server with an internal-CA cert would sync but
  never send. SMTP now uses the OS store too.
- rustls is added with default-features off: its defaults pull in
  aws-lc-rs, which needs cmake/clang and would break the runner build.

Also fixes: UIDVALIDITY resets now only delete IMAP-sourced rows (sent
mail shares the table and exists nowhere else); first sync no longer
logs a false "UIDVALIDITY changed" warning; Message-IDs are stored bare
whichever path writes them (ENVELOPE brackets them, mail-parser doesn't);
accounts can choose STARTTLS vs implicit TLS for SMTP, defaulting by port.

Verified end to end against a TLS mail server with a private-CA cert,
with no plaintext patching: IMAPS sync, SMTPS send, delivery and headers
checked from the recipients' mailboxes, Bcc absent from other copies,
plaintext refused when STARTTLS is unavailable, retry scheduling on
connection failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 10:59:28 +02:00

52 lines
2.6 KiB
SQL

-- Sending: SMTP submission via an outbox queue (server-architecture-spec.md §3.3).
-- An IMAP username isn't necessarily an email address ("alice" vs
-- "alice@example.com"), so the sender identity is stored explicitly.
ALTER TABLE accounts ADD COLUMN email_address TEXT;
ALTER TABLE accounts ADD COLUMN display_name TEXT;
UPDATE accounts SET email_address = username WHERE username LIKE '%@%';
-- Sent mail lives alongside synced mail so a thread shows both sides of the
-- conversation. Sync is one-way and only reads INBOX, so without this a
-- reply would vanish from its own thread. Sent rows have no IMAP UID.
ALTER TABLE messages ADD COLUMN source TEXT NOT NULL DEFAULT 'imap';
ALTER TABLE messages ALTER COLUMN uid DROP NOT NULL;
ALTER TABLE messages ADD CONSTRAINT messages_uid_matches_source
CHECK ((source = 'imap') = (uid IS NOT NULL));
CREATE TABLE outbox (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
account_id UUID NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
-- draft: editable, never sent automatically
-- pending: queued, waiting for next_attempt_at
-- sending: claimed by the worker
-- sent / failed: terminal (failed can be edited and re-queued)
status TEXT NOT NULL DEFAULT 'draft'
CHECK (status IN ('draft', 'pending', 'sending', 'sent', 'failed')),
to_addrs TEXT[] NOT NULL DEFAULT '{}',
cc_addrs TEXT[] NOT NULL DEFAULT '{}',
bcc_addrs TEXT[] NOT NULL DEFAULT '{}',
subject TEXT NOT NULL DEFAULT '',
text_body TEXT NOT NULL DEFAULT '',
-- Reply context, derived server-side from the parent message rather than
-- trusted from the client.
in_reply_to TEXT,
references_ TEXT[] NOT NULL DEFAULT '{}',
thread_id UUID,
-- Assigned when first queued and reused on every retry, so if a server
-- accepted a message but the ack was lost, the retry is recognisably the
-- same message to recipients' clients rather than a new one.
message_id TEXT,
attempts INTEGER NOT NULL DEFAULT 0,
last_error TEXT,
next_attempt_at TIMESTAMPTZ,
sent_message_id UUID REFERENCES messages(id) ON DELETE SET NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
sent_at TIMESTAMPTZ
);
CREATE INDEX outbox_due_idx ON outbox (next_attempt_at) WHERE status = 'pending';
CREATE INDEX outbox_user_status_idx ON outbox (user_id, status);