SPF (Sender Policy Framework) is a DNS record that tells receiving mail servers which IP addresses are allowed to send email for your domain. By publishing an SPF record, you reduce the chance that spammers can forge your domain in the 'From' address, which protects your business reputation and improves deliverability. This guide walks you through creating and publishing an SPF record, from gathering your sending sources to testing the final result.
What You'll Need
- Access to your domain's DNS management (usually via your domain registrar, hosting provider, or a dedicated DNS service like Cloudflare or Route53).
- A list of all services that send email on behalf of your domain (e.g., your web server, email marketing platform, CRM, or third-party transactional email service).
- Your domain name (e.g., example.com).
- Optional: a command-line tool like
digornslookupfor testing, or an online SPF checker.
Step-by-Step
- List all legitimate email senders. Go through every system that sends email using your domain. Common ones include: your office email server (often hosted by Microsoft 365, Google Workspace, or your own mail server), your website's contact form (if it uses PHP
mail()or SMTP), your email marketing service (e.g., Mailchimp, Constant Contact), and any transactional email service (e.g., SendGrid, Amazon SES). Write down the IP addresses or hostnames for each. - Find your current SPF record (if any). Use a DNS lookup tool or run
dig TXT example.comin a terminal. Look for a TXT record that starts withv=spf1. If you already have one, you'll modify it rather than create a new one (you can only have one SPF record per domain). If none exists, you'll create a new one. - Build your SPF record. The record is a single line of text. Start with
v=spf1. Then add mechanisms for each sender. For IP addresses, useip4:203.0.113.10(replace with your actual IP). For hostnames, useinclude:spf.sendgrid.net(replace with the provider's include). If you use a third-party service, they usually provide a specific include value in their documentation. If you have a dedicated mail server, addip4:your.server.ip. If you want to allow any server in your domain to send (not recommended for most), you could useaormx, but it's safer to be explicit. - Add the 'all' qualifier. At the end of the record, add
-all(hard fail) to reject all other senders, or~all(soft fail) to mark them as suspicious but still accept them. For most businesses,-allis recommended because it gives the strongest protection. Example record:v=spf1 ip4:203.0.113.10 include:spf.sendgrid.net -all. - Publish the record in DNS. Log into your DNS provider, go to the DNS records section, and add a new TXT record. The host/name field should be your domain (or leave blank if the provider defaults to the domain). The value field is your full SPF string. Save the record. Note: DNS changes can take anywhere from a few minutes to 48 hours to propagate globally.
- Verify your SPF record. After propagation, use an online SPF checker or run
dig TXT example.comto confirm the record is visible and correctly formatted. The checker will also tell you if you have any syntax errors or if you've exceeded the 10-DNS-lookup limit (see Common Mistakes). - Test sending an email. Send a test email from each of your sending sources to a Gmail or Outlook address. Open the email headers and look for 'Received-SPF' or 'Authentication-Results' fields. They should show 'pass' for legitimate sources. If any show 'fail', revisit your record.
Common Mistakes
- Multiple SPF records. If you have more than one TXT record starting with
v=spf1, receiving servers will typically reject both (or use a 'permerror'). Always consolidate into a single record. - Exceeding the 10-DNS-lookup limit. Each
include,a,mx, orptrmechanism counts as a DNS lookup. If you exceed 10, SPF will permanently error. Count your lookups and consolidate if needed. - Using
+allor forgettingall.+allmeans 'allow everyone' and is useless. Omittingallmeans the record is incomplete and may be treated as neutral. - Not including all senders. If you forget a service, its emails will fail SPF and may go to spam or be rejected. Double-check your list.
- Using a hard fail (
-all) when you have unlisted senders. This can cause legitimate email to be rejected. If you're unsure, start with~alland tighten later.
Tips
- Use
include:for third-party services instead of listing their IPs manually – this way, if they change IPs, your record stays accurate. - Keep your SPF record as short as possible. The fewer mechanisms, the fewer chances of hitting the lookup limit.
- If you use multiple services, consider using a subdomain for some of them (e.g.,
mail.example.com) and create a separate SPF record for that subdomain, so you don't bloat the main record. - After publishing, wait at least 24 hours before making further changes, as propagation is not instant.
- Monitor your email deliverability for a week. If you see a drop, check your SPF record and your sending sources.
Troubleshooting
Emails are going to spam after adding SPF. First, check the SPF result in the email headers. If it says 'fail', you likely missed a sender or used -all too aggressively. Temporarily switch to ~all and confirm all senders are listed. If it says 'neutral' or 'none', your record may not be published correctly – check the exact TXT value and ensure there are no extra spaces or quotes.
SPF record is not appearing. DNS propagation can take time. Use a global DNS checker (like whatsmydns.net) to see if it's visible from multiple locations. Also, ensure you added the record as TXT, not as an SPF type (SPF type is deprecated).
Receiving 'permerror' from receiving servers. This usually means your SPF record has a syntax error or exceeds the 10-lookup limit. Use an SPF validation tool to identify the issue.
One of your services is failing SPF. Look up the service's documentation for the correct include. Some services require you to add a specific subdomain or IP. If you're using a custom SMTP server, verify the IP you listed is the actual outbound IP (not just the server's inbound IP).
You have a shared hosting server. If you share an IP with other domains, listing that IP in SPF means those other domains could send as you. Consider using a dedicated IP for your mail, or use an include from your hosting provider if they offer one.
Frequently Asked Questions
What does SPF actually protect against?
SPF prevents spammers from using your domain in the 'From' address of their emails. It does not encrypt emails or prevent phishing that uses your domain in the reply-to field – that requires DMARC.
Can I have both SPF and DMARC?
Yes, they work together. SPF authenticates the sending server, while DMARC tells receivers what to do if SPF or DKIM fails. For full protection, set up both.
How long does it take for SPF to work?
DNS propagation typically takes 1-48 hours, but most updates are visible within a few hours. You can test immediately after propagation, but allow a full day before judging deliverability.
What if I don't have a static IP for my mail server?
If your ISP assigns dynamic IPs, you can't use ip4: for that server. Instead, use an include: from a service that provides a dynamic IP range, or use a relay service. Some small businesses use a third-party SMTP relay to avoid this.
Is it safe to use ~all instead of -all?
Yes, ~all is a soft fail, which means receiving servers will not reject the email but may mark it as suspicious. It's a good starting point if you're not 100% sure you've listed all senders.
What happens if I remove my SPF record?
Your emails will still be sent, but receiving servers will have no way to verify your domain, which can increase spam filtering and reduce deliverability. It's better to keep a valid record.