SPF, DKIM and DMARC for developers, set up on Cloudflare

SPF, DKIM and DMARC are 3 DNS TXT records. SPF lists the servers allowed to send for a domain. DKIM publishes the key that checks each message's signature. DMARC passes a message when SPF or DKIM passes and its domain matches the From address. Roll DMARC out in 3 steps: none, quarantine, then reject.

By · Published · Updated

What do SPF, DKIM and DMARC each do?

All three are published as TXT records in your DNS zone. Receiving servers look them up on every message. The example values below use example.com and the record names Cloudflare Email Sending uses.

RecordDNS typeNameExample valueWhat it proves
SPFTXTcf-bounce.example.comv=spf1 include:_spf.mx.cloudflare.net ~allThe server that connected is allowed to send for the envelope sender's domain
DKIMTXTcf-bounce._domainkey.example.comv=DKIM1; h=sha256; k=rsa; p=MIIB...The message carries a valid signature from a key the domain published, and the signed parts were not changed
DMARCTXT_dmarc.example.comv=DMARC1; p=none; rua=mailto:dmarc@example.comSPF or DKIM passed for a domain that matches the From address, plus your policy and where reports go

SPF is defined in RFC 7208, DKIM in RFC 6376 and DMARC in RFC 7489. SPF records must be TXT records. The dedicated SPF record type was discontinued.

The SPF qualifier on all sets how strict the record is. -all means fail, ~all means softfail, a weak statement that the host is probably not authorized. RFC 7208 advises receivers not to reject on softfail alone.

Each DKIM signature names a signing domain in d= and a selector in s=. The selector lets one domain publish several keys. The public key lives in a TXT record at selector._domainkey.domain. Only one TXT record may exist per selector. DKIM requires the From header to be signed.

What is alignment, and why does DMARC need it?

SPF and DKIM on their own prove a domain, but not necessarily the one the reader sees. A spammer can pass SPF for their own domain and still put your domain in the From address. DMARC closes that gap. It passes a message only when SPF or DKIM passes and the authenticated domain is aligned with the From domain.

ModeTagRuleExample that passes
Relaxed (default)adkim=r, aspf=rBoth domains share the same organizational domainFrom example.com, SPF checked on cf-bounce.example.com
Strictadkim=s, aspf=sThe domains match exactlyFrom example.com, DKIM d=example.com

One aligned pass is enough. DMARC does not need both SPF and DKIM to align. That is why a sending service can authenticate with its own bounce subdomain and still pass your DMARC check under relaxed alignment. The organizational domain is found with the public suffix list, so it is a heuristic (RFC 7489, section 3.2).

How do I roll out DMARC without losing mail?

The p tag sets the policy. Move up one step at a time, and read the aggregate reports between steps.

StepRecordWhat receivers doMove on when
1. Monitorv=DMARC1; p=none; rua=mailto:dmarc@example.comNothing to failing mail. You get reportsReports show every real sender passing with an aligned SPF or DKIM
2. Quarantinev=DMARC1; p=quarantine; pct=10; rua=mailto:dmarc@example.comSend failing mail to spam, for pct percent of messagesNo legitimate mail lands in spam. Raise pct to 100
3. Rejectv=DMARC1; p=reject; rua=mailto:dmarc@example.comRefuse failing mailStay here, and keep reading reports

Details that matter:

Check what is already in your zone before you start. Cloudflare adds v=DMARC1; p=reject; when you onboard a sending domain, but only if no DMARC record exists. If other systems send mail as your domain, switch that record to p=none first. Gmail and Yahoo require a DMARC record from bulk senders, and p=none satisfies them: see the sender requirements checklist.

What mistakes break the records?

MistakeWhat happensFix
Two SPF records on one nameSPF returns permerror and fails for every message (RFC 7208, section 4.5)Merge them into one record with several include: terms
More than 10 DNS lookups in SPFSPF returns permerror. Terms that count: include, a, mx, ptr, exists and redirect. ip4, ip6 and all do not countDrop services you no longer use, or replace include: terms with ip4: ranges. More than 2 lookups that return nothing also trigger permerror
Wrong DKIM selectorReceivers query s= and d= from the signature. A key published under a different name is never foundCopy the name the sender shows. Read s= and d= from a real message header and query that exact name
Two DMARC recordsReceivers drop DMARC for the message (RFC 7489, section 6.6.3)Keep one TXT record on _dmarc
Jumping straight to p=rejectLegitimate mail from a forgotten system is refusedRun p=none with rua first
Pasting a long DKIM key as one stringA TXT string is limited to 255 octetsSplit it into several quoted strings. Receivers join them with no spaces

If a domain already has an SPF record, merge the new entries into it. Cloudflare says the same in its domain setup page.

How do I check the records with dig?

Query each name and read the TXT answer. Add +short for the value only.

# SPF: the envelope sender domain
dig TXT cf-bounce.example.com +short
"v=spf1 include:_spf.mx.cloudflare.net ~all"

# DKIM: selector + _domainkey + signing domain
dig TXT cf-bounce._domainkey.example.com +short
"v=DKIM1; h=sha256; k=rsa; p=MIIB..."

# DMARC
dig TXT _dmarc.example.com +short
"v=DMARC1; p=none; rua=mailto:dmarc@example.com"

Count the answers. Two lines starting with v=spf1 or v=DMARC1 are a bug. To find the right DKIM name, open a delivered message and look at its DKIM-Signature header:

DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=selector1; ...

# the key is at s + ._domainkey. + d
dig TXT selector1._domainkey.example.com +short

To ask a specific resolver, put it after an at sign: dig @1.1.1.1 TXT _dmarc.example.com +short. Cloudflare says a new record that shows "Not Found" usually needs 5 to 15 minutes, and propagation can take up to 24 hours.

How do I set this up on Cloudflare?

Cloudflare Email Sending, a public beta, manages the sending records for each domain you onboard. If you work through an agent, sendmcp is a free, MIT-licensed Worker on your own Cloudflare account whose MCP tools write the records (domains_add) and read back the DMARC policy (deliverability_check). It follows the rule above: it leaves an existing DMARC record alone, writes p=none with reports when other services send as the domain, and keeps Cloudflare's p=reject when nothing else does. The steps are on the SPF, DKIM and DMARC setup page.

Sources

  1. RFC 7208: Sender Policy Framework (SPF) checked
  2. RFC 6376: DomainKeys Identified Mail (DKIM) Signatures checked
  3. RFC 7489: DMARC checked
  4. Cloudflare: Email Service, domain configuration checked
  5. Cloudflare: DNS record types checked
  6. Google: Set up DMARC checked

Questions about this

Do I need both SPF and DKIM for DMARC to pass?

No. DMARC passes when either SPF or DKIM passes for a domain that aligns with the From address. Set up both anyway: Gmail and Yahoo require both from bulk senders.

Can a domain have two SPF records?

No. Two SPF records on the same name make SPF return permerror. Put every sending service in one record, and keep it under 10 DNS lookups.

Is p=none enough?

It is enough to meet the Gmail and Yahoo bulk sender rules, which ask for a DMARC record with at least p=none. It does not stop anyone from spoofing your domain. Move to quarantine and then reject when the reports are clean.

Send from every project for $0.

Open source. One click puts it on your Cloudflare account.