SPF Macros Explained: exists:%{i}, %{ir}, %{d}, and Why Naive Checkers Falsely Error
Decode RFC 7208 §7 dynamic SPF macros (%{i}, %{ir}, %{s}, %{d}, %{h}) used by Salesforce, Proofpoint, and Mimecast, and prevent false NXDOMAIN void-lookup errors.
Incident Symptom & RFC 7208 Root Cause
SMTP & Deliverability Symptom
After adding Salesforce (include:_spf.salesforce.com) or enterprise hosted SPF using exists:%{i}._spf.mta.example.com, simple web SPF flatteners crash or claim your SPF record is broken with NXDOMAIN void lookups—yet live Gmail headers show spf=pass!
DNS Resolver & RFC 7208 Mechanics
RFC 7208 §7 lets SPF records interpolate runtime SMTP connection variables—such as %{i} (connecting client IP), %{ir} (reversed client IP octets), %{d} (current domain), and %{s} (MAIL FROM sender)—into DNS query names at verification time! Any static tool that queries literal '%{i}...' over DNS without macro awareness gets NXDOMAIN and cannot statically flatten the infinite per-IP namespace.
Broken vs Verified DNS Configuration
; Vendor record (_spf.salesforce.com) uses per-IP exists macro:
; "v=spf1 exists:%{i}._spf.mta.salesforce.com -all"
; BROKEN: Running a naive static IP-flattening script drops exists:%{i}... completely
; because no static CIDR list is returned by a non-macro TXT crawl -> all Salesforce mail fails!
example.com. 3600 IN TXT "v=spf1 ip4:192.0.2.0/24 -all"; Keep macro-powered vendor includes unflattened (costs only +1 lookup for exists:%{i}!):
example.com. 3600 IN TXT "v=spf1 include:_spf.google.com include:_spf.salesforce.com -all"
; Verify manually from terminal by expanding %{i} to a real sender IP (e.g. 13.110.54.12):
; dig +short A 13.110.54.12._spf.mta.salesforce.com
; -> Returns 127.0.0.2 (any A record match = exists: PASS, regardless of IPv4 value returned!)Protocol Mechanics & Operational Deep Dive
Look at how clever RFC 7208 §5.7 'exists:' combined with %{i} actually is: instead of publishing 40 nested netblock TXT records that burn 6 DNS lookups, an enterprise sender like Salesforce or an authoritative hosted-SPF server publishes a single mechanism: exists:%{i}._spf.mta.vendor.com. When an email arrives from IP 13.110.54.12, the receiver expands %{i} and performs ONE single DNS Type-A query for 13.110.54.12._spf.mta.vendor.com! If ANY Type-A record is returned (conventionally 127.0.0.2, just like DNSBLs), exists: matches immediately at a cost of 1 mechanism lookup (or 2 including the parent include:)!
Understanding macro transformers in RFC 7208 §7.1 makes reading complex enterprise SPF records effortless: %{i} expands to dotted IPv4 (192.0.2.1) or nibble-dotted IPv6; adding 'r' (%{ir}) reverses dot-delimited labels (1.2.0.192, mirroring in-addr.arpa hierarchy); adding a trailing integer like %{d2} keeps only the right-most 2 labels of the domain (turning sub.mail.example.com into example.com); %{v} expands to 'in-addr' for IPv4 or 'ip6' for IPv6; and %{h} expands to the SMTP HELO/EHLO hostname.
Watch out for two real risks when writing custom SPF macros yourself: (1) if an authorized sender IP fails to match exists:%{i}..., that NXDOMAIN response DOES count as +1 toward RFC 7208's 2-void-lookup limit—meaning stacking three separate macro-based ESPs in sequence will hit 3 NXDOMAIN void lookups -> PermError whenever an email originates from the 4th mechanism in your list! Place direct ip4:/ip6: and standard CIDR includes BEFORE exists:%{i} macro includes so common corporate mail matches before firing dynamic NXDOMAIN queries! (2) Never pass %{s} / %{l} (local-part before @) to untrusted third-party DNS servers if internal email local-parts leak sensitive addresses via DNS queries.
RFC 7208 §7 SPF Macro Variables Reference
| Mechanism / Case | RFC 7208 Evaluation | Production Remediation |
|---|---|---|
| %{i} and %{ir} (Client IP / Reversed Client IP) | 198.51.100.7 -> %{i} = '198.51.100.7', %{ir} = '7.100.51.198'. | Test in OpsLint SPF Workbench with simulatedIp set, or substitute a real IP in dig A. |
| %{d} (Current Domain) and %{o} (Sender Domain) | Expands to domain currently being evaluated (%{d}) or MAIL FROM domain (%{o}); %{d2} = last 2 labels. | Allows shared multi-tenant SPF zones (_spf.%{d}) across hundreds of brand domains. |
| %{v} (IP Version Tag) | Expands to 'in-addr' for IPv4 connections and 'ip6' for IPv6 connections. | Commonly combined as %{ir}.%{v}._spf.%{d} in hosted DNS zones. |
| Stacking >=3 exists:%{i} macro vendors | Each non-matching exists:%{i} returns NXDOMAIN (+1 void lookup); 3 misses in a row -> PermError! | Put static CIDR includes (Google/M365) BEFORE exists:%{i} includes, or split onto subdomains! |
Edge Cases & Engineering FAQs
Why did putting Salesforce FIRST in our SPF record cause M365 mail at position #4 to fail with void-lookup PermError?
Brilliant real-world catch! When outbound M365 mail leaves Microsoft's IPs, checking exists:%{i}._spf.mta.salesforce.com FIRST returns NXDOMAIN (Void Lookup #1). If positions #2 and #3 ALSO use exists:%{i} macros or have a NODATA 'a' record, Void Lookup #3 triggers PermError BEFORE reaching include:spf.protection.outlook.com at position #4! Always order standard CIDR includes before exists:%{i} macro includes.
Does exists: inspect the IPv4 address returned in the DNS A answer?
No! Per RFC 7208 §5.7, exists:<domain-spec> queries DNS Type A ONLY (even when the SMTP connection itself is over IPv6!) and matches if ANY A record is returned (127.0.0.2, 127.0.0.1, etc.).
Are literal percent signs allowed in an SPF record outside macros?
No! In RFC 7208 §7.1, a bare '%' not followed by '{...}', '%-' (URL-encoded space), '%_' (space), or '%%' (literal %) is an immediate syntax PermError.
Aligned Transactional Delivery Infrastructure
Dedicated transactional delivery streams with custom Return-Path subdomain SPF alignment and 2048-bit DKIM signing.