Sub topic 5
Setting up SPF, DKIM and DMARC
This chapter assumes you know how SMTP delivery works, can edit TXT records in your DNS zone, and can send mail from an email service provider (ESP) or your own server. You will publish three records for a sending subdomain, check them, and learn the errors that most often break them.
What each record does
Mailbox providers use three standards to decide whether a message really comes from the domain it claims.
- SPF (Sender Policy Framework) lists the IP addresses allowed to send for a domain. The receiver checks the connecting IP against that list.
- DKIM (DomainKeys Identified Mail) adds a cryptographic signature to each message. The receiver fetches the public key from DNS and verifies the signature.
- DMARC (Domain-based Message Authentication, Reporting and Conformance) tells receivers what to do when SPF and DKIM fail to align with the visible From domain. It also asks them to send you reports.
Alignment is the part people miss. DMARC passes only if SPF or DKIM passes and the domain that passed matches the From domain. Passing SPF for your ESP's bounce domain does not help if your From address uses your own domain.
Publish the records
These three records are for the sending subdomain mail.example.com. Replace the IP address, selector and key with your own values.
; Example zone entries for the sending subdomain mail.example.com
; Replace the IP address, selector and key with your own values.
mail.example.com. IN TXT "v=spf1 ip4:203.0.113.25 -all"
selector1._domainkey.mail.example.com. IN TXT "v=DKIM1; k=rsa; p=<base64-encoded public key>"
_dmarc.mail.example.com. IN TXT "v=DMARC1; p=none; rua=mailto:[email protected]"- The SPF record authorizes one IP and ends in
-all, which tells receivers to treat every other sender as a failure. If you send through an ESP, use theinclude:mechanism the ESP documents instead of, or alongside,ip4:. - The DKIM record sits under
<selector>._domainkey. The selector is a label you choose, and it lets you publish several keys and rotate them. Your ESP or mail server generates the key pair. You publish only the public half. Use a 2048-bit key if your DNS provider accepts long TXT values. - The DMARC record starts at
p=none. This is monitoring mode. Mail is not affected, and you receive aggregate reports at theruaaddress.
Verify before the first send
Check that each record resolves, and confirm the sending IP has matching reverse DNS.
# SPF record of the sending domain
dig +short TXT mail.example.com
# DKIM public key for the selector
dig +short TXT selector1._domainkey.mail.example.com
# DMARC policy
dig +short TXT _dmarc.mail.example.com
# Reverse DNS (PTR) of the sending IP
dig +short -x 203.0.113.25
# Forward lookup of the PTR hostname; it should return the same IP
dig +short A mail.example.comThen send a test message to a mailbox you control and read the headers. Look for spf=pass, dkim=pass and dmarc=pass in the Authentication-Results header. Also confirm that header.d for DKIM and the SPF domain match your From domain.
Common errors
| Symptom | Likely cause |
|---|---|
| `spf=permerror` | Two SPF records, or more than 10 lookups |
| `dkim=fail (body hash did not verify)` | Something changed the message after signing, such as a footer added by a gateway |
| `dkim=neutral` or no signature | Wrong selector, or the key was not published |
| `dmarc=fail` with SPF and DKIM passing | The passing domains do not align with the From domain |
| No DMARC reports arrive | Typo in the `rua` address, or the report address is on another domain that has not authorized reports |
Tightening DMARC later
Stay at p=none while you warm up and read the reports. Once legitimate sources all pass and align, move to p=quarantine, and later to p=reject. Changing the policy early can block your own mail.
Exercise
Your test message shows spf=pass and dkim=pass, but dmarc=fail. The From address is [email protected]. What is the most likely cause, and what do you check?
Show answer
Alignment is failing. SPF probably passed for the ESP's bounce domain and DKIM probably signed with the ESP's domain, so neither matches mail.example.com. Check header.d in the DKIM result and the Return-Path domain for SPF. Then configure the ESP to sign with your own domain (using your selector) and, if it supports it, use a custom return-path on your subdomain.