Verify SPF, DKIM & DMARC for Node.js Mail Domain
In this article
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
- 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 likedefault,k1, ors1) and the SPF record they recommend. - 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.comordig TXT example.com
The SPF record starts withv=spf1. It lists authorized senders (likeinclude:spf.example.com) and ends with~allor-all. - Verify SPF syntax.
Ensure the record meets these rules:
• Starts withv=spf1.
• Uses only allowed mechanisms:ip4,ip6,include,a,mx,exists,ptr(deprecated).
• Has no more than 10 DNS lookups (count eachinclude,a,mx,ptras one).
• Ends with a qualifier like~all(soft fail) or-all(hard fail). - Check your DKIM record.
DKIM records are stored as TXT records under a subdomain likeselector._domainkey.example.com. Use your email service’s selector. For example, if the selector iss1, run:dig TXT s1._domainkey.example.com
The record should containv=DKIM1; k=rsa; p=...wherep=is a long base64 public key. - Verify DKIM key length and format.
Ensure the record starts withv=DKIM1, the key type isk=rsa, and thep=value is a valid base64 string (typically 2048 bits, about 400+ characters). Some providers usek=ed25519– that’s fine too. - 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 withv=DMARC1. - Validate DMARC policy and alignment.
Check thatp=is one ofnone,quarantine, orreject. Also look forsp=for subdomains andadkim/aspfalignment settings. For strict alignment, setadkim=sandaspf=s. - Use Node.js to programmatically verify.
Install thednsmodule (built-in) and write a script to fetch and parse the records. Example usingdns.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 withnode check.js. - 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 forReceived-SPF,DKIM-Signature, andAuthentication-Resultslines. They should showpass. - 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
includestatements 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=rejectcan 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=noneto monitor, then move toquarantine, thenreject. - Use a dedicated subdomain for bulk email (e.g.,
mail.example.com) to protect your main domain’s reputation. - Regularly monitor DMARC reports (the
ruaaddress) 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.