Why our sign-in codes went to spam
Every Chaufr sign-in needs a code sent by email, so email is not optional for us. During the beta, a rider told us his code had landed in Gmail's spam folder. Asking him to mark it "not spam" helps a little. Finding out why helps everyone after him.
We send our own mail, through ocaml-smtpd, Mark Elvers' mail server written in OCaml. That makes us responsible for everything a big email provider would otherwise handle for us.
What Gmail checks
Since 2024, Gmail asks every sender, however small, for a few things:
- SPF: a DNS record listing the addresses allowed to send mail for the domain.
- DKIM: a signature on each message, checked against a public key in DNS.
- Reverse DNS: the sending address must name a host, and that host must point back to the same address.
- TLS on the way in, and a low rate of spam complaints.
DMARC, a policy that ties SPF and DKIM to the visible From address, is required only of bulk senders, but it is free and worth having.
Read the verdict before guessing
Gmail will tell you what it decided. Open a message, choose Show original, and the top of the page lists SPF, DKIM and DMARC as pass or fail. For a code sent to ourselves, all three passed, over TLS 1.3. So what was different for the rider?
The address nobody had set up
Our records described the server's IPv4 address. But the server also had an IPv6 address, and Gmail accepts mail over both. A message that leaves over IPv6 comes from an address our SPF record did not list, with no reverse DNS name. Gmail is especially strict about mail over IPv6.
We never confirmed which way the rider's message travelled, so this is the likely cause rather than a proven one. It was worth closing either way:
chaufr.invarware.com AAAA 2001:db8::1
chaufr.invarware.com TXT "v=spf1 ip4:203.0.113.10 ip6:2001:db8::1 -all"
2001:db8::1 (reverse) PTR chaufr.invarware.com
_dmarc.chaufr... TXT "v=DMARC1; p=none; rua=mailto:..."
(The addresses above are documentation examples, not ours.)
The rua part of DMARC asks providers to send us daily reports of every address that sent mail as our domain, and whether it passed. The next time something fails, we will see it in a report instead of hearing about it from a rider.
A header that said the wrong thing
Show original turned up something else. Our mail server added its own Authentication-Results header to every message it sent, claiming spf=temperror and dkim=none.
That header is meant for mail arriving from other servers: the server checks the stranger's mail and records the verdict. ocaml-smtpd ran those checks on everything, including mail Chaufr hands it to send out, where the checks mean nothing. Gmail mostly ignores such headers from other servers, so this was unlikely to be the spam cause, but a message should not carry a false statement about itself.
The fix is one rule. Mail from the machine itself, or from a signed-in user, is outgoing: it is sent as it is, and only mail from other servers is checked. In OCaml, written for this post and checked on every build with mdx:
let is_loopback ip =
String.starts_with ~prefix:"127." ip
|| ip = "::1"
|| String.starts_with ~prefix:"::ffff:127." ip
let is_submission ~client_ip ~username =
is_loopback client_ip || Option.is_some username
# is_submission ~client_ip:"127.0.0.1" ~username:None;;
- : bool = true
# is_submission ~client_ip:"203.0.113.9" ~username:(Some "sender");;
- : bool = true
# is_submission ~client_ip:"203.0.113.9" ~username:None;;
- : bool = false
After deploying it, a fresh sign-in code arrived with only Gmail's own verdict, SPF, DKIM and DMARC all passing, one second after it was sent. We plan to offer the fix upstream, since anyone using ocaml-smtpd to send an application's mail meets the same header.
The lesson
Running your own mail is cheap and it works, but nobody checks it for you. Two habits do most of the work: read Show original before guessing, and ask for DMARC reports so the evidence comes to you.
Thanks to the rider who told us. If your product depends on email arriving, we would like to hear about it.
— Chukwuma