Advanced SPF Record Configuration

Detailed guide on configuring SPF records for complex email infrastructures, including multiple domains and third-party senders.

Properly configured SPF records are essential for ensuring the deliverability and security of emails sent from your domain. For organizations with complex email infrastructures spanning multiple domains and utilizing third-party email service providers, crafting comprehensive SPF records can be a challenging task. This in-depth guide will walk you through the process of configuring advanced SPF records, complete with detailed explanations, real-world examples, and best practices to ensure your emails are properly authenticated and delivered to your recipients' inboxes.

Understanding SPF Records

Sender Policy Framework (SPF) is an email authentication protocol that allows domain owners to specify which mail servers are authorized to send emails on behalf of their domain. Receiving servers check SPF against the envelope sender (the MAIL FROM or Return-Path domain, or the HELO name), not the visible From address. On its own, SPF therefore can't stop someone faking your From header; combined with DKIM and DMARC, it helps stop spammers spoofing your domain and protects your brand's reputation.

An SPF record is a TXT record that consists of a series of mechanisms and modifiers that define the permitted mail servers for your domain. Here's an example of a basic SPF record:

v=spf1 mx ip4:192.0.2.0/24 include:_spf.example.com -all

This record states that only the domain's MX records, IP addresses in the 192.0.2.0/24 range, and any servers listed in the _spf.example.com record are allowed to send emails for the domain. The "-all" mechanism indicates that any other mail servers are explicitly not permitted.

Planning Your SPF Record

Before configuring your SPF record, it's crucial to identify all the mail servers and third-party services that send emails on behalf of your domain. This may include:

  • Your organization's primary mail servers
  • Backup or failover mail servers
  • Transactional email services (e.g., Mailgun, SendGrid)
  • Marketing automation platforms (e.g., Marketo, HubSpot)
  • CRM systems with email capabilities (e.g., Salesforce, Zoho)

Create a comprehensive list of all these sources, along with their IP addresses or include domains, to ensure your SPF record accurately reflects your email infrastructure.

Crafting Your SPF Record

When constructing your SPF record, follow these best practices:

Use the "include" mechanism for third-party services

Instead of listing individual IP addresses for third-party services, use the "include" mechanism to reference their SPF records. For example, to authorize SendGrid, use "include:sendgrid.net" in your SPF record.

Tip: Most reputable email service providers publish their SPF include domain in their documentation or support resources.

Limit the number of DNS lookups

To prevent SPF processing issues, ensure your SPF record requires no more than 10 DNS lookups. Each "include", "a", "mx", "exists" and "redirect" counts as a lookup, and so does every lookup inside the records you include, so be mindful of the number of third-party services you include. "ip4" and "ip6" don't count.

Warning: Exceeding the 10 DNS lookup limit makes SPF return a permanent error (permerror), which many receivers treat as a failure, potentially impacting your email deliverability.

Use the "~all" softfail mechanism during testing

When first deploying your SPF record, use the "~all" softfail mechanism instead of "-all" to prevent legitimate emails from being rejected due to misconfigurations. Once you've confirmed your SPF record is correct, switch to "-all" to ensure strict enforcement.

Here's an example of a comprehensive SPF record for a complex email infrastructure:

v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.0/28 include:_spf.example.com include:sendgrid.net include:_spf.salesforce.com ~all

The following diagram explains what each part of an SPF record does:

Six cards explaining the parts of an SPF record: the v=spf1 tag, ip4 and ip6 ranges, include, a and mx, the all qualifier, and the limit of 10 DNS lookups.
What each part of an SPF record does

Implementing Your SPF Record

To implement your SPF record, follow these steps:

  1. Log in to your domain registrar or DNS management console.
  2. Navigate to the DNS settings for your domain.
  3. Add a new TXT record with the following details:
    • Name/Host: @
    • Value: Your SPF record, e.g., "v=spf1 ip4:192.0.2.0/24 include:_spf.example.com -all"
    • TTL: 3600 (or your preferred TTL value)
  4. Save the new TXT record and wait for the changes to propagate (often within minutes, but allow up to the TTL of any old record, and occasionally up to 48 hours).
Best Practice: If you have an existing SPF record, update it rather than creating a second one. Multiple SPF records may cause issues with email deliverability.

The following diagram illustrates the process of rolling out an SPF record:

Five steps to roll out SPF: list every sending service, build one record, publish it as a single TXT record, test each source, then switch from ~all to -all.
Rolling out an SPF record without blocking real mail

Testing and Troubleshooting

After implementing your SPF record, it's essential to test its correctness and troubleshoot any issues that may arise. Here are some tools and techniques to help you ensure your SPF record is working as intended:

Several online tools can help you validate your SPF record and identify any syntax errors or misconfigurations:

These tools will parse your SPF record, check for syntax errors, and provide recommendations for improvement.

To ensure your emails are properly authenticated, send test messages from each of your mail servers and third-party services to an email testing tool like:

These tools will analyze your email headers and provide a report on your SPF, DKIM, and DMARC alignment, helping you identify any authentication issues.

Continuously monitor your email deliverability and authentication metrics using tools like:

These platforms provide valuable insights into your email performance, such as authentication results, spam complaint rates and reputation signals. DMARC aggregate reports are the most direct way to see SPF and DKIM pass and alignment results for every source sending as your domain. Regularly reviewing these metrics will help you quickly identify and address any authentication issues that may impact your email deliverability.

Advanced SPF Considerations

In addition to the basic SPF configuration, there are several advanced considerations to keep in mind when managing SPF records for complex email infrastructures:

Dynamic SPF Includes

For organizations with frequently changing IP addresses or third-party services, maintaining an up-to-date SPF record can be challenging. Dynamic SPF includes allow you to manage your SPF configuration externally and reference it in your primary SPF record using the "include" mechanism. This approach simplifies updates and reduces the risk of exceeding the 10 DNS lookup limit.

Subdomain SPF Records

If your organization sends emails from subdomains with different mail servers or third-party services, you may need to create separate SPF records for each subdomain. This allows for more granular control over email authentication and can help prevent issues with the 10 DNS lookup limit.

Aligning SPF with DKIM and DMARC

While SPF is an essential component of email authentication, it works best when combined with DKIM (DomainKeys Identified Mail) and DMARC (Domain-based Message Authentication, Reporting, and Conformance). DMARC passes when SPF or DKIM passes and uses a domain that aligns with your From domain, so making sure at least one of them aligns (ideally both) will maximize your email deliverability and protect your domain from spoofing and phishing attempts.

Note: Implementing DKIM and DMARC is beyond the scope of this guide, but it's crucial to consider these protocols when developing a comprehensive email authentication strategy.

Conclusion and Next Steps

Implementing advanced SPF configurations is crucial for organizations with complex email infrastructures to ensure proper email authentication, deliverability, and security. By following the best practices outlined in this guide and regularly testing and monitoring your SPF records, you can protect your domain's reputation and maintain high inbox placement rates.

To further enhance your email authentication strategy, consider the following next steps:

  1. Implement DKIM and DMARC in alignment with your SPF records
  2. Develop a process for regularly reviewing and updating your SPF configuration
  3. Monitor your email performance metrics and sender reputation scores
  4. Educate your team on email authentication best practices and the importance of maintaining accurate SPF records

By taking a proactive approach to email authentication and staying up-to-date with industry best practices, you can ensure your organization's emails are delivered reliably and securely to your recipients' inboxes.

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