Sub topic 8
Setting up SPF, DKIM and DMARC for your domain
This chapter assumes you can log in to your domain's DNS settings and know that a TXT record is a DNS entry holding text. It also assumes you have read the deliverability chapter, which explains why mailbox providers distrust unauthenticated mail. After this chapter you should be able to publish all three records, check them, and explain why a provider's setup wizard sometimes fails.
What each record proves
Email lets anyone type any address in the "From" field. Authentication adds three checks that a receiving server can run.
| Record | Question it answers | What it checks | Where it lives |
|---|---|---|---|
| SPF | Is this server allowed to send for the domain? | The sending server's IP address | TXT record at the domain |
| DKIM | Was this message altered, and did the domain sign it? | A cryptographic signature in the message headers | TXT or CNAME record at `selector._domainkey` |
| DMARC | What should the receiver do if the checks fail or don't line up with the visible From address? | SPF and DKIM results plus alignment | TXT record at `_dmarc` |
Gmail and Yahoo have required SPF, DKIM and DMARC from bulk senders (about 5,000 or more messages a day to their users) since 2024. Smaller senders also get better placement when all three pass.
The three records
Providers give you the exact values. The records below show the shape only, and the include value, selector and key are placeholders.
; SPF: authorizes the provider's servers to send for your domain.
; Use the exact include value your provider documents; this one is a placeholder.
example.com. TXT "v=spf1 include:spf.provider.example ~all"
; DKIM: public key your provider gives you. Selector name and key come from the provider.
selector1._domainkey.example.com. TXT "v=DKIM1; k=rsa; p=<public-key-from-provider>"
; DMARC: start in monitoring mode and read the reports before tightening the policy.
_dmarc.example.com. TXT "v=DMARC1; p=none; rua=mailto:[email protected]"SPF in detail
SPF checks the envelope sender, also called the Return-Path or bounce address. That address is often not the one readers see in the From field. When you send through an ESP, the ESP usually sets the envelope domain to its own domain, so SPF passes for the ESP's domain but does not match your From domain. This is why SPF alone does not satisfy DMARC for most ESP setups, and why DKIM matters more.
Rules that cause most SPF failures:
- One SPF record per domain. Two
v=spf1TXT records make SPF return a permanent error. If you add a second provider, merge it into the existing record:v=spf1 include:spf.provider.example include:spf.other.example ~all. - Ten DNS lookups at most. Each
include,a,mxandredirectcounts, and nested includes count too. An eleventh lookup produces a permanent error. Remove providers you no longer use. - The ending matters.
~allis a softfail, meaning "probably not authorized".-allis a hardfail, meaning "reject". Start with~all. DMARC gives you the enforcement you need. - Some ESPs do not ask for SPF on your From domain. If the provider handles the envelope domain itself, follow its instructions and don't add an include it doesn't request.
DKIM in detail
Your provider holds a private key and signs each outgoing message. You publish the matching public key in DNS under a name made of a selector and _domainkey. The selector lets one domain publish several keys, for example one per provider, so they don't collide.
- Some providers give you a TXT record with the key. Others give you CNAME records that point to keys they host, which lets them rotate keys for you. Publish whichever they specify.
- Prefer 2048-bit keys where the provider offers them. A long key may exceed the 255-character limit of a single DNS string. Most DNS panels split it automatically. If yours does not, you must split it into quoted chunks within one record.
- Some DNS panels append your domain automatically. If you enter
selector1._domainkey.example.comand the panel adds.example.com, you create a broken name. Enter onlyselector1._domainkeyin that case.
DMARC in detail
DMARC passes when SPF or DKIM passes and the domain that passed aligns with the domain in the visible From address. In relaxed alignment (the default), a subdomain such as mail.example.com aligns with example.com. In strict alignment the domains must match exactly.
The main tags are:
p: the policy.noneonly monitors,quarantineasks receivers to treat failures as suspicious, andrejectasks them to refuse failures.rua: an address that receives daily aggregate XML reports showing who sends mail using your domain and whether it passes.pct: the percentage of failing messages the policy applies to. It lets you ramp up enforcement.adkimandaspf:rfor relaxed orsfor strict alignment.
A safe rollout order
- Inventory senders. List every system that sends mail as your domain: your ESP, your office mailbox, billing, support, forms.
- Add SPF. Merge all authorized senders into one record, and stay under ten lookups.
- Add DKIM for each sender. Use each provider's wizard to get the selector and key, and publish them.
- Publish DMARC at
p=none. Include aruaaddress that you or a reporting service actually reads. - Verify and wait. Send test messages and read the DMARC reports for two to four weeks.
- Tighten gradually. Move to
p=quarantine; pct=25, raisepctto 100, and then considerp=rejectonce legitimate sources all pass.
Consider sending marketing mail from a subdomain such as news.example.com. Its reputation is then separate from your main domain's, so a bad campaign cannot harm the mailboxes your team uses day to day. DMARC at the organizational domain covers subdomains unless you set an sp tag.
Checking your work
Query the records with dig, which ships with most Linux and macOS systems. On Windows use nslookup -type=TXT.
dig +short TXT example.com
dig +short TXT selector1._domainkey.example.com
dig +short TXT _dmarc.example.comDNS changes can take from a few minutes to a few hours to propagate, depending on the record's TTL (time to live). Then send a real message to a mailbox you control and open the full headers. Look for spf=pass, dkim=pass and dmarc=pass in the Authentication-Results header. In Gmail, choose "Show original" to see them.
Common errors
- Two SPF records. Merge them into one.
- More than ten lookups. Remove unused includes, or ask which providers you really still use.
- DKIM selector typed wrongly or pasted with line breaks. Re-copy the value, and check that the name matches the selector in the provider's panel.
- SPF passes but DMARC fails. The SPF domain does not align with your From domain. Make sure DKIM is set up for your own domain.
- No one reads the reports. Raw XML is hard to read. Many providers and free services parse DMARC reports into dashboards.
- Old records after switching providers. Leave the old DKIM record in place until the migration is finished. Remove old SPF includes afterward to free up lookups.
Exercise
Your DMARC reports show that mail from your ESP fails SPF alignment but passes DKIM, and your domain has p=none. A colleague wants to switch to p=reject right away because "DKIM passes". Is that safe, and what should you check first?
Show answer
DKIM passing with alignment is enough for DMARC to pass, so ESP mail would not be rejected. It is still not safe to switch yet. The reports may also show other sources, such as billing or support tools, that fail both checks. Review every sending source in the reports, fix or authenticate each one, and then move gradually through quarantine with a rising pct before using reject.