A fast intro to Sender Policy Framework
Sender Policy Framework controls which IP addresses can send emails from your domain name. It’s not strictly required, but without it you will find a lot of services either block your emails or put them in “Spam”.
To configure SPF for your domain, create a DNS TXT record that looks like this:
v=spf1 ip4:192.0.2.0/24 include:otherdomain.com a -all
An SPF policy starts with the version: v=spf1. Then it lists all of the mechanisms which are allowed to send email (or not) from that domain:
ip4:...whitelists the given IPv4 address.ip6:...does the same but for IPv6 addresses.include:example.comtells the email receiver to fetch the SPF policy forexample.comand include it in this policy. So if you are sending emails using Amazon SES for example, you would addinclude:amazonses.com.atells the email receiver to check the DNS A record for this domain and add its IP to this policy.mxtells the email receiver to do the same but for the DNS MX record.-allrejects emails from any source which hasn’t already been listed. Email hosts evaluate SPF policies from left-to-right, so make sure you put this at the end!
Qualifiers
Why is there a dash at the start of -all in the policy above?
It’s a qualifier which tells the email receiver to reject emails from that source. Having just all (or +all) in our SPF policy means “accept emails from all other sources” whereas -all means “reject emails from all other sources”.
You can add a qualifier to any of the SPF mechanisms, for example -ip4:... or ~mx.
| Qualifier | Result | Notes |
|---|---|---|
+ | PASS | Accept emails from this source. The default. |
? | NEUTRAL | Treat the source as if there was no SPF policy set. |
~ | SOFTFAIL | Accept emails from this source but “tag” them (often this means putting them in the spam folder); useful for debugging. |
- | FAIL | Reject emails from this source. |
PASS is the default if there’s no qualifier, so mx and +mx mean the exact same thing.
? seems a bit pointless and + is optional, so in reality you’ll generally only use ~ and -. Use SOFTFAIL when you’re setting up and testing your SPF policy, and FAIL once you’re confident you’ve got it right.
When testing keep in mind that there is variation in how email hosts treat the different qualifiers. For example, Outlook.com puts SOFTFAIL emails in spam, but Gmail appends “via actualsender.com” to the “From” field instead. I imagine it also depends on the reputation of the sender.
Happy emailing! đź‘‹
See also
- RFC 7208, where the Sender Policy Framework is defined.
Last updated on