October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

SPF vs. DKIM for Node.js Email: Why DKIM Can Help When Mail Goes to Spam

SPF authorizes sending hosts; DKIM signs messages. Learn when DKIM is the more durable signal for Node.js email, how to configure it in Nodemailer, and how to troubleshoot spam placement.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For Node.js mail, SPF and DKIM do different jobs, and you generally need both. SPF authorizes sending hosts in DNS; DKIM adds a signature to each message that the recipient can verify. DKIM is often the more durable signal when your app uses multiple providers, shared infrastructure, or changing IP addresses, but it cannot guarantee inbox placement. Keep SPF accurate and add DMARC so receivers can apply policy and check alignment.

What SPF and DKIM actually check

Question SPF DKIM
What is checked? Whether the host connecting to send the message is authorized by the domain’s DNS policy. Whether a cryptographic signature for selected message headers and the body can be verified.
Where is the DNS record? In the sending domain’s SPF TXT record. At <selector>._domainkey.<domain>, where the receiver looks up the public key.
What does it help establish? That a particular sending host is authorized for the domain. That the signed parts of the message have a verifiable signature associated with the signing domain.
Typical failure in Node.js delivery A provider or sender is missing from the SPF policy, or forwarding changes the apparent sending host. The selector or public key is published incorrectly, the private and public keys do not match, or infrastructure changes signed headers.
Node.js action Maintain one accurate SPF policy that covers every legitimate sender. Configure Nodemailer’s domainName, keySelector, and privateKey, then publish the matching public key.

SPF does not sign the message or protect its contents. DKIM does not authorize every server that may send for your domain. They are complementary checks, not interchangeable alternatives.

As an Amazon Associate I earn from qualifying purchases.

Is DKIM better than SPF when Node.js mail goes to spam?

DKIM is often the better first fix when your application sends through several providers, shared mail infrastructure, or systems with changing IP addresses. Its signature accompanies the message, whereas SPF evaluates the host seen by the receiving server. Forwarding can therefore complicate SPF results, and an SPF policy that omits a legitimate provider can fail for mail sent through that provider.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

That does not make SPF optional. Keep a single accurate SPF policy and include every service that legitimately sends mail for the domain. Google warns that messages from third-party senders missing from SPF are more likely to be marked as spam. DKIM signing also depends on correct DNS publication and on downstream systems not changing signed headers.

Neither SPF nor DKIM passing guarantees that a message will reach the inbox. Google says authenticated messages help protect recipients from spoofing and phishing, and that authentication makes mail less likely to be rejected or marked as spam. Content, list hygiene, reverse DNS, TLS, and sender reputation also matter.

Gmail’s sender requirements

These requirements are specific to Gmail and may change. Google’s sender guidance says all senders need SPF or DKIM. Senders that send more than 5,000 messages per day to Gmail have additional requirements: SPF, DKIM, and DMARC. Google’s bulk-sender requirements took effect February 1, 2024.

  • For bulk senders, configure SPF and DKIM so they are aligned with each other at the organizational level, and use DMARC.
  • For personal Gmail delivery, Google’s current guidance sets a 1,024-bit minimum DKIM key and recommends a 2,048-bit key when supported.
  • Google’s 2024 bulk-sender guidance cites a 0.30% user-reported spam-rate ceiling. Monitor your complaint and spam rates rather than treating authentication as a substitute for good list practices.

DMARC gives receiving systems a policy and checks whether SPF or DKIM authenticates in alignment with the domain visible in the message’s From: address. Passing a check for a different domain is not the same as alignment.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to add DKIM signing in Nodemailer

Nodemailer supports DKIM signing for a whole transport or an individual message. For transport-wide signing, provide your signing domain, selector, and private key when creating the transporter:

const transporter = nodemailer.createTransport({
  host: "smtp.example.com",
  port: 465,
  secure: true,
  dkim: {
    domainName: "example.com",
    keySelector: "2017",
    privateKey: fs.readFileSync("./dkim-private.pem", "utf8"),
  },
});

With these example values, publish the corresponding public key as a TXT record at 2017._domainkey.example.com. The selector in Nodemailer must match the selector in that DNS name, and the published public key must match the private key used to sign. Keep the private key available to the sending application but do not publish it.

Nodemailer recommends checking the DNS record with dig TXT 2017._domainkey.example.com. DNS providers may display or return TXT data in chunks; verify the full value rather than assuming one displayed string is the entire record.

If an SMTP provider rewrites Date or Message-ID after Nodemailer signs the message, DKIM verification can fail. Nodemailer’s DKIM options include skipFields for excluding mutable headers from signing; use it only for headers your delivery path actually changes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why SPF can pass while Gmail still marks mail as spam

An SPF pass means the connecting host passed the domain’s SPF policy; it does not establish that the message is desirable, that DKIM passes, or that DMARC alignment succeeds. Inbox placement is a broader decision than a single authentication result. A message can pass SPF and still be filtered because of other authentication results, sender reputation, content, or recipient complaints.

Check the receiving message’s Authentication-Results header to see the separate SPF, DKIM, and DMARC outcomes. Interpret each result in context: identify which domain each mechanism authenticated and whether it aligns with the visible From: domain.

Troubleshoot Node.js mail in this order

  1. Inspect the received message. Open its full headers and find Authentication-Results. Record the SPF, DKIM, and DMARC results and the domains involved.
  2. Check SPF policy coverage. Confirm there is exactly one SPF TXT policy for the sending domain and that it includes every legitimate Node.js sender and provider. A missing third-party sender can make its messages more likely to land in spam.
  3. Verify DKIM DNS and key selection. Query the selector TXT record, confirm the name matches the Nodemailer keySelector, and ensure the public key corresponds to the private key configured in the application.
  4. Check for post-signing changes. Find out whether an SMTP relay or other downstream system rewrites signed headers such as Date or Message-ID. Exclude headers with Nodemailer’s skipFields option if they are in fact being changed.
  5. Review DMARC and alignment. Confirm the authenticated SPF or DKIM domain aligns with the visible From: domain and that your DMARC policy reflects the intended handling.
  6. Investigate non-authentication causes. Review message content, list hygiene, reverse DNS, TLS, and sender reputation; SPF and DKIM passes alone do not guarantee inbox placement.

When diagnosing DNS in Node.js, dns.resolveTxt() returns a two-dimensional array because a single TXT record can be split into multiple chunks. Join or otherwise interpret the chunks belonging to each record before evaluating its policy; treating each chunk as a separate record can lead to a mistaken diagnosis.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.