Sub topic 5
Setting up SPF, DKIM and DMARC
This chapter assumes you know how SMTP delivery works, can edit TXT records in your sending domain's DNS zone, and have a mail server or email service provider (ESP) that can send from the new domain. Authentication comes before any warm-up volume. Without it, mailbox providers have no reliable way to tie your mail to your domain, so they cannot attach a reputation to it.
What each protocol proves
The three protocols answer different questions.
| Protocol | Question it answers | Where it lives | What it checks |
|---|---|---|---|
| SPF | Is this server allowed to send for this domain? | TXT record at the envelope sender domain | Connecting IP against a list of authorized IPs |
| DKIM | Was this message signed by the domain, and was it altered? | TXT record at `selector._domainkey.domain`, plus a header in the message | Cryptographic signature over selected headers and the body |
| DMARC | Do SPF or DKIM pass for the domain the recipient sees, and what should happen if not? | TXT record at `_dmarc.domain` | Alignment with the From domain, then applies your policy |
SPF and DKIM each authenticate a domain, but not necessarily the one in the visible From header. DMARC closes that gap through alignment.
SPF
SPF is a TXT record that lists the hosts allowed to send mail for a domain. The receiver takes the domain from the SMTP envelope sender (the MAIL FROM, also called the Return-Path), looks up its SPF record, and compares the connecting IP address with the list.
mail.example.com. IN TXT "v=spf1 ip4:203.0.113.25 -all"The parts are:
v=spf1identifies the record as SPF.ip4:203.0.113.25authorizes one IPv4 address. Useip6:for IPv6 andinclude:to pull in an ESP's published list.-allis a hard fail for everything not listed.~allis a soft fail and is common during testing.
SPF gotchas
- Only one SPF record per domain. Two
v=spf1records produce apermerrorand SPF fails. Merge them into one. - Ten DNS lookups at most. Each
include,a,mx,existsandredirectcounts. Going over also gives apermerror. Plainip4andip6entries do not count. - The record belongs to the envelope domain. If your ESP sets its own Return-Path domain, SPF authenticates that domain, not yours. The result is a pass that does not align for DMARC. Ask the ESP for a custom return-path (bounce) domain on your subdomain.
- Forwarding breaks SPF. The forwarder's IP is not in your record. This is one reason DKIM matters.
DKIM
DKIM adds a DKIM-Signature header, created with a private key held by your mail system. The receiver reads the d= (signing domain) and s= (selector) tags, fetches the public key from DNS, and verifies the signature.
selector1._domainkey.mail.example.com. IN TXT "v=DKIM1; k=rsa; p=<base64-encoded public key>"Most ESPs generate the key pair and give you either the TXT value or a CNAME to publish. If you run your own server, tools such as OpenDKIM generate the pair.
DKIM practical points
- Use 2048-bit RSA keys. 1024-bit keys are still accepted by many receivers but are weaker. Some DNS panels limit TXT strings to 255 characters. A 2048-bit key exceeds that, so split it into several quoted strings inside one record. Most DNS providers do this automatically.
- Selectors let you rotate keys. Publish a new selector, switch signing to it, and remove the old one after a delay. Rotating every six to twelve months is a reasonable routine.
- Set
d=to your own domain. If the ESP signs with its domain, DKIM passes but does not align with your From domain, so it contributes nothing to DMARC. - Sign the headers that matter. At minimum
From,To,Subject,DateandMessage-ID. A mailing list or gateway that edits the body or those headers will break the signature.
DMARC
DMARC ties the two checks to the From address, which is the one users see. A message passes DMARC when at least one of SPF or DKIM passes and the authenticated domain aligns with the From domain.
_dmarc.mail.example.com. IN TXT "v=DMARC1; p=none; rua=mailto:[email protected]"The key tags are:
| Tag | Meaning |
|---|---|
| `p` | Policy for the domain: `none`, `quarantine` or `reject` |
| `rua` | Address for aggregate (XML) reports |
| `sp` | Policy for subdomains, when different from `p` |
| `adkim`, `aspf` | Alignment mode: `r` (relaxed, default) or `s` (strict) |
| `pct` | Percentage of failing mail the policy applies to |
Alignment
Relaxed alignment accepts a match on the organizational domain. A signature with d=example.com aligns with a From of [email protected]. Strict alignment needs an exact match. Relaxed is the right default for most senders.
Rolling out the policy
Start with p=none. It changes no delivery but makes receivers send you aggregate reports, which show every source sending as your domain. You usually find forgotten systems this way, such as a billing tool or a CRM.
- Publish
p=nonewith aruaaddress and collect reports for two to four weeks. - Fix any legitimate source that fails or is unaligned.
- Move to
p=quarantine, optionally withpct=25rising to 100. - Move to
p=rejectonce reports show only legitimate mail passing.
For a brand-new subdomain that has only one known sender, you can move through these stages faster. Do not stay on p=none forever. It offers no protection against spoofing.
Verifying before you send
Check each record from outside your DNS provider's interface. Use dig to confirm what the public DNS returns.
# 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.comThen send a real test message to a mailbox you control at a large provider. Open the original message source and read the Authentication-Results header. You want to see something like this:
Authentication-Results: mx.example.net;
spf=pass smtp.mailfrom=bounce.mail.example.com;
dkim=pass header.d=mail.example.com;
dmarc=pass header.from=mail.example.comCheck three things in that output. The dkim=pass line names your domain in header.d. The spf=pass line names a domain that aligns with your From domain. The dmarc=pass line is present. A pass for SPF or DKIM with a foreign domain is not enough.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `spf=permerror` | Two SPF records, or more than 10 lookups | Merge records, remove unneeded includes |
| `spf=pass` but `dmarc=fail` | Return-Path uses the ESP's domain | Configure a custom return-path on your domain |
| `dkim=fail (body hash did not verify)` | A gateway or footer injector changed the body after signing | Sign after all modifications, or disable the injector |
| `dkim=none` | Signing is not enabled, or the selector name is wrong | Check the selector in the message header against DNS |
| DNS lookup returns nothing | Wrong host name, such as a doubled domain suffix from a DNS panel that appends the zone | Enter the relative name the panel expects, then retest |
| No DMARC reports arrive | Typo in `rua`, or the report address is on another domain without authorization | Check the syntax and the external reporting authorization record |
Where this fits in the warm-up
Authentication does not create reputation. It makes sure the reputation your sending earns is credited to your domain. It is also a stated requirement for bulk senders at the large providers, which later chapters cover. Finish these records, confirm them with a live test, and then move on to reverse DNS.
Exercise
You send a test message and see spf=pass smtp.mailfrom=bounces.espvendor.net, dkim=pass header.d=espvendor.net and dmarc=fail header.from=mail.example.com. Why does DMARC fail, and what do you change?
Show answer
Both SPF and DKIM passed, but for the ESP's domain (espvendor.net), not for the From domain mail.example.com. Neither result aligns, so DMARC fails. Configure the ESP to use a custom return-path domain on your subdomain for SPF, and to sign with your own domain (d=mail.example.com) by publishing the DKIM record it supplies. Then retest and confirm that dmarc=pass appears.
Exercise
Your domain publishes v=spf1 include:_spf.espone.example -all and a second record v=spf1 include:_spf.esptwo.example -all. What happens, and how do you fix it?
Show answer
Two SPF records on one domain cause an SPF permerror, so SPF fails for all mail. Merge them into one record: v=spf1 include:_spf.espone.example include:_spf.esptwo.example -all. Then count the DNS lookups to confirm the total stays at 10 or fewer.