Email Security

Dec 25, 2025

Ensuring Proper Email Security: A Deep Dive into SPF, DKIM, DMARC, and SPF Flattening

Email remains a critical communication tool for businesses and individuals alike. However, it’s also a prime target for cybercriminals who exploit vulnerabilities through phishing, spoofing, and impersonation attacks. To combat these threats, implementing robust email authentication protocols is essential. This blog post provides a comprehensive, detailed guide to setting up Sender Policy Framework (SPF), DomainKeys Identified Mail (DKIM), and Domain-based Message Authentication, Reporting, and Conformance (DMARC). We’ll also include a dedicated subchapter on SPF flattening—a technique to optimize SPF records when they become overly complex. Each section includes step-by-step instructions, code examples, and best practices to help you secure your domain effectively..

Email Authentication Protocols

Email authentication helps verify that messages are sent from legitimate sources, preventing unauthorized use of your domain. SPF checks if the sending server is authorized, DKIM adds a cryptographic signature to ensure message integrity, and DMARC ties them together with policy enforcement and reporting. Together, these protocols can reduce spoofing risks by up to 90%, improve deliverability, and provide visibility into email traffic. Without them, your domain is vulnerable to being impersonated, leading to phishing attacks that could damage your reputation or result in data breaches.

Setting up these protocols involves modifying your domain’s DNS records, typically TXT or CNAME entries. Always test changes in a monitoring mode to avoid disrupting legitimate emails. Tools like online checkers (e.g., for SPF, DKIM, and DMARC) are invaluable for validation.

Sender Policy Framework (SPF)

PF is an email validation system that allows domain owners to specify which IP addresses or servers are permitted to send emails on their behalf. When a receiving server gets an email, it checks the sender’s IP against the domain’s SPF record in DNS. If it matches, the email passes SPF authentication; otherwise, it may be marked as spam or rejected. This helps prevent domain spoofing, where attackers forge the “From” address. SPF works by publishing a TXT record in your DNS that lists authorized mechanisms. Common mechanisms include:

However, SPF has a limit of 10 DNS lookups per record to prevent abuse of DNS resources. Exceeding this can cause a “PermError” and fail authentication entirely.

How to Set Up SPF

Identify All Authorized Senders: Audit every service that sends email from your domain, such as marketing tools (e.g., Mailchimp), CRMs (e.g., Salesforce), or email providers (e.g., Microsoft 365). List their required includes or IPs. Create the SPF Record: Start with v=spf1 and add mechanisms. Keep it under 255 characters if possible, and monitor lookup count. Publish in DNS: Log into your DNS provider (e.g., GoDaddy, Cloudflare). Add a TXT record with: Host: @ (root domain). Value: Your SPF string. TTL: 3600 seconds (1 hour) for quick propagation.

Test and Validate: Use tools like dig TXT yourdomain.com or online SPF checkers to ensure the record is live and syntax is correct. Send test emails and check headers for spf=pass. Monitor and Update: Regularly review for changes in sender IPs or new services.

Basic SPF for a single IP:

v=spf1 ip4:192.168.0.1 -all 

This authorizes only the IP 192.168.0.1 and fails all others.

For multiple services (e.g., icloud):

v=spf1 include:_spf.google.com include:sendgrid.net ~all

This includes Apple’s SPF records with a soft fail qualifier. Note: This counts as 2 lookups initially, but nested includes may add more. Advanced with IPs and MX:

v=spf1 mx ip4:203.0.113.0/24 include:_spf.google.com -all

Authorizes MX records, a specific IP range, and Google, with strict fail.

Best Practies

DomainKeys Identified Mail (DKIM): Signing Your Messages

DKIM provides a digital signature for emails, verifying that the message hasn’t been tampered with in transit and originates from an authorized sender. It uses asymmetric cryptography: a private key signs the email on the sending server, and the public key is published in DNS for recipients to verify. Unlike SPF, which checks the envelope sender, DKIM focuses on the message content and headers.

DKIM is resilient to forwarding but can break if intermediaries alter signed headers.

Setup DKIM

Example DKIM TXT Record:

v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDnVgd0NyrRE261IIiPqi+0H1baNyKcdj8Kea/VlSP4exzvKx8pJ01EWMwd094FV/6OCBIf7KGKgowMnWl3tW3Z5G++uZHkdgF+6xg7b9PynmX/NTo2kx92hlGgegwyulF5B7d2FM0doaCeoO4rD05jZzwi3cXx/156Gg9Xwd/Z/QIDAQAB

Host: default._domainkey (for selector default). For services like SendGrid (using CNAME for delegation):

Type: CNAME
Host: s1._domainkey
Value: s1.domainkey.u123456.wl.sendgrid.net

This delegates signing to SendGrid. Example Email Header with DKIM Signature:

DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yourdomain.com; s=default; h=from:to:subject:date; bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv8=; b=AuUoFEfDxTDkHlLXSZEpVj79LICEps6eda7W3deTVFOk4yAUoqOB4nujc7YopdG5 dHwxs4yRUESOrqN4hXQDgQBWKGXn4vcoD9hGf6Y6Uv5KevhgXKvc4OH60z5vJpkHH uCiNMBBjqpTHYy2hXzrQ==

This signs specific headers and the body hash.

Best Practices

Domain-based Message Authentication, Reporting, and Conformance (DMARC): Enforcing and Reporting

DMARC builds on SPF and DKIM by specifying what to do with emails that fail authentication. It requires “alignment” (SPF/DKIM domains matching the From header) and provides policies: none (monitor), quarantine (spam), or reject (block). Additionally, it sends aggregate (rua) and forensic (ruf) reports to help monitor abuse. DMARC protects against exact-domain spoofing and improves visibility—over 80% of domains without it are at risk.

Setup DMARC

SPF Flattening – Optimizing Complex Records

SPF flattening is a technique to resolve the 10 DNS lookup limit by replacing include: mechanisms with their underlying IP addresses or ranges. Instead of nested lookups (e.g., including _spf.google.com which itself has lookups), you “flatten” the record into a direct list of IPs. This reduces lookups to 1 (your record) but can make the record lengthy. It’s a workaround for domains with many third-party senders, preventing “Too Many DNS Lookups” errors that cause SPF failures. For example, a nested record might exceed limits, leading to rejections. Flattening compiles all IPs into one flat entry.

Why is SPF Flattening Needed?

The SPF standard (RFC 7208) caps lookups at 10 to avoid DNS overload. With multiple services (e.g., Google, Microsoft, CRM tools), includes can chain and exceed this, breaking authentication. Flattening eliminates nested lookups, ensuring compliance. It’s relevant for complex setups but not ideal long-term due to maintenance issues.

Manual implementation is error-prone; prefer automated tools.

Before Flattening (Nested, potentially over limit):

v=spf1 include:_spf.google.com include:sendgrid.net include:mail.zendesk.com -all

This could involve 15+ lookups if nested.

After Flattening (Flat IPs):

v=spf1 ip4:66.249.64.0/19 ip4:173.194.0.0/16 ip4:209.85.128.0/17 ip4:198.51.100.0/24 ip6:2607:f8b0:4004::/48 -all

This lists Google’s and others’ IPs directly, with 0 additional lookups.

Pros and Cons

Pros:

Cons:

Use flattening only if you exceed 10 lookups and can’t optimize otherwise—e.g., with 15+ senders. Avoid it by:

Tools like SPF flatteners or DMARC analyzers can help monitor without flattening.

Conclusion: Securing Your Email Ecosystem

Implementing SPF, DKIM, and DMARC is a foundational step in email security, protecting against impersonation while boosting deliverability. Start with SPF and DKIM, then layer on DMARC for enforcement. For complex SPF setups, consider flattening cautiously, but prioritize optimization to avoid its pitfalls. Regular audits, testing, and monitoring reports will keep your configuration robust. By following these detailed steps, you can significantly reduce risks and ensure your emails reach the inbox securely. If you’re managing multiple domains, consider professional services for ongoing maintenance.