SPF lets a domain list the servers that may send mail on its behalf. The record is a single TXT entry, and every time it contains an include, a, mx, exists or redirect, the receiver has to make another DNS query to resolve it. The standard limits this to ten lookups. A domain that uses a mailbox provider, a marketing platform, a CRM, a help desk and a transactional service can reach the limit quickly, and when it does, SPF returns permerror and fails for every message. Flattening is the technique of replacing those lookups with the IP addresses they resolve to. It works, and it is also one of the easiest ways to break your own authentication later. This guide explains the limit, how flattening works, what can go wrong and which alternatives should come first. It complements our guides to fixing SPF permerror and SPF with several senders.
What counts toward the limit
| Mechanism | Counts as a lookup? | Notes |
|---|---|---|
| include: | Yes, plus any lookups inside the included record | The usual culprit: nested includes add up |
| a, mx | Yes (mx also resolves each host) | Avoid mx if you can; it can cost up to ten more |
| exists:, ptr | Yes | ptr is discouraged |
| redirect= | Yes | Replaces the record |
| ip4:, ip6:, all | No | No DNS query needed |
| Void lookups | Counted separately; more than two empty results is an error | Dead includes cause trouble |
You can count lookups with the SPF checker. Remember that it counts recursively: a single include:_spf.vendor.example may use three or four lookups by itself.
How flattening works
To flatten, you resolve each include to its list of IP ranges and write those ranges directly into your own record as ip4: and ip6: terms. Since these terms cost nothing, the lookup count drops. For example:
; before: 3 includes, about 9 lookups in total
example.com. TXT "v=spf1 include:_spf.google.com include:sendgrid.net include:mail.vendor.example ~all"
; after flattening: no lookups, but a static list of ranges
example.com. TXT "v=spf1 ip4:203.0.113.0/24 ip4:198.51.100.10 ip4:198.51.100.11 ip6:2001:db8::/32 ~all"The result is a valid record with zero lookups. Done once by hand it is simple. The trouble starts the day after.
Why static flattening is risky
- Vendors change IPs without telling you. Large senders add and retire ranges regularly. The include is their promise that it will always be correct; your copy is a snapshot. When the vendor starts sending from a new range, your mail fails SPF and nobody notices until delivery drops.
- Record size. A TXT record is limited to 255 characters per string and, in practice, about 450 bytes if you want it to fit in a UDP response; longer records are split into several strings or fail on some resolvers. Many vendors publish dozens of ranges. See TXT record length and registrar quirks.
- Accidental authorisation. If you copy ranges that belong to a shared platform, you authorise every customer on that platform to send as your domain as far as SPF is concerned.
- Lost visibility. A reader of the record no longer sees which vendors you use.
- Mistakes on update. Manual editing of long lists invites typos, duplicates and syntax errors that give a permerror of a different kind.
Because SPF is only one of the checks DMARC accepts, a wrong flattened record may not stop delivery if DKIM passes and aligns. That is precisely why the failure can go unnoticed for weeks. Read DMARC alignment.
Do these things before flattening
- Remove what you do not use. Old vendors, trial accounts and migrated services often remain in the record. Remove their includes after confirming in DMARC reports that they no longer send. See DMARC aggregate reports.
- Use DKIM-first vendors. If a vendor can sign with your domain and use its own return-path domain, SPF for your main domain is not needed for that stream. Set up a custom return-path as described in custom return-path and tracking domain.
- Split by subdomain. Give each service its own subdomain, for example
news.example.comfor marketing andbilling.example.comfor invoices, each with a short SPF record. The ten-lookup limit applies to each name separately. See sending subdomain strategy. - Replace mx and a with ip4. If the addresses are stable and yours, listing them directly costs nothing.
- Ask the vendor for a shorter include. Some vendors offer region-specific or reduced includes.
If you must flatten: do it safely
- Flatten only the unstable middle. Keep one or two includes for large providers whose ranges change often, and flatten only vendors with small, rarely changing ranges.
- Automate and monitor. Use a managed flattening service or a scheduled job that resolves includes, builds the record and publishes it through your DNS provider's API. Alert on changes and on lookup counts above eight.
- Use a low TTL for the record so that corrections propagate quickly.
- Compare before and after. Resolve both versions and check that every address authorised before is still authorised.
- Watch DMARC reports. A rise in SPF failures from a known vendor is the first sign that a range was missed.
- Document it. Keep a list of which vendor each range belongs to so that people can audit the record later.
Some managed flattening works by publishing a record on a name the service controls and pointing an include at it. That moves the update work to the service, but it also adds a dependency: if the service fails, your SPF fails. Choose a provider with a clear record of reliability and keep a fallback plan.
Record size and format
A flattened record can approach or exceed the length that a single DNS response comfortably carries. Keep the full record below roughly 450 characters if you can, and never rely on DNS providers to split strings correctly without checking. Test with several resolvers, including those used by large mailbox providers. Make sure the mechanism order is right: put the most common senders first, and end with ~all or -all once you are confident that the list is complete. See SPF record syntax and softfail vs fail.
Testing after a change
After any change to SPF, send test messages from every system that uses the domain, not only the main one. Look at the Authentication-Results header in each delivered message and confirm that SPF shows pass, then check that DMARC still passes through SPF or DKIM. Schedule a recurring review, for example each quarter, so that new vendors are added deliberately and departed vendors are removed before the record grows again.
A decision guide
| Situation | Recommended approach |
|---|---|
| Eleven or twelve lookups because of one old vendor | Remove the unused include |
| Several marketing and transactional vendors | Subdomains per stream plus DKIM alignment |
| Vendor with a tiny, stable IP range | Flatten that vendor only |
| Many vendors with changing ranges and no way to split | Managed dynamic flattening with monitoring |
| Parked domain that sends no mail | Publish v=spf1 -all; see parked domains |
Flattening solves a real problem, but it trades a failure that is loud and immediate (permerror) for one that is quiet and delayed (stale ranges). Reduce the number of senders on the main domain first, use subdomains and DKIM alignment, and flatten only with automation and monitoring.
Authenticate each stream cleanly Use your own DKIM and a custom return-path so SPF on the main domain stays short.
References and further reading
Frequently asked questions
What is SPF flattening?
Replacing include and similar terms with the IP ranges they resolve to, so that the record needs fewer DNS lookups.
Why is there a limit of ten lookups?
The standard caps DNS queries per evaluation to protect receivers from abuse and slow responses. Exceeding it gives a permerror.
Is flattening safe?
Only with automation and monitoring. A static list goes stale when a vendor changes its IP ranges.
What should I try before flattening?
Remove unused includes, use subdomains for different services and sign with DKIM aligned to your domain.
How long can an SPF record be?
Each TXT string is limited to 255 characters, and a total near 450 bytes is the practical limit for reliable delivery over UDP.