Sign In
Deliverability

Simple steps how to prevent spam

How to Prevent Spam: Mastering Email Authentication

Email spam remains a persistent problem, clogging inboxes and posing security risks. While spam filters have become sophisticated, spammers continually adapt their tactics. This article focuses on a crucial aspect of combating spam: email authentication. We’ll delve into implementing and managing SPF, DKIM, and DMARC records – essential tools for verifying email sender identity and improving email deliverability, ensuring your legitimate emails reach their intended recipients.

This guide aims to provide a practical, hands-on approach to understanding and implementing these protocols, empowering you to take control of your email security and minimize the risk of your emails being flagged as spam.

SectionDescription
Understanding SPF (Sender Policy Framework)Explains what SPF is and how it helps prevent email spoofing.
Implementing DKIM (DomainKeys Identified Mail)Details how DKIM uses cryptographic signatures to verify email integrity.
Configuring DMARC (Domain-based Message Authentication, Reporting & Conformance)Shows how DMARC builds on SPF and DKIM to define policies for handling unauthenticated emails.
Testing and Troubleshooting Authentication RecordsProvides practical steps for verifying and resolving issues with your SPF, DKIM, and DMARC configurations.

Understanding SPF (Sender Policy Framework)

How to prevent spam - Illustration of how SPF works: An email being sent, an SPF check happening at the recipient's mail server, and a pass/fail result.
Sender Policy Framework (SPF) is an email authentication method designed to prevent spammers from using your domain to send unauthorized emails. It works by allowing you to specify which mail servers are authorized to send email on behalf of your domain. When a receiving mail server receives an email claiming to be from your domain, it checks the SPF record to verify if the sending server is listed as an authorized sender. If the sending server isn’t listed, the email is more likely to be flagged as spam. Essentially, SPF creates a “whitelist” of IP addresses or hostnames that are permitted to send emails using your domain name in the “Mail From” address (also known as the envelope sender address). This helps receiving mail servers determine if an email is legitimate or a potential forgery. Creating an SPF Record An SPF record is a TXT record added to your domain’s DNS settings. It contains a specific string that defines the authorized sending sources for your domain. Here’s a breakdown of the common components:
  • v=spf1: This specifies the SPF version being used (currently, only v=spf1 is used).
  • ip4: Specifies individual IPv4 addresses or ranges.
  • ip6: Specifies individual IPv6 addresses or ranges.
  • a: Authorizes the IP address(es) of the host specified by the domain name.
  • mx: Authorizes the IP address(es) of the mail exchangers (MX records) for the specified domain.
  • include: Includes another domain’s SPF record. This is often used when using third-party email services.
  • -all, ~all, ?all, +all: The “all” mechanism specifies what should happen if none of the preceding mechanisms match. “-all” means “fail” (emails from non-authorized sources should be rejected), “~all” means “softfail” (emails from non-authorized sources should be accepted but marked as potentially suspicious), “?all” means “neutral” (no policy), and “+all” (rarely used, essentially disables SPF).
Example 1: Basic SPF Record
v=spf1 ip4:192.0.2.0/24 -all
This record authorizes any server within the 192.0.2.0/24 IPv4 range to send email on behalf of your domain. The -all mechanism specifies that any other source should be considered a hard fail, meaning the receiving server should reject the email. Example 2: Including a Third-Party Email Service
v=spf1 include:servers.mailchimp.com -all
This record includes the SPF record of Mailchimp, allowing Mailchimp to send emails on behalf of your domain. This is crucial if you use a service like Mailchimp for sending newsletters or marketing emails. Without this, your emails sent through Mailchimp might be flagged as spam. Example 3: Authorizing Multiple Sources
v=spf1 ip4:192.0.2.0/24 ip4:203.0.113.10 include:servers.mailchimp.com -all
This record authorizes the 192.0.2.0/24 and 203.0.113.10 IPv4 ranges, as well as Mailchimp’s servers, to send email on behalf of your domain. Best Practices for SPF Records
  • Limit the Number of Lookups: SPF has a limit of 10 DNS lookups. Exceeding this limit can cause SPF checks to fail. This limit is especially relevant when using multiple include statements.
  • Use Specific IP Addresses or Ranges: Avoid using overly broad IP ranges if possible. Be as specific as you can to minimize the risk of unauthorized senders being included.
  • Regularly Review Your SPF Record: Ensure your SPF record remains accurate and reflects your current sending infrastructure. As your email sending practices change, update your SPF record accordingly.
  • Start with ~all and Monitor: Before implementing -all, which can potentially block legitimate emails if misconfigured, start with ~all (softfail). Monitor your email deliverability and SPF reports to identify any false positives before switching to -all.
  • Consider Using a Dedicated Subdomain: If you send transactional emails and marketing emails from different sources, consider using a dedicated subdomain for marketing emails. This allows you to create a separate SPF record for that subdomain, isolating any potential deliverability issues.
Expert Tip: It’s often better to start with a restrictive SPF record and gradually add authorized senders as needed, rather than starting with a permissive record and trying to restrict it later.

Implementing DKIM (DomainKeys Identified Mail)

How to prevent spam - Diagram showing the DKIM signing process: An email being composed, the email server using a private key to create a DKIM signature, the signature being added to the email header, the recipient's mail server verifying the signature using the public key from DNS.
DomainKeys Identified Mail (DKIM) is an email authentication method that uses cryptographic signatures to verify the integrity of an email and confirm that it was indeed sent by the domain it claims to be from. Unlike SPF, which focuses on the sending server, DKIM focuses on the email content itself. It adds a digital signature to the email header, which receiving mail servers can use to verify that the email hasn’t been altered in transit and that it originated from the claimed sender. DKIM works by using a pair of cryptographic keys: a private key, which is kept secret by the sending server, and a public key, which is published in the domain’s DNS records. When an email is sent, the sending server uses its private key to generate a digital signature based on the email’s headers and body. This signature is then added to the email’s header as a DKIM-Signature field. Generating DKIM Keys The first step in implementing DKIM is generating the public and private key pair. Many email servers and email service providers (ESPs) provide tools for generating these keys. Here’s an example using OpenSSL:
openssl genrsa -out private.key 2048
openssl rsa -in private.key -pubout -out public.key
This will generate two files: private.key (containing the private key) and public.key (containing the public key). Keep the private key secure. It should only be accessible to your email server. Adding the DKIM Record to DNS The public key needs to be added to your domain’s DNS as a TXT record. The record name typically follows the format selector._domainkey.yourdomain.com, where “selector” is an arbitrary string you choose to identify the key. Common selectors are “default” or “mail”. Example: DKIM DNS Record
Selector: default
Record Name: default._domainkey.example.com
Record Type: TXT
Record Value: v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwWfW7wW9r99... (rest of the public key) ...AQAB;
Here’s a breakdown of the DKIM record values:
  • v=DKIM1: Specifies the DKIM version.
  • k=rsa: Specifies the key type (RSA).
  • p=: Contains the public key. This is the long string generated earlier. Remove any line breaks from the public key before adding it to the DNS record.
Important Considerations:
  • Key Length: Use a key length of at least 2048 bits for strong security.
  • Selector Uniqueness: If you have multiple email sending services, use different selectors for each to avoid conflicts.
  • Record Length Limits: Some DNS providers have length limits for TXT records. If your public key is too long, you may need to split it into multiple strings.
Configuring Your Email Server to Sign Emails The final step is to configure your email server (e.g., Postfix, Exim, Sendmail) to sign outgoing emails using the private key. The specific configuration steps vary depending on the email server software you’re using. Example: Postfix Configuration (using OpenDKIM)
  • Install OpenDKIM: sudo apt-get install opendkim opendkim-tools
  • Configure OpenDKIM in /etc/opendkim.conf:
Syslog          yes
SyslogSuccess   yes
LogWhy          yes

UserID          opendkim:opendkim

Socket          inet:8891@localhost

KeyTable        refile:/etc/opendkim/KeyTable
SigningTable    refile:/etc/opendkim/SigningTable
TrustedHosts    refile:/etc/opendkim/TrustedHosts
  • Create the KeyTable file (/etc/opendkim/KeyTable):
default._domainkey.example.com example.com:default:/etc/opendkim/keys/example.com/default.private
  • Create the SigningTable file (/etc/opendkim/SigningTable):
*@example.com        default._domainkey.example.com
  • Create the TrustedHosts file (/etc/opendkim/TrustedHosts):
127.0.0.1
localhost
::1
example.com
  • Create the directory for the keys and copy the private key: sudo mkdir -p /etc/opendkim/keys/example.com; sudo cp private.key /etc/opendkim/keys/example.com/default.private; sudo chown opendkim:opendkim /etc/opendkim/keys/example.com/default.private; sudo chmod 600 /etc/opendkim/keys/example.com/default.private
  • Configure Postfix to use OpenDKIM in /etc/postfix/main.cf:
milter_default_action = accept
milter_protocol = 2
smtpd_milters = inet:127.0.0.1:8891
non_smtpd_milters = inet:127.0.0.1:8891
  • Restart OpenDKIM and Postfix: sudo systemctl restart opendkim; sudo systemctl restart postfix
This is a simplified example, and the specific configuration will depend on your email server setup. Refer to the OpenDKIM documentation for more detailed instructions. Expert Tip: Regularly rotate your DKIM keys (e.g., every year) to enhance security. This involves generating a new key pair and updating your DNS record and email server configuration.

Configuring DMARC (Domain-based Message Authentication, Reporting & Conformance)

Domain-based Message Authentication, Reporting & Conformance (DMARC) builds upon SPF and DKIM to provide a comprehensive email authentication framework. DMARC allows you to define a policy for how receiving mail servers should handle emails that fail SPF and/or DKIM checks. It also provides a mechanism for receiving reports about email authentication results, allowing you to monitor your email sending sources and identify potential spoofing attempts. DMARC essentially tells receiving mail servers what to do with emails that fail authentication:
  • none: The receiving server should take no specific action. This is typically used for initial monitoring.
  • quarantine: The receiving server should mark the email as spam or move it to the spam folder.
  • reject: The receiving server should reject the email entirely.
Creating a DMARC Record A DMARC record is a TXT record added to your domain’s DNS settings. The record name is always _dmarc.yourdomain.com. Example: DMARC DNS Record
Record Name: _dmarc.example.com
Record Type: TXT
Record Value: v=DMARC1; p=none; rua=mailto:dmarc-reports@example.com; ruf=mailto:dmarc-forensic@example.com; adkim=r; aspf=r;
Here’s a breakdown of the DMARC record values:
  • v=DMARC1: Specifies the DMARC version.
  • p=: Specifies the policy to be applied to emails that fail authentication (none, quarantine, or reject).
  • rua=: Specifies the email address to which aggregate reports should be sent. These reports provide a summary of email authentication results.
  • ruf=: Specifies the email address to which forensic reports should be sent. These reports provide detailed information about individual emails that failed authentication.
  • adkim=: Specifies the DKIM alignment mode. ‘r’ for relaxed, ‘s’ for strict. Relaxed allows for subdomain matching.
  • aspf=: Specifies the SPF alignment mode. ‘r’ for relaxed, ‘s’ for strict. Relaxed allows for subdomain matching.
Important Considerations:
  • Start with p=none: It’s crucial to start with p=none to monitor your email traffic and identify any legitimate emails that might be failing authentication. Analyze the DMARC reports to understand the sources of your email and identify any misconfigurations or unauthorized sending sources.
  • Monitor DMARC Reports: Regularly review the aggregate reports (rua) to identify any issues with your SPF and DKIM configurations. Look for unauthorized sending sources and investigate any emails that are failing authentication.
  • Gradually Increase the Policy: Once you’re confident that your SPF and DKIM configurations are correct and that you’re not inadvertently blocking legitimate emails, you can gradually increase the policy to p=quarantine and then p=reject.
  • Forensic Reports (ruf): Forensic reports can provide valuable insights into specific emails that are failing authentication. However, be aware that these reports may contain sensitive information, so handle them with care. Also, not all mail providers support sending forensic reports.
  • Alignment Modes (adkim, aspf): Understanding alignment is critical. Strict alignment requires an exact match between the domain in the From header and the domain used for SPF/DKIM. Relaxed alignment allows for subdomain matching, which is often necessary when using services that send emails on behalf of your domain.
Example: DMARC Policy Progression
  • Phase 1: Monitoring (p=none): Start with v=DMARC1; p=none; rua=mailto:dmarc-reports@example.com;. Monitor the reports for a few weeks or months to get a clear understanding of your email traffic.
  • Phase 2: Quarantine (p=quarantine): Once you’re confident, change the policy to v=DMARC1; p=quarantine; rua=mailto:dmarc-reports@example.com;. This will cause emails that fail authentication to be moved to the spam folder. Continue monitoring the reports.
  • Phase 3: Reject (p=reject): Finally, change the policy to v=DMARC1; p=reject; rua=mailto:dmarc-reports@example.com;. This will cause emails that fail authentication to be rejected entirely. Continue monitoring the reports.
Quote: “DMARC is not a ‘set it and forget it’ solution. It requires ongoing monitoring and adjustments to ensure it’s effectively protecting your domain and not inadvertently blocking legitimate emails.” – Security Expert

Testing and Troubleshooting Authentication Records

After implementing SPF, DKIM, and DMARC, it’s essential to test your configuration to ensure it’s working correctly. This involves verifying that your DNS records are properly configured and that emails are being authenticated as expected. Troubleshooting issues can be challenging, but several tools and techniques can help. Tools for Testing Authentication Records Several online tools can help you verify your SPF, DKIM, and DMARC records:
  • MXToolbox: Provides a suite of tools for checking DNS records, including SPF, DKIM, and DMARC.
  • DMARC Analyzer: Offers a DMARC record checker and reporting tools.
  • EasyDMARC: Provides a user-friendly interface for managing and monitoring DMARC.
  • Kitterman’s SPF Record Testing Tool: A simple tool for validating SPF records. http://www.kitterman.com/spf/validate.html
These tools can help you identify syntax errors, missing records, and other issues with your authentication configurations. Verifying Email Authentication Manually You can also manually verify email authentication by examining the headers of emails you send. Most email clients allow you to view the full email headers. Look for the following:
  • Received-SPF: This header indicates the result of the SPF check. It should show “pass” if the email passed the SPF check.
  • Authentication-Results: This header contains the results of both SPF and DKIM checks. Look for entries indicating “spf=pass” and “dkim=pass”.
  • DMARC: This header may be present if the receiving server performed a DMARC check. It will indicate the DMARC policy that was applied.
Example: Email Headers Showing Authentication Results
Received-SPF: pass (google.com: domain of sender@example.com designates 203.0.113.10 as permitted sender) client-ip=203.0.113.10;
Authentication-Results: mx.google.com;
       dkim=pass header.i=@example.com header.s=default header.b=abcdefg;
       spf=pass (google.com: domain of sender@example.com designates 203.0.113.10 as permitted sender) smtp.mailfrom=sender@example.com;
       dmarc=pass (p=none sp=none dis=none) header.from=example.com
In this example, the email passed SPF, DKIM, and DMARC checks. Troubleshooting Common Issues
  • SPF Fails:
    • Problem: SPF record is missing or incorrect.
    • Solution: Double-check the syntax of your SPF record. Ensure that all authorized sending sources are included. Use an SPF record checker to identify any errors. Pay attention to the 10 DNS lookup limit.
  • DKIM Fails:
    • Problem: DKIM signature is invalid or missing.
    • Solution: Verify that your email server is properly configured to sign outgoing emails. Ensure that the public key in your DNS record matches the private key used by your email server. Check the selector in the DKIM-Signature header matches the selector used in your DNS record. Check time synchronization on your mail server, as DKIM signatures are time sensitive.
  • DMARC Fails:
    • Problem: Emails fail SPF and/or DKIM checks, and the DMARC policy is set to quarantine or reject.
    • Solution: Investigate why the emails are failing SPF and/or DKIM checks. Check your SPF and DKIM configurations. Ensure that the From header domain aligns with the domain used for SPF and DKIM.
  • DMARC Reports Not Received:
    • Problem: You are not receiving DMARC reports at the specified email address.
    • Solution: Ensure that the email address in the rua tag is correct and can receive emails. Check your spam filter to ensure that DMARC reports are not being blocked. Some mail providers may not send DMARC reports, so test with multiple providers if possible.
  • DNS Propagation Issues:
    • Problem: Changes to your DNS records are not being reflected immediately.
    • Solution: DNS changes can take up to 48 hours to propagate fully. Use online DNS propagation checkers to verify that your new records are visible from different locations.
Example: Troubleshooting SPF 10 Lookup Limit If your SPF record includes too many include statements, you might exceed the 10 DNS lookup limit. This can cause SPF checks to fail. To resolve this, you can try:
  • Consolidating Includes: If possible, combine multiple include statements into a single include statement.
  • Using IP Addresses Directly: Replace include statements with direct IP addresses or ranges (ip4, ip6) if possible.
  • Flattening the SPF Record: Some tools can “flatten” your SPF record by resolving all include statements and replacing them with the corresponding IP addresses. Be careful with this approach, as it can make your SPF record very long and difficult to manage.
; Example of an SPF record exceeding the lookup limit
v=spf1 include:spf1.example.com include:spf2.example.com include:spf3.example.com include:spf4.example.com include:spf5.example.com include:spf6.example.com include:spf7.example.com include:spf8.example.com include:spf9.example.com -all

; Potential solution: Flattening the record (replace with actual IP addresses)
v=spf1 ip4:192.0.2.1 ip4:192.0.2.2 ip4:192.0.2.3 ip4:192.0.2.4 ip4:192.0.2.5 ip4:192.0.2.6 ip4:192.0.2.7 ip4:192.0.2.8 ip4:192.0.2.9 -all
Remember to test your SPF record after making any changes to ensure that it’s still working correctly.

Share this article