Skip to content

Publish SPF

SPF authorizes sending IPs for the SMTP envelope domain. Inventory all legitimate sending routes first.

Setup

Prepare your values and Debian packages. Replace the example domain and IP.

This block prints the TXT value for a single IPv4 sending address. Publish one SPF TXT record at the domain name. Add other providers only with the includes they actually require; SPF evaluation permits at most ten DNS-querying mechanisms or modifiers.

. /root/mailserver.env
printf '%s. IN TXT "v=spf1 ip4:%s -all"\n' "$DOMAIN" "$PUBLIC_IPV4"

Test

set -euo pipefail
. /root/mailserver.env
dig "$DOMAIN" TXT +short
python3 - <<'PY'
import dns.resolver, os
records = ["".join(p.decode() for p in answer.strings) for answer in dns.resolver.resolve(os.environ["DOMAIN"], "TXT")]
spf = [value for value in records if value.startswith("v=spf1")]
assert len(spf) == 1, ("Exactly one SPF policy must be present", spf)
assert "ip4:" + os.environ["PUBLIC_IPV4"] in spf[0].split()
assert spf[0].split()[-1] == "-all"
print("PASS: exactly one published SPF policy authorizes your IPv4 address and ends with -all.")
PY

The CLI test checks this specific single-IP recipe, not arbitrary include trees. For actual SPF verification, send a message through your own external sending route and inspect the recipient's locally determined SPF result. SPF does not automatically protect the visible From address.