SPF on Cloudflare is a single TXT record at the apex that lists every source allowed to send mail as your domain — and the two ways it goes wrong on Cloudflare specifically are both self-inflicted. First: the a and mx mechanisms resolve hostnames, and if those hostnames are proxied they resolve to Cloudflare edge IPs, so you end up authorizing Cloudflare’s proxy instead of your server. Second: every include costs a DNS lookup against a hard budget of 10, and stacking providers blows through it, turning the whole record into a permerror. The safe pattern is ip4: for your own server, include: for each real provider, nothing speculative.
What’s happening
A receiving mail server takes the envelope sender’s domain, fetches the TXT record starting with v=spf1, and evaluates mechanisms left to right until one matches the connecting IP. Mechanisms that trigger extra DNS queries — include, a, mx, ptr, exists, and the redirect modifier — count against a limit of 10 lookups per check, set by RFC 7208. Exceed it and evaluation aborts with permerror; most receivers then treat the mail as unauthenticated, and a DMARC policy can push it to spam or rejection outright. Nested includes count too: include:_spf.google.com pulls in three more _netblocks lookups, so Google alone spends 4 of your 10.
The Cloudflare-specific trap is the proxy. On a zone where example.com is orange-clouded, the mechanism a (meaning “the IP behind example.com”) evaluates to Cloudflare’s anycast IPs — which will never be the IP your mail actually leaves from. The record looks reasonable and passes syntax checkers, yet every real message fails SPF. The fix is to never make SPF depend on a proxied name: write the sending server’s true IP with ip4: / ip6: (those cost zero lookups), and keep a / mx only for grey-cloud hostnames, if at all.
What to check first
- How many SPF records exist. More than one TXT starting with
v=spf1on the same name is an automaticpermerror. Merge them. - What your real sending sources are. The web server (contact forms, app mail), the mail server, and each provider — Microsoft 365, Google Workspace, SES, SendGrid, a newsletter tool.
- Whether any
a/mxmechanism points at a proxied hostname. If yes, it authorizes the wrong IPs. - Your current lookup count. Count includes plus their nested includes before adding anything new.
How to verify
# The record as the world sees it
dig +short TXT example.com | grep spf1
dig @1.1.1.1 +short TXT example.com
# Walk the includes to count lookups (Google spends 4 total)
dig +short TXT _spf.google.com
dig +short TXT _netblocks.google.com
dig +short TXT _netblocks2.google.com
dig +short TXT _netblocks3.google.com
Then prove a real message passes. Send one from the server and read the receiver’s verdict:
# Test delivery from the sending host to a mailbox you control
swaks --to you@your-test-mailbox.net --from app@example.com --server 127.0.0.1
# On the receiving side, the header tells the truth:
# Authentication-Results: ...; spf=pass smtp.mailfrom=example.com
# Outbound source IP the world sees — must appear in the record
curl -s https://api.ipify.org
If you run the mail server, grep spf /var/log/exim_mainlog (or your MTA’s log) shows inbound verdicts; for your own outbound, the Authentication-Results header at the destination is the reference.
The fix
-
Inventory every legitimate sender. App servers, the mail host, and each SaaS provider that sends as
@example.com. Anything not on the list does not go in the record. -
Write the record with
ip4:for machines you own. Zero lookups, immune to proxy status:v=spf1 ip4:203.0.113.25 include:_spf.google.com include:sendgrid.net ~allThat example spends 5 of 10 lookups (4 for Google’s nest, 1 for SendGrid) and authorizes the origin directly.
-
Publish it in Cloudflare as one TXT record on the apex name (
example.com), TTL Auto, proxy status not applicable — TXT records are always DNS-only. Delete any olderv=spf1record in the same save. -
Pick the failure qualifier deliberately.
~all(softfail) while you are still confirming sources;-allonce the inventory is proven. Never+all, which authorizes the entire internet. -
Cover subdomains that send. SPF does not inherit downward:
bounce.example.comormail.example.comeach need their own TXT record if mail uses them as envelope sender. -
Re-verify with
dig @1.1.1.1and one test message per sending source, checkingspf=passat the destination.
When this fix is wrong
If mail lands in spam despite spf=pass, SPF is not the lever — look at DKIM, DMARC alignment, and sender reputation. And if you are near the lookup ceiling because the business genuinely uses many providers, the answer is flattening or trimming, not creative nesting — the approach is covered in fixing the SPF 10-lookup limit.
Common mistakes
- Two
v=spf1records after a provider’s wizard “added” theirs next to yours. aormxmechanisms referencing orange-clouded hostnames — Cloudflare IPs get authorized, your server does not.ptrmechanisms: deprecated, slow, and a wasted lookup.- Copying a provider’s include without removing the ones you stopped using — dead includes still burn lookups.
- Putting the record on
wwwor a random name instead of the apex, or expecting the apex record to cover sending subdomains.
Prevention
Treat the SPF record as configuration under change control: one owner, a comment trail of why each mechanism exists, and a lookup count checked before every addition. Re-run the dig battery after any provider onboarding, and keep the record’s logic documented next to your zone export. Cloudflare’s DNS documentation covers TXT handling; the wider SPF/DKIM/DMARC trio is mapped in the DNS record cookbook.
Mail that breaks after a DNS move is one of the most common incidents we untangle — Stack Harbor manages the full DNS and mail path under Managed Cloud Ops; book an infrastructure review before the next migration.