SPF Records Explained: Implementation and Troubleshooting

A comprehensive technical guide to Sender Policy Framework (SPF) records, including syntax, implementation, validation, and common troubleshooting steps.

Sender Policy Framework (SPF) is an essential email authentication mechanism that helps prevent domain spoofing and enhances email deliverability. This comprehensive guide will walk you through the fundamentals of SPF records, their syntax, implementation best practices, and troubleshooting techniques. By the end of this guide, you'll have a solid understanding of how SPF works and how to effectively deploy it for your domain.

What is Sender Policy Framework (SPF)?

Sender Policy Framework (SPF) is an open standard email authentication method designed to combat email spoofing. It allows domain owners to specify which mail servers are authorized to send emails on behalf of their domain. When an email is received, the receiving server looks up the SPF record of the envelope sender domain (the Return-Path or MAIL FROM address, not the visible From address) and checks whether the connecting server's IP address is authorized. This helps prevent spammers and attackers from sending fraudulent emails using your domain name.

Understanding SPF Record Syntax

An SPF record is a DNS TXT record that specifies the authorized mail servers for a domain. The record consists of a version identifier followed by mechanisms, each of which can carry an optional qualifier in front of it. Here's a breakdown of the SPF record syntax:

v=spf1 [qualifier]mechanism [qualifier]mechanism ... [qualifier]all
  • v=spf1: The version identifier, indicating that this is an SPF record.
  • [mechanism]: Specifies the authorized mail servers or IP addresses. Common mechanisms include:
    • a: Allows the IP addresses in the domain's A (and AAAA) records.
    • mx: Allows the IP addresses of the domain's MX hosts.
    • ip4: Allows a specific IPv4 address or range.
    • ip6: Allows a specific IPv6 address or range.
    • include: Includes another domain's SPF record.
    • all: Matches every remaining source; it goes at the end of the record.
  • [qualifier]: Specifies the result when the mechanism matches. Qualifiers include:
    • +: Pass (default)
    • -: Fail
    • ~: Soft fail
    • ?: Neutral
Note: An SPF record can have multiple mechanisms and qualifiers. The order of mechanisms is important, as the first matching mechanism determines the result.

Example SPF Record

Here's an example SPF record for the domain example.com:

v=spf1 ip4:192.0.2.0/24 ip6:2001:db8::/32 include:_spf.google.com ~all

This record allows emails from the specified IPv4 and IPv6 ranges, includes Google's SPF record, and soft fails all other sources.

Implementing SPF for Your Domain

To implement SPF for your domain, follow these steps:

  1. Identify all mail servers and third-party services that send emails on behalf of your domain.
  2. Create an SPF record that includes the authorized mail servers and services.
  3. Publish the SPF record as a DNS TXT record for your domain.
  4. Test your SPF record using an SPF validation tool.
Best Practices for SPF Implementation
  • Publish only one SPF record per domain; two or more cause a permanent error.
  • Keep your SPF record concise and limit the number of DNS lookups to 10 or fewer.
  • Use the include mechanism for third-party services instead of listing individual IP addresses.
  • End your SPF record with an -all or ~all qualifier to prevent unauthorized sources.
  • Regularly review and update your SPF record as your email infrastructure changes.

SPF Validation and Checking

When an email is received, the receiving server performs an SPF check to validate the sender's identity. The validation process involves the following steps:

  1. The receiving server takes the domain from the envelope sender (the MAIL FROM address, which later appears as the Return-Path header), or from the HELO name if the envelope sender is empty. The visible "From" header is not used.
  2. The server queries the domain's DNS for the SPF record.
  3. The server evaluates the SPF record, checking the sender's IP address against the allowed mechanisms.
  4. The server returns an SPF result based on the matching mechanism and qualifier.

The SPF validation process is illustrated in the following diagram:

The receiving server takes the envelope sender domain from the Return-Path, looks up that domain's SPF TXT record, and checks the sending IP against the mechanisms in order. The first match gives the result: pass, soft fail or fail.
How a receiving server checks SPF

SPF Results and Their Meanings

Result Meaning
Pass The email originated from an authorized source.
Fail The email originated from an unauthorized source.
Soft Fail The email is likely unauthorized but not definitively.
Neutral The SPF record does not assert whether the email is authorized or not.
None The domain does not have an SPF record.
PermError A permanent error occurred during SPF evaluation, for example a syntax error, more than 10 DNS lookups, or more than one SPF record.
TempError A temporary error occurred during SPF evaluation.
Note: While SPF helps prevent email spoofing, it does not guarantee the authenticity of the email content. It's recommended to use SPF in conjunction with other email authentication methods like DKIM and DMARC for better protection.

SPF Troubleshooting

When implementing SPF, you may encounter some common issues. Here are a few troubleshooting tips:

SPF has a limit of 10 DNS lookups to prevent excessive load on DNS servers. The include, a, mx, exists and redirect terms count toward the limit (including lookups inside included records), while ip4 and ip6 do not. If your record exceeds the limit, SPF returns a PermError, which DMARC treats as a fail. To resolve this issue:

  • Remove include mechanisms for services you no longer use.
  • Replace a and mx mechanisms with ip4/ip6 ranges where the addresses are stable.
  • Move some senders to their own subdomain with a separate SPF record.
  • Consider SPF flattening, but only with a tool that keeps the record updated, because providers change their IP addresses.

If legitimate emails are failing SPF checks, it may be due to missing or incorrect SPF record configurations. To troubleshoot this issue:

  • Verify that your SPF record includes all authorized mail servers and third-party services.
  • Check for any recent changes in your email infrastructure that may require updating your SPF record.
  • Use SPF testing tools to validate your SPF record and identify any misconfigurations.
  • Remember that forwarding usually breaks SPF, because the forwarding server's IP isn't in your record. DKIM survives forwarding, which is why you should use both.

If unauthorized emails are passing SPF checks, it may indicate that your SPF record is too permissive. To address this issue:

  • Review your SPF record and remove any unnecessary include mechanisms or overly broad IP ranges.
  • Ensure that your SPF record ends with a -all or ~all qualifier to prevent unauthorized sources.
  • Consider implementing additional email authentication methods like DKIM and DMARC to strengthen your email security.

The following diagram illustrates common SPF troubleshooting scenarios and their solutions:

Five common SPF problems with fixes: more than 10 DNS lookups, legitimate services missing from the record, two SPF records on one domain, a record that is too permissive, and forwarded mail failing SPF.
Common SPF problems and how to fix them

Conclusion and Next Steps

Implementing SPF is a critical step in securing your email infrastructure and protecting your domain from spoofing and phishing attempts. By following the best practices outlined in this guide and regularly monitoring your SPF configuration, you can enhance your email deliverability and maintain a strong domain reputation.

To further strengthen your email security, consider implementing additional authentication methods like DKIM and DMARC. These methods work in conjunction with SPF to provide a multi-layered defense against email fraud.

Action Items

  • Assess your current email infrastructure and identify all authorized mail servers and third-party services.
  • Create an SPF record that accurately represents your email sending sources.
  • Publish your SPF record as a DNS TXT record for your domain.
  • Test your SPF implementation using SPF validation tools and monitor your email deliverability.
  • Educate your team about SPF and email authentication best practices.
  • Explore implementing DKIM and DMARC to further enhance your email security posture.

By following this guide and taking action to implement SPF, you're well on your way to securing your email communications and protecting your domain's reputation. Stay proactive, monitor your email authentication setup regularly, and adapt to emerging threats to maintain a robust email security strategy.

Check your own email in a minute. Send one email to a test address and see your SPF, DKIM, DMARC and spam score.
Test your email
Was this guide helpful?

Topics: Deliverability, Authentication, Best Practices, Technical