Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SPF, DKIM & DMARC: Email Security Best Practices

· 8 min read · Home Security

In this article
  1. Why Email Authentication Matters
  2. SPF: Sender Policy Framework
  3. DKIM: DomainKeys Identified Mail
  4. DMARC: Domain-based Message Authentication, Reporting & Conformance
  5. How to Implement SPF, DKIM, and DMARC Correctly
  6. Common Pitfalls and Misconceptions
  7. What to Do When Email Fails Authentication
  8. Frequently Asked Questions

SPF, DKIM, and DMARC are the three DNS-based email authentication standards that together stop spammers from forging your domain. You implement all three by publishing DNS TXT records, and when configured correctly they tell receiving mail servers which senders are allowed to use your domain and what to do with messages that fail authentication. This article explains what each standard does, how they work together, and the exact steps to set them up without breaking legitimate email.

Why Email Authentication Matters

Email was designed in an era when every sender was trusted, so the protocol has no built-in way to verify that the "From" address is real. Attackers exploit this by sending messages that appear to come from your domain — a technique called spoofing. Recipients see your company name and logo, so they are more likely to open attachments, click links, or wire money. Without authentication, your domain is also more likely to land in spam folders because mailbox providers treat unauthenticated mail as suspicious.

SPF, DKIM, and DMARC each solve a different part of the problem. SPF says which IP addresses may send mail for your domain. DKIM attaches a cryptographic signature to each message that proves it wasn't altered in transit. DMARC tells receivers what to do when SPF and DKIM both fail, and it gives you visibility into who is sending mail using your domain.

SPF: Sender Policy Framework

SPF is a DNS TXT record that lists the IP addresses or hostnames authorized to send email for your domain. When a receiving server gets a message claiming to be from your domain, it looks up your SPF record and checks whether the connecting server's IP is on the list. If it isn't, the message fails the SPF check.

A typical SPF record looks like this:

v=spf1 include:_spf.google.com include:mailgun.org ~all

The record starts with v=spf1 to identify the version. Then you list mechanisms: include: pulls in another domain's SPF record (useful if you use a third-party email service), ip4: and ip6: specify individual IP addresses, and a or mx authorize the IPs that host your website or mail server. The final part is the qualifier: ~all means "softfail" (accept but mark as suspicious), -all means "hardfail" (reject), and +all means "allow everyone" — which you should never use.

The most common SPF mistake is having multiple SPF records or exceeding the 10-DNS-lookup limit. The specification allows only one SPF record per domain, and each include: or redirect: counts as a lookup. If you hit the limit, some receivers will ignore the record entirely, making your domain more spoofable.

DKIM: DomainKeys Identified Mail

DKIM uses public-key cryptography to sign each outgoing message. Your email server or sending service generates a private key and a public key. The private key signs the message header and body, and the public key is published in your DNS as a TXT record. When a receiving server gets a signed message, it fetches your public key and verifies the signature. If the message was altered in transit, the signature fails.

A DKIM DNS record looks like this (the selector is the subdomain prefix):

default._domainkey.yourdomain.com.  TXT  "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC..."

The selector (default in this example) is an arbitrary label you choose, and it must match the selector your sending server uses. Many email services let you generate the key pair and give you the exact DNS record to publish. The public key is long, so it's normal for the TXT value to be several hundred characters.

DKIM's strength is that it survives forwarding. SPF often fails when a message is forwarded because the forwarding server's IP isn't in the original sender's SPF record. DKIM, however, travels with the message, so the signature remains valid even after forwarding — as long as the forwarder doesn't modify the signed parts.

DMARC: Domain-based Message Authentication, Reporting & Conformance

DMARC ties SPF and DKIM together and tells receivers what to do when both fail. It also generates reports that show you who is sending mail using your domain, which helps you detect spoofing attempts and legitimate services you forgot to authenticate.

A DMARC DNS record is published at _dmarc.yourdomain.com and looks like this:

v=DMARC1; p=quarantine; rua=mailto:dmarc@yourdomain.com; pct=100

The key tags are:

  • v=DMARC1 — the version, always this value.
  • p= — the policy: none (monitor only), quarantine (send to spam), or reject (block the message).
  • rua= — the email address where you want aggregate reports sent.
  • ruf= — the address for forensic reports (optional).
  • pct= — what percentage of failing messages the policy applies to (useful for gradual rollout).
  • sp= — policy for subdomains if you want it to differ from the main domain.

DMARC passes if the message passes either SPF and the domain in the SPF record aligns with the From address, or DKIM and the DKIM signing domain aligns with the From address. Alignment means the domain used in SPF or DKIM is the same as (or a subdomain of) the address in the "From" header. This prevents an attacker from using a different domain that happens to pass SPF or DKIM.

How to Implement SPF, DKIM, and DMARC Correctly

Rolling out all three requires coordination with your email providers. Here's a step-by-step approach:

  1. Inventory your sending sources. List every service that sends email for your domain: your own mail server, Google Workspace, Microsoft 365, a marketing platform like Mailchimp, transactional email services, etc. Each one needs to be included in your SPF record or have its own DKIM key.
  2. Publish your SPF record. Start with v=spf1, add include: statements for each third-party service, and end with ~all during testing. Verify the record with a tool like MXToolbox or dig.
  3. Enable DKIM for each service. Log into each provider's admin console, generate a key pair, and publish the TXT record they give you. Test with a tool like DKIMvalidator to confirm the signature verifies.
  4. Publish a DMARC record with policy p=none. This lets you collect reports without affecting delivery. Set rua= to an address you monitor.
  5. Monitor reports for a week or two. Look for messages that fail authentication. If you see legitimate services failing, fix their SPF or DKIM setup before tightening the policy.
  6. Gradually increase the policy. Move from p=none to p=quarantine with pct=50, then to pct=100, and finally to p=reject once you're confident all legitimate mail authenticates.

If you use a subdomain for marketing emails (e.g., marketing.yourdomain.com), you can publish a separate DMARC record for that subdomain with a different policy. This is useful because marketing mail often has lower authentication rates, and you don't want it to affect your main domain's reputation.

Common Pitfalls and Misconceptions

One of the biggest misconceptions is that SPF alone is enough. SPF fails when a message is forwarded, and it doesn't protect against subdomain spoofing if you haven't published a DMARC record for subdomains. Another misconception is that DMARC rejects messages that fail SPF or DKIM individually — it only rejects messages that fail both and don't align.

Other common mistakes include:

  • Using +all in SPF — this authorizes every IP and makes SPF useless.
  • Publishing multiple SPF records — receivers may reject all of them.
  • Forgetting to include all sending services — a missing include: causes legitimate mail to fail SPF.
  • Setting DMARC to reject too early — you could block legitimate mail that hasn't been authenticated yet.
  • Ignoring DMARC reports — the reports are your only way to see what's failing and why.

Also note that DMARC does not encrypt email. It only authenticates the sender. If you need confidentiality, you need TLS in transit and end-to-end encryption for the content.

What to Do When Email Fails Authentication

When you see a failure in your DMARC reports, the first step is to identify which authentication mechanism failed. If SPF fails, check whether the sending IP is in your SPF record. If it's a legitimate service, add the appropriate include:. If DKIM fails, verify that the selector in the message header matches the selector in your DNS record, and that the public key is correct. Sometimes a service rotates keys, and you need to update your DNS.

If both SPF and DKIM fail, the message is likely spoofed. DMARC's policy will handle it — with quarantine it goes to spam, with reject it's blocked. If you're still in p=none, you'll see the attempts in your reports, which helps you build a case for tightening the policy.

Occasionally, a legitimate sender will fail because of forwarding. If a recipient's mail server forwards your message to another provider, the forwarding server's IP won't be in your SPF record, and the DKIM signature may be stripped if the forwarder modifies the message. DMARC handles this by allowing alignment — if the DKIM signature survives, the message passes. If not, you may need to ask your recipients to add you to their safe sender list, but that's a temporary fix.

Frequently Asked Questions

Do I need all three: SPF, DKIM, and DMARC?

Yes. SPF and DKIM each provide a layer of authentication, but DMARC is what actually stops spoofing by telling receivers to reject or quarantine unauthenticated mail. Without DMARC, a message that fails SPF and DKIM might still be delivered. With DMARC, you control the outcome.

What happens if my SPF record is too long?

If your SPF record requires more than 10 DNS lookups, receivers may return a "permerror" and treat the record as invalid. This means all mail from your domain fails SPF. To fix it, consolidate services, use ip4: for direct IPs, or create a subdomain for some senders.

Can I use DMARC without publishing an SPF or DKIM record?

Technically yes, but it's pointless. DMARC only works if at least one of SPF or DKIM is in place. If neither is published, all messages fail DMARC, and your policy will block all your email.

How long does it take for DMARC changes to take effect?

DNS changes propagate in minutes to hours, but many receiving servers cache DNS records for up to 24 hours. It's safe to wait a day or two before expecting the new policy to be fully enforced.

What is the difference between DMARC alignment and SPF/DKIM passing?

SPF or DKIM can pass, but if the domain in the SPF record or the DKIM signature doesn't match the From address, DMARC still fails. This is called alignment. For example, if you send from news@yourdomain.com but the DKIM signature is from mailer.thirdparty.com, DMARC fails unless you have a subdomain policy that allows it.

Will DMARC stop all phishing emails?

No. DMARC stops spoofing of your exact domain, but attackers can use lookalike domains (e.g., yourdomain.co instead of yourdomain.com) or display names that mimic yours. DMARC also doesn't prevent compromised accounts from sending malicious mail. It's a critical layer, but not a complete security solution.