Collection · 10 utilities

Email utilities

Small functions for the code around sending and receiving email: SMTP replies, DMARC records, domains, masking, normalization, Retry-After, backoff and idempotency keys. Written by Oveyon for this edition, with the RFCs and API contracts they rely on.

Convert an email domain to punycode

Keep the local part and convert an international domain to its ASCII xn-- form.

function toAsciiAddress(address) { const at = address.lastIndexOf('@'); return `${address.slice…email

Get the domain of an email address

Take the part after the last @ and lowercase it, for domain checks and grouping.

function emailDomain(address) { return address.slice(address.lastIndexOf('@') + 1).toLowerCase(…email

Mask an email address for logs

Keep the first character and the domain; hide the rest of the local part.

function maskEmail(address) { return `${address.slice(0, 1)}•••${address.slice(address.lastInde…email

Normalize an email address for comparison

Trim whitespace and lowercase only the domain, preserving the local part.

function normalizeEmail(address) { const value = address.trim(); const at = value.lastIndexOf('…email

Read a Retry-After header

Turn delay-seconds or an HTTP date into milliseconds to wait, never negative.

function retryAfterMs({ retryAfter, date }) { const seconds = Number(retryAfter); return Math.m…email

Compute a backoff delay with full jitter

Double the window on every attempt, cap it, then pick a point inside it.

function backoffMs({ attempt, baseMs, capMs, random }) { return Math.round(random * Math.min(ca…email

Build an idempotency key from an event

Join a scope, a business id and a version into one stable, colon-separated key.

function idempotencyKey({ scope, id, version }) { return [scope, id, version].map(part => encod…email

Classify an SMTP reply

Read the reply code and enhanced status code, and tell a temporary failure from a permanent one.

function classifySmtpReply(reply) { const [, code, enhanced] = /^([2-5]\d{2})(?:[ -]([245]\.\d{…email

Build a DMARC record

Compose the TXT value for _dmarc.<domain> from a policy and an optional report address.

function dmarcRecord({ policy, rua }) { return ['v=DMARC1', `p=${policy}`, ...(rua ? [`rua=${ru…email

Parse a DMARC record

Split a published DMARC TXT value into its tags.

function parseDmarc(record) { return Object.fromEntries(record.split(';').map(tag => tag.trim()…email

Try a function in the local code lab, then read its limits before using it in an application.

Type two or more characters. Try “duplicate”, “webhook” or “retry”.