SPF Record for Dummies: Improving Mail Delivery
SPF (Sender Policy Framework) record in short, is a TXT record that you could (and MUST!) add to your DNS records to tell the outside world which hosts (based on IP address) is permitted to send mail under your domain name. It is currently the industrial standard for email verification to prevent email spoofing.
A typical SPF record looks like this:
yourdomain.com. IN TXT "v=spf1 a mx -all"
This tell the receiving Mail Transport Agent (MTA) to accept email from IP addresses that matches the A/AAAA records (the 'a' rule) and the MX records (the 'mx' rule) of 'yourdomain.com' and reject email from all other sources (-all).
In general, a SPF record that the following form:
v=spf1 <q-1><rule-1> <q-2><rule-2> <q3-><rule-3> ...
Where <q-n> is one of the qualifies below:
+ (or omitted) means pass (MTA should accept, mark as 'pass')
? means neutral (MTA should accept, mark as 'neutral')
~ means soft fail (MTA should accept but mark accordingly)
- means fail (MTA should reject)
And <rule-n> is one of the mechanism below:
all - match any IP address
a - match A/AAAA records
ip4 - match a given IPv4 address range
ip6 - match a given IPv6 address range
mx - match the IP addresses resolved from MX record
ptr - match the PTR record in DNS
exists - match as long as the given domain name resolves to any address
include - Include policies from another SPF record
SPF record is assess rule-by-rule from left to right. So, you should notice that the 'all' rule is usually at the end to act as the default action. For example:
"v=spf1 a mx +all" is no different from "v=spf1 +all" and means anyone is allow to send email using yourdomain.com (which is a bad idea).
yourdomain.com. IN TXT "v=spf1 a a:example.com"
a:example.com will match the A/AAAA record of example.com. If the domain name is omitted (i.e. just 'a'), it means matching A/AAAA record of yourdomain.com
yourdomain.com. IN TXT "v=spf1 mx mx:example.com"
mx:example.com will match the MX record of example.com. If the domain name is omitted (i.e. just 'mx'), it means matching MX record of yourdomain.com
ip4 - match a given IPv4 address range
yourdomain.com. IN TXT "v=spf1 ip4:123.456.789.12 ip4:12.345.678.0/8"
ip4:123.456.789.12 match a single IP address as stated, the second rule match an IP address range 12.345.678.* (note the '/8' at the end).
ip6 - match a given IPv6 address range
Similar to ip4 but matches IPv6 address instead.
include - include policies from another SPF record
yourdomain.com. IN TXT "v=spf1 include:_spf.google.com"
This include the SPF record of _spf.google.com, which is common if you use GMail of Google Apps as your email service.
ptr and exists are less used. We will skip the details here.
Here is a good tool to check your SPF record
http://mxtoolbox.com/spf.aspx
Why SPF (for the curious one)?
The reason for implementation of SPF is that SMTP allows any computer to send email claiming from any source address just by setting the Return-Path and From headers. With SPF, a receiving mail server could compare the domain name specified in the Return-Path with SPF record of the domain to check if a host (based IP address) is allow to send email on behalf of the domain name.
For example, if you use a server (IP address: 123.456.789.012) to send out an email to a GMail account, claiming from [email protected]. GMail will check the SPF record of yourdomain.com to see if 123.456.789.012 is permitted to send email on behalf of yourdomain.com. If it is not permitted, GMail will drop the message. Many mail servers nowadays are very strict on SPF record in a sense that if SPF record is missing, the message will be dropped. Thus, it is important to setup your SPF record correctly.