SPF
SPF
- SPF (Sender Policy Framework) is a mechanism that identifies email servers that are authorized to send email from your domain.
- Receivers perform an SPF Lookup by comparing the incoming mail's sending server to the list of authenticated senders in the SPF record.[2]
- If the email comes from an authorized sender, the email will
passthe SPF lookup.- DMARC would check for SPF Alignment by comparing the SMTP
MAILFROMand the email's "From:" fields to make sure they match.
- DMARC would check for SPF Alignment by comparing the SMTP
- If the sender is not authenticated, then the email either
failorsoftfailthe SPF lookup.- This means the email will rely on DKIM to pass DMARC and could result in undelivered mail.
- SPF alignment often fails in cases where email is automatically forwarded (e.g., from a professional account to a personal account, or if the email is sent to a distribution list), so it is best to have both SPF and DKIM enabled where possible.
- This means the email will rely on DKIM to pass DMARC and could result in undelivered mail.
- If the SPF record is missing, most receivers will treat is as a
noneorneutralresult.- This means that the receiver's policies will play a bigger role in whether the email is delivered or not.
- If the email comes from an authorized sender, the email will
- Each sending mail server/domain must be identified
- This can be directly via IP or through a domain/DNS lookup
- SPF is limited to 10 DNS lookups, and is evaluated all at once.
- Going over 10 causes a
PermError.
- Going over 10 causes a
TempErroris often caused by a transient (e.g. temporary) DNS lookup error.- It can be caused by recipient policies or a DNS lookup timeout; there may not be much you can do, but you should still check your record to make sure it's well below 10 look ups.[3]
- Once the SPF record is validated, the receiving email server matches the sending email server's address against the SPF record, stopping at the first match and corresponding action (pass, softfail, etc.)
- SPF is limited to 10 DNS lookups, and is evaluated all at once.
- This can be directly via IP or through a domain/DNS lookup
- Use subdomains for third-party services where possible.
- Configuring a unique subdomain (like
newsletter.example.com) for email marketing services can help you stay below the 10 lookup limit with a separate SPF record isolates reputation harm from from misconfigured vendors. - It is not recommended to add marketing services, like Mailchimp or Sendgrid, to your main SPF record
- Marketers use tons of servers to send mail to get around spam filters, and because of this, SPF DNS lookups often reach their limit before getting to the root IP and fail authentication.
- Configuring a unique subdomain (like
- SPF Authorizes a list of approved senders and provides weak[4] Authentication by comparing the sending IP against the list of approved senders.
SPF Implementation
The syntax guide on Open-SPF is phenomenal, but for simplicity, here's an example of a sub-optimal SPF record.
SPF uses qualifiers to indicate whether a source is permitted or unauthorized. They're also used to enforce policy if there's no accompanying DMARC record or if the receiving server is not equipped to handle DMARC policies.
+= Pass, the email originating host is allowed to send.- This is the default, and does not need to be explicitly written out.
-= Fail, the originating host is not allowed to send and messages should be rejected.~= SoftFail, originating host is not officially allowed to send, but authentic mail may be sent from it.?= Neutral, or no policy.
- Name:
@ - Type: TXT
- TTL: 3600
- Value:
v=spf1 +a mx ip4:123.45.67.89 a:contoso.com include:spf.example.com ~include:olddomain.com -all
The SPF record is first checked as a whole to ensure it is a valid record and falls below the 10 maximum DNS lookups. Then the record is processed in order, and once the email is matched to a qualifier, it stops getting processed. Let's break it down.
- Name:
@@- Designates the domain to which the SPF record applies;
@is the current top-level domain (e.g.,example.com) - Can also use a subdomain, like
mailormail.example.com, with the exact formatting dependent on your DNS name server.
- Designates the domain to which the SPF record applies;
- Type: TXT
- This is a text record (as opposed to A, CNAME, etc.)
- TTL: 3600
- The time to live is 1 hour (3600 seconds)
- This is an expiration date for the SPF record, and helps DNS servers maintain up-to-date records.
- Value:
v=spf1 +a mx ip4:123.45.67.89 a:contoso.com include:spf.example.com -allv=spf1- SPF version 1
- There is currently only one version.
+a- The
+qualifier passes emails which match this sender- Because the
+is the default qualifier, is not normally explicitly stated, and will not be stated in further records.
- Because the
- The
atells the recipient to check the current domain's DNS for A records (e.g., IPv4 addresses) and mark them as designated senders - Note: dmarcian recommends avoiding
aentries as they are often unnecessary and increase the number of lookups.[5]
- The
mx- Check the current domain's DNS for MX records and mark them as designated senders
- Note: Since the default mechanism is
+, it does not need to be explicitly written out
- Note: Since the default mechanism is
- The
mxmechanism resolve to the A/AAAA records, which can trigger additional lookups.- Note: dmarcian recommends avoiding
mxentries as they are often unnecessary and increase the number of lookups.[6]
- Note: dmarcian recommends avoiding
- Check the current domain's DNS for MX records and mark them as designated senders
ip4:123.45.67.89- Designates the IP address
123.45.67.89as an authorized sender - Could also be used to designate an IP range (e.g.,
ip4:123.45.67.0/24) - Note: Use
ip6:to designate an IPv6 address or range.
- Designates the IP address
a:contoso.com- Checks
contoso.comfor A and AAAA records[7] and designates them as authentic senders
- Checks
include:spf.example.com- Checks
spf.example.comfor its SPF record and includes that in the current SPF record lookup - Be careful as this can bring you much closer to the 10 DNS lookup limit and cause a
PermError
- Checks
~include:olddomain.com- This marks emails from
olddomain.comwith a "softfail" qualifier, indicating that emails from that domain shouldn't be rejected outright, but possibly tagged as suspicious.
- This marks emails from
-all- Fail all originating hosts that have not been matched to this point in the record
- Because this is the last entry, any email that is authenticated by one of the earlier entries will get through, and everything else will fail authentication
- This is similar to Firewall configurations where the last rule is often
deny any any
- Because this is a Fail qualifier, it must to be manually written out as
-~allis also frequently seen in default configurations, and is used when transitioning between services or when using email marketing services (Mailchimp, Sendgrid, etc.) which do not get added to the SPF record.
- The last entry is always an
allmechanism, which sets the qualifier for any email that doesn't match any earlier mechanism.
- Fail all originating hosts that have not been matched to this point in the record
Honorable mention: redirect
If you manage multiple domains and subdomains that all point reference the same record and information, you can configure them to all point to the same SPF record with a redirect. This replaces the entire SPF record and does not count towards the DNS lookup limit, and can reduce the headache of managing multiple identical SPF records.
A subdomain that redirects to an "spf-primary" record would look like this: mailer.example.com txt "v=spf1 redirect=spf-primary.example.com"
dig
Managing DNS Lookups and DNS Flattening
Going above 10 DNS lookups will cause a permanent error for your SPF record, preventing it from being used for authentication. Because most SPF records will point to external mail services whose structure could change without notice, it's important to stay as far below 10 as possible so you don't accidentally begin erroring out.
There are a few tools you can use to check the number of lookups you have:
- SPF Surveyor - dmarcian
- Well organized and easy to read.
- EasyDMARC SPF Checker
- Similar to dmarcian, though a little less clean.
- SPF Record Lookup - DMARCLY
- Pretty spartan, but gets the job done.
- Recommends flattening, which is bad practice.
What can you do to reduce lookups?
- Remove unnecessary and duplicate records
aandmxrecords are often unnecessary:arecords identify domain-name lookup addresses, likestore.example.com, and modernly the same IPs are not often reused for mail (especially with third-party email providers).mxrecords instruct sending domains where you receive email, and while intuitively it's the same place as where you send email from, this is often not the case (especially with third-party email providers).
- Remove any old or unused vendors or mechanisms
- Consolidate where possible
- If there are overlapping SPF records for different services you use, you may be able to eliminate one of them. The tools listed above can help you find all sub-includes and IP ranges to determine eligibility.
- Remove records from mass-marketing vendors
- Mass-marketing vendors (like MailChimp or SendGrid) do not work well with SPF, and will dramatically inflate the number of lookups you will perform. It's better to just configure DKIM for them instead.
- Use subdomains to split up certain senders
- If certain groups or departments use automated systems to send email, you could create a subdomain for that group or purpose that isolates its records from the main domain.
- e.g., if you use Acme Invoices as your billing platform, you could create
billing.example.comand configure it with just Acme Invoice's SPF records to remove it fromexample.com's SPF record.
- e.g., if you use Acme Invoices as your billing platform, you could create
- If certain groups or departments use automated systems to send email, you could create a subdomain for that group or purpose that isolates its records from the main domain.
- Configure DKIM
What is flattening, and why is it bad?
Flattening an SPF record is the process of condensing all DNS lookups (e.g., include:spf.example.com, a:contoso.com, etc.) into hard-coded IP addresses. This gets around the lookup problem by not requiring any lookups, and if you control the servers that send email, this could be just fine. However, if you rely on third-parties for your email needs, flattening will get you into trouble.
Third-party services may add, delete, or change their infrastructure without informing you. Using hard-coded IPs will open you up to bad-actors who take over the abandoned IPs, or cause legitimate email to fail authentication because the vendor added new IPs that are not part of your hard-coded SPF record. Unless you manage every part of your email infrastructure, it's best to avoid flattening.
Metadata
Sources
SPF: Project Overview
SPF: SPF Record Syntax
Why SPF Authentication Fails: none, neutral, fail(hard fail), soft fail, temperror, and permerror Explained - DMARCLY
Tags
There used to be an SPF record type, but that is no longer in use. ↩︎
More technically, the receiving server fetches the SPF TXT record from the sending domain in the SMTP
MAILFROMfield, and checks whether the sending IP address is listed in the record. ↩︎dmarcian's SPF Surveyor is a great tool that identifies the number of lookups at each step. ↩︎
Since it does not cryptographically verify content like DKIM ↩︎
SPF Best Practices: Avoiding SPF Record Flattening - dmarcian ↩︎
SPF Best Practices: Avoiding SPF Record Flattening - dmarcian ↩︎
Remember that A records are IPv4 addresses and AAAA records are for IPv6 addresses. ↩︎