Most SPF records are simple lists: a few ip4: ranges, an include: for each provider and a final -all. Occasionally you meet a record that contains strange expressions such as exists:%{i}._spf.%{d}. These are SPF macros, a part of the specification that lets a record change its DNS queries depending on the connection being checked. Macros make it possible to build dynamic authorisation systems, such as allowing only IP addresses that appear in a DNS zone you update, but they also add DNS lookups, complexity and risk. This guide explains how macros work, the letters and transformers, readable examples, practical use cases, security considerations and testing. It builds on SPF syntax, the 10-lookup limit and SPF flattening.
Macro syntax
A macro begins with %{, contains a letter, optional transformers and a delimiter list, and ends with }. Three short escapes also exist: %% for a literal percent sign, %_ for a space and %- for a URL-encoded space. Macros can appear only in the domain-spec part of mechanisms and modifiers (for example after include:, exists:, a:, mx:, ptr:, in redirect= and exp=), not in the ip4 or ip6 mechanisms.
| Letter | Expands to | Example for client 203.0.113.25 sending from [email protected] via host mail.example.net |
|---|---|---|
| %{s} | The full sender (envelope From) | [email protected] |
| %{l} | Local part of the sender | user |
| %{o} | Domain part of the sender | example.com |
| %{d} | The domain currently being checked | example.com |
| %{i} | The IP address of the connecting client; IPv6 is expanded to dotted nibbles | 203.0.113.25 |
| %{p} | Validated domain name of the client (discouraged, slow) | mail.example.net |
| %{v} | The string "in-addr" for IPv4 or "ip6" for IPv6 | in-addr |
| %{h} | The HELO/EHLO domain | mail.example.net |
| %{c}, %{r}, %{t} | Only valid in the exp (explanation) text: readable IP, receiving host, timestamp | Used in rejection messages |
Transformers
| Part | Effect | Example |
|---|---|---|
| A number | Keep only that many right-hand parts of the result after splitting | %{d2} on mail.example.com gives example.com |
| The letter r | Reverse the order of the parts | %{ir} turns 203.0.113.25 into 25.113.0.203 |
| Delimiters (. - + , / _ =) | Characters to split on; the default is a dot | %{l-} splits the local part at hyphens |
| Uppercase letter | URL-escape the result | %{S} gives user%40example.com |
With the reverse transformer, %{ir}.%{v}._spf.%{d} expands for our example client to 25.113.0.203.in-addr._spf.example.com. This is the pattern used for DNS-based allow lists: if that name exists, SPF treats the client as authorised.
example.com. TXT "v=spf1 exists:%{ir}.%{v}._spf.%{d} -all"
; To authorise 203.0.113.25, publish an A record at the expanded name
25.113.0.203.in-addr._spf.example.com. A 127.0.0.2How exists works with macros
The exists mechanism performs an A lookup (not an AAAA lookup, even for IPv6 clients) and matches if any address is returned; the value itself is ignored. By inserting macros you make the lookup depend on the client, the sender or the HELO name, so authorisation becomes a database in DNS. Practical uses:
| Pattern | Purpose | Trade-off |
|---|---|---|
exists:%{ir}.%{v}._spf.%{d} | Per-IP allow list managed by adding or removing DNS records | Many small records; instant change; one lookup |
exists:%{l}._u.%{d} | Allow only specific users (local parts) to send | Management overhead; leaks which users exist |
include:%{d}._spf.vendor.example | Vendor-hosted record that varies by customer domain | Requires trust in the vendor and extra lookups |
exists:%{i}._rbl.local.example with a deny qualifier | Block listed IPs inside SPF | Rare; better handled by filters |
redirect=_spf.%{d2} | Share a common record across subdomains | Neat for many subdomains of one organisation |
exp=explain.%{d} | Show a custom explanation to rejected senders | Cosmetic; some clients display the text |
Why most domains do not need macros
- Standard records with
includeandip4are simpler to read, audit and debug. - Macros hide the real set of allowed senders in DNS data: the record shows a pattern, not the list.
- Every macro-based mechanism uses a DNS lookup; the SPF limit of 10 mechanisms that cause lookups still applies, and exceeding it gives a permanent error. See SPF lookup limit.
- Several mechanisms (
a,mx,ptr,exists,include,redirect) count;ip4,ip6andalldo not. - Some validators and monitoring tools do not expand macros and report incomplete results, which makes audits harder.
- The
ptrmechanism and the %{p} macro are discouraged by the standard because they are slow and unreliable.
Security and abuse considerations
- Information leakage. Macro-based lookups send the client IP, sender or HELO name to your DNS servers (and to third-party DNS servers for includes). Treat these logs as data containing personal information in some regimes.
- Amplification. A hostile sender can cause your DNS to answer many lookups by sending many messages with chosen HELO or sender values. The lookup limit bounds the damage, but monitor query volume.
- Wildcards. A wildcard A record in the zone used by
existswould match everything and authorise every sender. Never put wildcard records under that zone. - Stale entries. Per-IP records must be removed when an IP changes hands. Review them regularly.
- Vendor trust. If an
includeuses macros that resolve inside a vendor zone, the vendor controls who is authorised for your domain. - Do not mix with uncontrolled input. Sender local parts and HELO names are attacker-controlled; avoid using them to build names that could point at unexpected zones.
A worked example
Imagine a message arrives from IP 198.51.100.7 with the envelope sender [email protected] and HELO name out7.example.com. The receiver fetches the SPF record of example.com and finds v=spf1 ip4:203.0.113.0/24 exists:%{ir}.%{v}._spf.%{d} -all. The IP is not in the 203.0.113.0/24 range, so evaluation moves on to the exists mechanism. The receiver expands the macro to 7.100.51.198.in-addr._spf.example.com and asks for its A record. If the administrator has published a record there, the answer arrives, the mechanism matches and the result is pass. If no record exists, the lookup returns no data, the mechanism does not match and the final -all produces a fail. The same domain can authorise a new server within minutes by creating one DNS record, without editing the TXT record that tools and humans read, which is exactly why large organisations sometimes prefer the pattern. The cost is that the TXT record alone no longer tells you who may send, so documentation and an export of the underlying zone become part of your audit evidence.
Reading a macro record
- Write down the connection facts for a sample message: IP, envelope sender, HELO name.
- Expand each macro by hand using the tables above.
- Query the resulting names with dig and see whether they exist.
- Follow includes and redirects, repeating the expansion at each level.
- Count the lookups. If the total reaches ten or more, simplify.
- Test with an SPF tester that supports macros and with a real message, reading the received result. See authentication results.
dig +short A 25.113.0.203.in-addr._spf.example.com
dig +short TXT example.com
# Check the SPF verdict a receiver recorded:
# Received-SPF: pass (... designates 203.0.113.25 as permitted sender)Recommendation
Use macros only when you have a clear need that plain records cannot meet: a large, frequently changing set of authorised senders managed by automation, or a platform that issues SPF for many customer domains. Keep the macro patterns few and documented, protect the zone they query, monitor query volume and test every change with real messages. For everyone else, a well-organised normal record with a minimal set of includes is clearer and safer. For alignment issues related to the envelope sender, see envelope From.
Check your SPF record See lookups, syntax and results for your domain.
References and further reading
Frequently asked questions
What is an SPF macro?
A placeholder such as %{i} or %{d} that is replaced by facts about the current connection before the DNS lookup is made.
What does %{ir}.%{v}._spf.%{d} expand to?
For IPv4 client 203.0.113.25 and domain example.com it expands to 25.113.0.203.in-addr._spf.example.com.
Do macros count toward the 10-lookup limit?
Yes. Mechanisms that use macros, such as exists and include, each count as a DNS lookup.
Are SPF macros safe to use?
They can be, but they leak connection data to DNS and hide the allowed senders, so use them only when plain records cannot do the job.
How do I test a macro-based record?
Expand the macros by hand, query the resulting names with dig and use a macro-aware SPF tester plus a real message.