# 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.

- **Canonical URL**: https://opslint.dev/spf/spf-macros-exists-percent-i-enterprise-senders/
- **Category**: `SPF`
- **Interactive Workbench**: [Open in Live Workbench](https://opslint.dev/spf/#domain=cloudflare.com&ip=198.51.100.42)
- **Normative Specifications**: `RFC 7208 §7 (Macros: %{s}, %{l}, %{o}, %{d}, %{i}, %{v}, %{h}, Transformers 'r' & Digits)`, `RFC 7208 §5.7 ('exists' Mechanism)`

## Incident Symptom & Root Cause

**Production 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!

**Protocol / Engine Root Cause**: 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.

## Remediation Diff (Broken vs Verified Fix)

### Attempting to 'Static Flatten' a Macro-Based SPF Include (Impossible & Destructive!)

```text
; 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"
```

### Preserve Dynamic Macro Includes Intact & Test with Simulated Sender IP

```text
; 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!)
```

## Engineering 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

| Case / Engine | Protocol Behavior | Production Remediation |
| :--- | :--- | :--- |
| **%{i} and %{ir} (Client IP / Reversed Client IP)** (`#macro-percent-i-ir`) | 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)** (`#macro-percent-d-o`) | 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)** (`#macro-percent-v`) | 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** (`#macro-void-ordering`) | 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! |

## Frequently Asked Engineering Questions

### 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.

## Related Problem-Cluster Guides

- [Fix SPF Void Lookups Limit Exceeded (>2 NXDOMAIN or Empty DNS Answers)](https://opslint.dev/spf/void-lookups-limit-2-nxdomain-permerror/) — Why dead SaaS includes and non-existent A/MX mechanisms trigger RFC 7208 §4.6.4 PermError after just 2 void lookups, even when total lookups are under 10.
- [Fix SPF PermError: Too Many DNS Lookups (Exceeding the RFC 7208 10-Lookup Limit)](https://opslint.dev/spf/permerror-too-many-dns-lookups-10-limit/) — Understand which SPF mechanisms count toward RFC 7208 §4.6.4's hard 10-lookup ceiling, why nested vendor includes blow the budget, and how to cut lookups safely.
- [Why Manual Static SPF IP Flattening Breaks Production Email (And Safer Alternatives)](https://opslint.dev/spf/static-ip-flattening-stale-netblocks-risks/) — Understand how inlining ip4:/ip6: CIDRs from Google, Microsoft, or SendGrid causes silent delivery outages when cloud netblocks rotate, plus zero-maintenance alternatives.
- [Fix Multiple SPF TXT Records PermError: How to Merge Two v=spf1 Records Safely](https://opslint.dev/spf/multiple-spf-txt-records-merge-guide/) — Why publishing two separate v=spf1 TXT records on the same hostname triggers immediate RFC 7208 §4.5 PermError, and the exact algorithm to merge them into one.
