Trustworthy DNS and mail
SPF, DKIM and DMARC
Three DNS-published policies that let a receiving mail server check that a message really comes from the domain it claims.
SPF, DKIM and DMARC are three DNS records that let a receiving mail server check a message comes from the domain
it claims. SPF lists the addresses allowed to send for the envelope sender's domain. DKIM signs headers
and body; the public key is at <selector>._domainkey.<domain>. DMARC (_dmarc.<domain>) counts a pass only
for a domain aligned with the From: line, and says what to do otherwise: none, quarantine or reject.
How SPF, DKIM and DMARC work, step by step
In the SPF, DKIM and DMARC lab a rowing club's captain mails from clubMail
(10.25.1.25, rowclub.lab) to Lotte at a big provider, postboxMail (10.25.2.25), and a forger at
10.25.2.66 writes in the captain's name. The email lab shows the plain relay; here three checks
are added on top of SMTP:
- The sender publishes policies in DNS, as TXT records: SPF at
rowclub.lab(v=spf1 ip4:10.25.1.25 -all), the DKIM public key atmail._domainkey.rowclub.lab(selectormail), and DMARC at_dmarc.rowclub.lab(v=DMARC1; p=reject). - The sending server signs (DKIM, RFC 6376). It hashes the body (
bh=), then signs that hash with the listed headers (h=From:To:Subject) using its private key, after canonicalization (c=relaxed/simple: header case and spacing normalized, body as sent). The result goes on top of the message as aDKIM-Signature:header withd=rowclub.lab; s=mail. - The receiver checks SPF (RFC 7208) during the SMTP conversation. It takes the domain of the envelope sender
(
MAIL FROM), reads its SPF record, and tests the connecting address against each mechanism in the order the record lists them (ip4,a,mx,include, usually ending inall); the first match decides.-allmeans fail,~allsoftfail. At most 10 DNS lookups are allowed; more is a permanent error. - The receiver checks DKIM. It fetches the key at
<s>._domainkey.<d>, recomputes the body hash and verifies the signature. Any change to the body or a signed header fails it. - DMARC decides (RFC 7489). It takes the domain in the visible
From:line and asks: did SPF pass for an aligned envelope domain, or DKIM for an alignedd=? Relaxed alignment (r, the default) accepts the same organizational domain, strict (s) only the exact name. One aligned pass is enough. Otherwise the policy applies:none,quarantineorreject. - The verdict is written down in
Authentication-Results:headers (RFC 8601), and reports go to the addresses in the DMARC record'srua=tag, if any.
What it looks like on a Linux machine
The policies are ordinary DNS records. The 2048-bit key is too long for one 255-character TXT string, so it comes as two, which readers join:
$ dig +short TXT rowclub.lab
"v=spf1 ip4:10.25.1.25 -all"
$ dig +short TXT mail._domainkey.rowclub.lab
"v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuD/7NiZx…" "…"
Postfix hands each message to milters: opendkim signs on clubMail and checks signatures on postboxMail, and opendmarc on postboxMail checks SPF itself, then DMARC. The captain's real message reaches Lotte with the verdicts on top:
Received: from clubMail.lab (clubMail [10.25.1.25])
Authentication-Results: postboxMail.lab; dmarc=pass (p=reject dis=none) header.from=rowclub.lab
Authentication-Results: postboxMail.lab; spf=pass smtp.mailfrom=rowclub.lab
Authentication-Results: postboxMail.lab;
dkim=pass (2048-bit key; unprotected) header.d=rowclub.lab header.i=@rowclub.lab header.a=rsa-sha256 header.s=mail …
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=rowclub.lab; s=mail;
The forger sends "Club fees: new bank account" from its own domain's envelope, billing@quickparcel.lab, with
From: captain@rowclub.lab. SPF passes for quickparcel.lab, nothing passes for rowclub.lab, and postboxMail
refuses it at the end of the SMTP conversation:
$ grep reject /var/log/mail.log
… milter-reject: END-OF-MESSAGE from forger[10.25.2.66]: 5.7.1 rejected by DMARC policy for rowclub.lab; from=<billing@quickparcel.lab> to=<lotte@postbox.lab>
curl, the sender here, reports it as curl: (8) Weird server reply.
What goes wrong, and how you see it
A real sender is missing. A newsletter service or a new server mails as the domain from an address SPF
does not list, and does not sign with the domain's key: spf=fail, no aligned DKIM, and p=reject bounces
every copy. Add it to SPF (services hand out an include:) and publish its DKIM key under its own selector.
SPF passes for the wrong domain. The forger uses its own envelope sender, whose SPF lists its own server:
spf=pass smtp.mailfrom=quickparcel.lab. DMARC still fails, because quickparcel.lab is not aligned with From: rowclub.lab. Alignment is the point of DMARC.
The DMARC record is at the wrong name. Receivers ask only for _dmarc.<domain>. A record typed as dmarc
is served happily at the wrong name, and every receiver writes dmarc=none: no policy at all.
The body changed in transit. A footer added by a mailing list, or an edit by an attacker, breaks the body hash and DKIM fails. Forwarding also breaks SPF, since the forwarder's address is not listed; ARC (RFC 8617) exists to carry the original results across such hops.
A signed message is replayed. Someone with one real message sends it again, unchanged, from their own server.
DKIM still passes, so DMARC passes, and it lands in the inbox. The newest Received: line shows where this copy
really came from.
The policy only watches. With p=none a failing forgery is delivered. Move to quarantine, then reject,
once reports show every real sender passing.
Questions people ask
Do I need all three? Yes. SPF alone breaks on forwarding, DKIM alone does not say what to do with a
failure, and neither checks the From: line the reader sees. DMARC needs at least one of them to pass, aligned.
What does "dmarc=fail" with "spf=pass" mean? SPF passed for the envelope domain, but that domain does not match the visible From domain. Typical of a forger using its own domain, or a third-party sender not set up to sign as you.
Where are SPF, DKIM and DMARC stored? All three are DNS TXT records: SPF on the domain
itself, DKIM at <selector>._domainkey.<domain>, DMARC at _dmarc.<domain>.
Read next
- SMTP How mail moves: from a client to its own mail server, then from server to server, found by the domain's MX record.
- DNS records The typed entries of a zone: A for an address, CNAME for an alias, NS for delegation, and more.
- DNSSEC and encrypted DNS Signed DNS answers, and DNS carried over an encrypted connection to a resolver you chose.
- Phishing A message that impersonates someone a target trusts to make them act — click a link, hand over a login, open a file — before they think.
- Lookalike domain A domain registered to pass for a real one at a glance — a typo, a swapped letter pair, an extra word or a different ending — used to host phishing, harvesters or misdirected mail.