New guide: how to warm up a dedicated IP without hurting your reputation. Read now →
Log in
MigoSMTP
Domains and DNSUpdated 26 Oct 2026

SPF Macros Explained: %{i}, %{d}, %{s} and When Dynamic SPF Records Make Sense

What SPF macros are, the letters and transformers, how to read macro-based records, practical uses such as per-sender include names and exists checks, DNS lookup cost, security considerations and testing.

CB
Written by Chloe Bennett
Customer Success Manager, Migrations
Reviewed by Jonas Lindqvist
  • Published17 Feb 2025
  • Read time6 min
  • Length1,293 words
Share this guide

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.

IdeaA macro is a placeholder replaced by facts about the current SMTP connection before the DNS query is made
Common letters%{i} sender IP, %{d} domain, %{s} sender address, %{h} HELO name
Typical useexists: lookups that make SPF depend on a per-IP DNS entry
CautionEach macro-based mechanism costs a DNS lookup and counts toward the limit of 10

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.

LetterExpands toExample 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 senderuser
%{o}Domain part of the senderexample.com
%{d}The domain currently being checkedexample.com
%{i}The IP address of the connecting client; IPv6 is expanded to dotted nibbles203.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 IPv6in-addr
%{h}The HELO/EHLO domainmail.example.net
%{c}, %{r}, %{t}Only valid in the exp (explanation) text: readable IP, receiving host, timestampUsed in rejection messages

Transformers

PartEffectExample
A numberKeep only that many right-hand parts of the result after splitting%{d2} on mail.example.com gives example.com
The letter rReverse 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 letterURL-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.2

How 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:

PatternPurposeTrade-off
exists:%{ir}.%{v}._spf.%{d}Per-IP allow list managed by adding or removing DNS recordsMany small records; instant change; one lookup
exists:%{l}._u.%{d}Allow only specific users (local parts) to sendManagement overhead; leaks which users exist
include:%{d}._spf.vendor.exampleVendor-hosted record that varies by customer domainRequires trust in the vendor and extra lookups
exists:%{i}._rbl.local.example with a deny qualifierBlock listed IPs inside SPFRare; better handled by filters
redirect=_spf.%{d2}Share a common record across subdomainsNeat for many subdomains of one organisation
exp=explain.%{d}Show a custom explanation to rejected sendersCosmetic; some clients display the text

Why most domains do not need macros

  • Standard records with include and ip4 are 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, ip6 and all do not.
  • Some validators and monitoring tools do not expand macros and report incomplete results, which makes audits harder.
  • The ptr mechanism and the %{p} macro are discouraged by the standard because they are slow and unreliable.

Security and abuse considerations

  1. 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.
  2. 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.
  3. Wildcards. A wildcard A record in the zone used by exists would match everything and authorise every sender. Never put wildcard records under that zone.
  4. Stale entries. Per-IP records must be removed when an IP changes hands. Review them regularly.
  5. Vendor trust. If an include uses macros that resolve inside a vendor zone, the vendor controls who is authorised for your domain.
  6. 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

  1. Write down the connection facts for a sample message: IP, envelope sender, HELO name.
  2. Expand each macro by hand using the tables above.
  3. Query the resulting names with dig and see whether they exist.
  4. Follow includes and redirects, repeating the expansion at each level.
  5. Count the lookups. If the total reaches ten or more, simplify.
  6. 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.

Open the checker

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.

Topics#spf#macros#dns#authentication#lookups
Was this guide helpful?Your answer helps us improve our documentation.

Start sending in minutes.

Create an account, add your domain and send your first email.

Get started Talk to sales