SPF flattening: what can go wrong, and how to do it safely
Flattening means replacing include: mechanisms with the ip4: and ip6: ranges they currently resolve to. IP mechanisms cost no DNS lookups, so a flattened record easily fits under the limit of 10.
The risks
- Stale IPs. Providers change their sending ranges without telling you. A static flattened record keeps the old list, and mail from the new IPs fails SPF. This is the main risk.
- Length. A single TXT string is limited to 255 characters. Longer records must be split into several strings, and a very long record can push DNS responses past 512 bytes. Splitting into chained records (
include:_spf1.yourdomain.com, ...) avoids that, and costs one lookup per chained record. - Macros and
exists:. Some providers (Salesforce, for example) useexists:%{i}.... These can't be flattened and have to stay as they are. - Qualifiers. Only pass (
+) mechanisms should be flattened. Keep-include:or~include:as-is.
Doing it safely
- Run
node spf-check.mjs yourdomain.com --flattenwith the free checker to get a preview, split automatically if it's long. - Keep a copy of your original record.
- Re-check on a schedule (daily at least) and update the record when the resolved ranges change. Open-source flatteners such as cfspflat and sender_policy_flattener can do this on a cron job you run yourself.
No command line handy? Use the online SPF lookup checker: same checks, in the browser.