Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 5 min read

Test Email Address Deliverability in Node.js: MX Records, Null MX and the Retry Trap

Picture it: Black Friday weekend, your signup form is on fire, and somewhere in the flood is [email protected]. Your app cheerfully fires off a welcome email, a receipt and a password reset to a domain that has never receiv

Picture it: Black Friday weekend, your signup form is on fire, and somewhere in the flood is [email protected]. Your app cheerfully fires off a welcome email, a receipt and a password reset to a domain that has never received a message in its life. Each one bounces. Multiply that by a busy weekend and your sending reputation starts to look like a fridge after Thanksgiving: technically still working, but nobody's proud of it.

The fix starts before you send. If you want to test email address deliverability at signup, the cheapest, most honest check is DNS: does this domain actually say it accepts mail? This post builds that check in Node.js with zero dependencies, following the rules the mail RFCs actually set, and is upfront about what DNS can't tell you.

What a DNS check can (and can't) tell you

A DNS check answers one question well: can mail for this domain be delivered anywhere at all? It catches typo domains, dead domains, and domains that explicitly refuse mail.

It does not tell you whether the mailbox jane exists. Only the receiving server knows that, and it may not tell you. Probing it with an SMTP RCPT TO from your app server is a trap: plenty of servers accept every address and bounce later, some greylist you, and repeated probing from one IP looks a lot like address harvesting. Leave mailbox-level verification to a double opt-in email, which also proves the person wanted to hear from you.

The rules, straight from the RFCs

Three rules decide what your code should do:

  1. MX first. RFC 5321, section 5.1: look up MX records for the domain. If the lookup says the domain doesn't exist, that "MUST be reported as an error". If you get a temporary error, the message "MUST be queued and retried later". So a DNS timeout is not a reason to reject a signup.
  2. No MX? Use the implicit MX. Same section: if the MX list comes back empty, the address is treated as if it had an implicit MX pointing at the domain itself. A domain with only an A or AAAA record can still receive mail.
  3. Null MX means "no mail, ever". RFC 7505 defines a single MX record with preference 0 and an exchange of . as the way a domain says it does not accept email. A domain that publishes it must not publish any other MX.

And two length limits from RFC 5321, section 4.5.3.1: the local part (before the @) is at most 64 octets, and a domain at most 255.

The code (about 50 lines, no dependencies)

import { promises as dns } from "node:dns";

// Deliberately loose: the goal is to catch obvious junk, not to re-implement RFC 5322.
const SYNTAX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

// Errors that mean "ask again later", not "this address is dead".
const TEMPORARY = new Set(["ETIMEOUT", "ESERVFAIL", "ECONNREFUSED", "EAI_AGAIN"]);

export async function checkAddress(address, resolver = dns) {
  const verdict = (status, reason, extra = {}) => ({ address, status, reason, ...extra });

  if (!SYNTAX.test(address)) return verdict("reject", "bad-syntax");

  const at = address.lastIndexOf("@");
  const local = address.slice(0, at);
  const domain = address.slice(at + 1).toLowerCase().replace(/\.$/, "");

  // RFC 5321 4.5.3.1: local-part max 64 octets, domain max 255 octets.
  if (Buffer.byteLength(local) > 64) return verdict("reject", "local-part-too-long");
  if (Buffer.byteLength(domain) > 255) return verdict("reject", "domain-too-long");

  let mx = [];
  try {
    mx = await resolver.resolveMx(domain);
  } catch (err) {
    if (err.code === "ENOTFOUND") return verdict("reject", "domain-does-not-exist");
    if (TEMPORARY.has(err.code)) return verdict("retry", "dns-temporary-failure");
    if (err.code !== "ENODATA") throw err; // something we didn't plan for: surface it
  }

  // RFC 7505: a single MX with preference 0 and exchange "." means "no mail here".
  const isNullMx = (r) => r.priority === 0 && (r.exchange === "" || r.exchange === ".");
  if (mx.length === 1 && isNullMx(mx[0])) return verdict("reject", "null-mx");

  if (mx.length > 0) {
    const hosts = mx.sort((a, b) => a.priority - b.priority).map((r) => r.exchange);
    return verdict("accept", "has-mx", { hosts });
  }

  // RFC 5321 5.1: no MX records -> treat the domain itself as an implicit MX.
  for (const lookup of ["resolve4", "resolve6"]) {
    try {
      const addrs = await resolver[lookup](domain);
      if (addrs.length) return verdict("accept", "implicit-mx", { hosts: [domain] });
    } catch (err) {
      if (TEMPORARY.has(err.code)) return verdict("retry", "dns-temporary-failure");
    }
  }
  return verdict("reject", "no-mail-host");
}

The resolver is injectable, which makes it easy to test without the network. Here's what it returns against a mocked set of zones (run with Node 22):

{"address":"[email protected]","status":"accept","reason":"has-mx","hosts":["mx1.good.test","mx2.good.test"]}
{"address":"[email protected]","status":"reject","reason":"null-mx"}
{"address":"[email protected]","status":"accept","reason":"implicit-mx","hosts":["aonly.test"]}
{"address":"[email protected]","status":"reject","reason":"no-mail-host"}
{"address":"[email protected]","status":"retry","reason":"dns-temporary-failure"}
{"address":"[email protected]","status":"reject","reason":"domain-does-not-exist"}
{"address":"not-an-email","status":"reject","reason":"bad-syntax"}

Three outcomes, not two. That middle one, retry, is the one most validators get wrong.

Wiring it into a signup form

  • reject: show an inline message ("We can't deliver to that domain. Typo?"). Don't block silently; people fix typos when you tell them.
  • retry: accept the signup, flag the address, and re-check before your first send. A flaky resolver at 2 a.m. should not cost you a customer.
  • accept: send your double opt-in or welcome email. A bounce from that first message is your mailbox-level answer.

Cache results per domain for a few minutes. Your form will see gmail.com thousands of times; DNS doesn't need to.

Common mistakes

  • Rejecting on a timeout. RFC 5321 says temporary errors are retried, not reported as failures. Your validator should agree.
  • Requiring an MX record. The implicit-MX rule means A-record-only domains are legal mail destinations. Rare, but real.
  • Treating null MX as "missing MX". A null MX is a deliberate "no", so it's a hard reject, not a fallback to A records.
  • SMTP-probing from production. It's slow, unreliable against catch-all servers, and can hurt the reputation of the IP you send real mail from.
  • Over-strict regexes. + tags, subdomains and long TLDs are all valid. Keep syntax checks loose and let DNS do the real work.

Checklist

  • [ ] Loose syntax check, length limits from RFC 5321
  • [ ] MX lookup, with ENOTFOUND as reject and timeouts as retry
  • [ ] Null MX (RFC 7505) as a hard reject
  • [ ] A/AAAA fallback for the implicit MX
  • [ ] Per-domain caching
  • [ ] Double opt-in for mailbox-level proof

Want to eyeball a domain by hand first? Our free MX lookup tool shows the records this code reads. And if you're sending receipts and resets from your app, Email Digit's transactional email API for developers checks suppression on every send, so an address that already bounced doesn't get a second chance to hurt you.

FAQ

Is a DNS check enough to verify an email address?
No. It proves the domain can receive mail, not that the mailbox exists. Pair it with a confirmation email.

Should I reject signups when the DNS lookup times out?
No. Treat it as "retry": accept, flag, and re-check before the first send.

What does a null MX record look like?
A single MX with preference 0 and . as the host (RFC 7505). It means the domain accepts no mail.

I work on Email Digit. Drafted with AI assistance and checked against RFC 5321 and RFC 7505.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.