Smart Systems for Home

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

Verify SPF, DKIM & DMARC for Node.js Mail Domain

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to verify that your domain’s SPF, DKIM, and DMARC records are correctly set up for sending email from a Node.js application. You’ll check DNS records, validate their syntax, and test authentication results to ensure your emails land in the inbox instead of spam.

What You'll Need

  • A domain name with DNS management access (e.g., through your hosting provider or DNS registrar).
  • Node.js installed on your machine (for running verification scripts).
  • Access to a terminal/command prompt.
  • If you use a third-party email service (like SendGrid, AWS SES, or Mailgun), you'll need the DKIM selector they provide.

Step-by-Step

  1. Identify your sending domain and service.
    Determine the exact domain you send from (e.g., example.com). If you use an email service, note the DKIM selector (often something like default, k1, or s1) and the SPF record they recommend.
  2. Check your current SPF record.
    Use a DNS lookup tool or the command line to fetch the TXT record for your domain. In a terminal, run:
    nslookup -type=TXT example.com or dig TXT example.com
    The SPF record starts with v=spf1. It lists authorized senders (like include:spf.example.com) and ends with ~all or -all.
  3. Verify SPF syntax.
    Ensure the record meets these rules:
    • Starts with v=spf1.
    • Uses only allowed mechanisms: ip4, ip6, include, a, mx, exists, ptr (deprecated).
    • Has no more than 10 DNS lookups (count each include, a, mx, ptr as one).
    • Ends with a qualifier like ~all (soft fail) or -all (hard fail).
  4. Check your DKIM record.
    DKIM records are stored as TXT records under a subdomain like selector._domainkey.example.com. Use your email service’s selector. For example, if the selector is s1, run:
    dig TXT s1._domainkey.example.com
    The record should contain v=DKIM1; k=rsa; p=... where p= is a long base64 public key.
  5. Verify DKIM key length and format.
    Ensure the record starts with v=DKIM1, the key type is k=rsa, and the p= value is a valid base64 string (typically 2048 bits, about 400+ characters). Some providers use k=ed25519 – that’s fine too.
  6. Check your DMARC record.
    DMARC is stored as a TXT record on _dmarc.example.com. Run:
    dig TXT _dmarc.example.com
    The record should look like: v=DMARC1; p=quarantine; rua=mailto:dmarc@example.com. It must start with v=DMARC1.
  7. Validate DMARC policy and alignment.
    Check that p= is one of none, quarantine, or reject. Also look for sp= for subdomains and adkim/aspf alignment settings. For strict alignment, set adkim=s and aspf=s.
  8. Use Node.js to programmatically verify.
    Install the dns module (built-in) and write a script to fetch and parse the records. Example using dns.promises:
    const dns = require('dns').promises;
    
    async function getTXT(hostname) {
      try {
        const records = await dns.resolveTxt(hostname);
        return records.map(r => r.join(''));
      } catch (err) {
        return [];
      }
    }
    
    (async () => {
      const domain = 'example.com';
      const spf = await getTXT(domain);
      const dkim = await getTXT('s1._domainkey.' + domain);
      const dmarc = await getTXT('_dmarc.' + domain);
      console.log('SPF:', spf);
      console.log('DKIM:', dkim);
      console.log('DMARC:', dmarc);
    })();
      
    Run it with node check.js.
  9. Test email authentication with a real send.
    Send a test email from your Node.js app (using Nodemailer or your service’s API) to a Gmail or Outlook address. Open the email’s full headers and look for Received-SPF, DKIM-Signature, and Authentication-Results lines. They should show pass.
  10. Use online validation tools.
    Run your domain through free tools like MXToolbox or DKIMvalidator. They give a detailed breakdown of each record’s correctness.

Common Mistakes

  • Missing trailing dot in DNS lookups – some tools require a dot at the end (e.g., example.com.).
  • SPF record with multiple TXT records – your domain should have only one SPF record. If you have several, they may conflict.
  • Exceeding SPF 10-lookup limit – too many include statements cause SPF to fail.
  • DKIM selector wrong – using the wrong selector leads to a missing record. Check your email service’s documentation.
  • DMARC policy too strict too soon – starting with p=reject can block legitimate email if SPF/DKIM aren’t fully aligned.
  • Forgetting to update DMARC after changing SPF – alignment may break.

Tips

  • Start with DMARC policy p=none to monitor, then move to quarantine, then reject.
  • Use a dedicated subdomain for bulk email (e.g., mail.example.com) to protect your main domain’s reputation.
  • Regularly monitor DMARC reports (the rua address) to see authentication failures.
  • Keep your DKIM keys long (2048-bit RSA) and rotate them periodically.
  • Use a script to automate checks during deployment.

Troubleshooting

SPF fails – Check for typos in the record. Ensure your sending IP or include is correct. Use a tool to count DNS lookups.

DKIM fails – Verify the selector and domain exactly. Ensure the public key is correctly copied from your email service. Some providers require you to remove line breaks in the key.

DMARC fails – Check alignment. If SPF passes but the domain in the envelope doesn’t match your From domain, set aspf=r (relaxed). Similarly for DKIM, use adkim=r.

Emails still go to spam – Even with all records passing, content and sender reputation matter. Try warming up the domain and sending to a test list.

DNS propagation delay – Changes can take up to 48 hours. Use a global DNS checker to see if records are live everywhere.

Frequently Asked Questions

Can I have multiple SPF records?

No, you should have exactly one SPF record. Multiple records can cause failures. Combine all include statements into a single record.

What is a DKIM selector?

A selector is a label that lets you have multiple DKIM keys for the same domain, allowing rotation. It’s part of the DNS record name (e.g., s1._domainkey).

What does DMARC alignment mean?

Alignment checks whether the domain in the From header matches the domain used in SPF or DKIM. Relaxed alignment allows subdomains; strict requires exact match.

How long does DNS propagation take?

Typically a few minutes to 48 hours. Use a global DNS checker to see when your records are visible worldwide.

Do I need DMARC if I have SPF and DKIM?

Yes, DMARC tells receiving servers what to do if SPF or DKIM fail. Without it, your emails may still be spoofed.

Can I use a third-party service’s SPF include?

Yes, that’s standard. For example, if you use SendGrid, you add include:sendgrid.net to your SPF record.