DEV Community

How DNS Records (SPF, DKIM, DMARC) Actually Work for Email Deliverability

If you've ever built an app that sends emails - transactional, marketing, or outreach - you've probably hit the "why are my emails going to spam?" wall.

The answer almost always comes down to three DNS records: SPF, DKIM, and DMARC. Here's a practical breakdown of what each one does and how to set them up correctly.

SPF (Sender Policy Framework)

SPF tells receiving mail servers which IP addresses are allowed to send email on behalf of your domain.

example.com. IN TXT "v=spf1 include:_spf.google.com include:sendgrid.net ~all"

How it works:

  • You send an email from h****@example.com
  • The receiving server looks up the SPF record for example.com
  • It checks if the sending server's IP is in the allowed list
  • If not, the email gets flagged or rejected

Common mistakes:

  • Too many DNS lookups: SPF allows a maximum of 10 DNS lookups. Each include: counts as one. Flatten your record if you're close to the limit.
  • Using +all: This tells the world "anyone can send as me." Always use ~all (softfail) or -all (hardfail).
  • Forgetting third-party senders: If you use Mailchimp, SendGrid, or any email service, their servers need to be in your SPF record.

DKIM (DomainKeys Identified Mail)

DKIM adds a cryptographic signature to your email headers that proves the email hasn't been tampered with in transit.

selector._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCS..."

How it works:

  • Your mail server signs each outgoing email with a private key
  • The signature is added to the email header as DKIM-Signature
  • The receiving server retrieves your public key from DNS
  • It verifies the signature matches the email content

Setup steps:

  1. Generate a key pair (most email providers do this for you)
  2. Add the public key as a TXT record in your DNS
  3. Configure your mail server to sign outgoing emails with the private key

Pro tip: Rotate your DKIM keys every 6-12 months. If a key is compromised, an attacker could send authenticated emails as your domain.

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

DMARC ties SPF and DKIM together and tells receiving servers what to do when authentication fails.

_dmarc.example.com. IN TXT "v=DMARC1; p=quarantine; rua=mailto:dma**********@example.com; pct=100"

Policy options:

  • p=none: Monitor only (good for starting out)
  • p=quarantine: Send failing emails to spam
  • p=reject: Block failing emails entirely

Recommended rollout:

  • Start with p=none and monitor reports for 2-4 weeks
  • Move to p=quarantine once you've confirmed all legitimate senders are properly authenticated
  • Graduate to p=reject for maximum protection

Checking Your Setup

You can verify your records with these tools:

  • dig command: dig TXT example.com to check SPF, dig TXT _dmarc.example.com for DMARC
  • MXToolbox: mxtoolbox.com for a visual breakdown
  • Google Admin Toolbox: Check specific message headers

Why This Matters for Developers

If you're building a SaaS product that sends emails (notifications, receipts, outreach), getting these records right is the difference between landing in the inbox and landing in spam. At getcoldmail.com, we built email generation with deliverability in mind - but the foundation always starts with your DNS setup. No amount of great copy helps if the email never reaches the inbox.

TL;DR: Set up SPF to whitelist your sending servers, DKIM to cryptographically sign your emails, and DMARC to enforce the policy. Start with p=none, monitor, then tighten. Your inbox placement rate will thank you.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.