Guide · Authentication: SPF, DKIM, DMARC
SPF macros explained: what %{d}, %{o}, %{i} and %{ir} expand to
An SPF macro is a placeholder in a published SPF record. The receiving server fills it in while it checks whether the connecting server may send for a domain. %{i} becomes the connecting IP address, and %{d} the domain whose SPF policy is being evaluated at that moment, so one record can look up a different DNS name for each connection. Most domains never need to write a macro. If your provider gave you one, knowing what it expands to helps you troubleshoot it without changing who may send your mail, and shows you what it costs in DNS lookups and privacy.
- Reading time
- 6 min
How the syntax works
A macro starts with %{, holds one letter and optional transformers, and ends with } (RFC 7208, section 7.1). The letter picks a value. A number keeps that many parts from the right, and r reverses the parts first (section 7.3). Parts are split on dots by default; you can name other delimiters from the list . - + , / _ =, but the parts are always joined again with dots.
| Macro | Input | Result |
|---|---|---|
%{d} | domain mail.team.example.com | mail.team.example.com |
%{d2} | the same | example.com |
%{d2r} | the same | team.mail: reversed to com.example.team.mail, then the last two parts |
%{l} | sender qa-team@mail.team.example.com | qa-team |
%{lr-} | the same | team.qa: split on the hyphen, reversed, joined with a dot |
RFC 7208's own examples (section 7.4) use strong-bad@email.example.com: %{d2r} gives example.email and %{lr-} gives bad.strong. Three escapes stand for literal text: %% for a percent sign, %_ for a space and %- for an encoded space. A % followed by anything else, for example %(ir) with round brackets, is a syntax error, and the check returns permerror (section 7.3).
%{d} vs. %{o}: the policy domain can change
Take an envelope sender of qa-team@mail.example.com and a connecting IPv4 address of 192.0.2.1. The receiver starts at mail.example.com, so %{d} and %{o} (the domain of the sender, section 7.2) both expand to mail.example.com.
Say that policy contains include:spf-policy.example.net and the receiver reaches it. While it evaluates the included policy, %{d} expands to spf-policy.example.net: an include runs the check again with the new domain, while the IP address and the sender stay the same (section 5.2). %{o} still gives mail.example.com. A redirect works the same way (section 6.1). Back in the original policy after an include that did not match, %{d} is again mail.example.com.
This matters when a provider's documentation says a lookup is "keyed to the sending domain". Check whether that means the envelope sender's domain or the domain that serves the policy: putting %{d} where %{o} belongs sends the receiver to a different DNS name with no syntax error. Neither macro is the visible From header. SPF checks the MAIL FROM identity and the HELO identity (section 2.2), and a message with an empty envelope sender is checked as postmaster@ its HELO name (section 2.4) (envelope sender vs. From).
%{i} vs. %{ir}: order matters
For 192.0.2.1, %{i} gives 192.0.2.1 and %{ir} gives 1.2.0.192. The r reverses the dot-separated parts, not the characters inside them. For IPv6, %{i} gives a dotted form with one hexadecimal digit per part instead of the usual colon notation; it is meant for use as %{ir} (section 7.3).
Say the included policy at spf-policy.example.net contains the illustrative term exists:%{ir}.%{d}.auth.example.net. For the example above, the lookup name is 1.2.0.192.spf-policy.example.net.auth.example.net. With %{o} instead it would be 1.2.0.192.mail.example.com.auth.example.net. These names are worked out by hand; they do not resolve.
The exists mechanism looks up an A record at the expanded name, even when the connection is IPv6, and matches if any A record comes back (section 5.7). The address in that answer does not have to be the connecting IP, unlike the comparison the a mechanism makes. Expanding a macro neither authorizes nor rejects anything: the mechanism and its qualifier decide that.
Do macros count toward the ten-lookup limit?
Expanding %{d}, %{o} or %{ir} is text work, not a DNS query. The term around it still counts if it is one of include, a, mx, ptr or exists, or the redirect modifier, and all of them share one limit of 10 for the whole evaluation; going over returns permerror (section 4.6.4). An exists inside an included policy uses the same budget as the include. A receiver can stop at an earlier match, so the number of terms evaluated can differ from one sender or IP address to the next.
%{p} is different: it looks up the validated host name of the connecting IP with reverse and forward queries, and the PTR records it queries count toward the same limit of 10 (section 4.6.4). RFC 7208 says %{p} should not be published (section 7.3). Lookups that return no records or a nonexistent name have their own limit: implementations should stop after two of these "void lookups" (section 4.6.4). A short record or a spreadsheet count does not prove every path stays within the limits (the SPF lookup limit).
The DNS cost and amplification risk
Macros make lookups vary by message, and that has a price. RFC 7208 (section 7.3) says sending domains had better avoid the s, l, o and h macros in mechanisms: they severely limit caching of SPF results and reduce the effectiveness of DNS caches. Without them, a receiver can cache a result on the domain and IP address alone.
The lookup limits exist to stop abuse. RFC 7208 (section 11.1) describes how an attacker can publish SPF records that make many receivers send DNS queries at a victim's domain, using the receivers to amplify the attack. Macros also let a sender inject arbitrary text into the receiver's DNS queries, so receivers must be prepared for hostile content (section 11.5.3). If you run a receiver, keep its limits on. If you publish macros, keep each path short.
What macro lookups can reveal
The expanded name is visible to the DNS servers that handle the query. RFC 7208 (section 11.6) warns that SPF queries, especially those caused by exists, can reveal who is sending mail and probably to which server. An exists term with %{l} puts the envelope sender's local part, the text before the @, into a DNS query; %{s} puts in the whole envelope sender. With a provider-owned suffix, that data goes to the provider or its DNS operators. Even a name built from the IP address alone says something about the connection.
Before you keep a provider's macro, expand it for a made-up message, find out who controls the DNS zone it points to, and ask whether the same decision can be made without data about individual senders. Check both a matching path and a path that falls through when you count lookups. Prefer the provider's plain documented record when it covers the same senders. Never copy a sample policy from an article into production without confirming which servers it would authorize.
Questions
What is the difference between %{d} and %{o}?
%{o} is the domain of the sender being checked: usually the envelope sender's domain, and the HELO name for a HELO check or an empty envelope sender (RFC 7208, sections 2.3, 2.4 and 7.2). %{d} is the domain whose policy is being evaluated, so it changes inside an include or a redirect (section 5.2).
Do SPF macros use up DNS lookups?
Expanding one does not. The mechanism it sits in does, if it is one that queries DNS, and %{p} adds PTR lookups to the count (section 4.6.4).
Are SPF macros a privacy risk?
They can be. An exists term with %{l} or %{s} puts parts of the sender's address into DNS queries that other parties see (section 11.6).
Try it
Check a domain's SPF record and its lookups with the SPF checker, or check an address with the free email checker.
Related
Related guides
- The SPF 10-lookup limit explained
- SMTP envelope sender vs. From header: which address goes where?
- Moving DMARC from p=none to p=reject
- DKIM canonicalization: what simple and relaxed really change
- Negative DNS caching: why a new MX record can still look missing
- All guides
Sources
- IETF: RFC 7208, Sender Policy Framework (SPF): sections 2.2 to 2.4 (identities), 4.6.4 (DNS lookup limits), 5.2 (include), 5.7 (exists), 6.1 (redirect), 7.1 to 7.4 (macros), 11.1 (processing limits and amplification), 11.5.3 (macro expansion), 11.6 (privacy exposure).
How we wrote this: drafted by the email-validation.org team with the help of AI writing tools, from our product documentation and the sources linked in the text.