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…emailCollection · 10 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.
Keep the local part and convert an international domain to its ASCII xn-- form.
function toAsciiAddress(address) { const at = address.lastIndexOf('@'); return `${address.slice…emailTake the part after the last @ and lowercase it, for domain checks and grouping.
function emailDomain(address) { return address.slice(address.lastIndexOf('@') + 1).toLowerCase(…emailKeep 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…emailTrim whitespace and lowercase only the domain, preserving the local part.
function normalizeEmail(address) { const value = address.trim(); const at = value.lastIndexOf('…emailTurn delay-seconds or an HTTP date into milliseconds to wait, never negative.
function retryAfterMs({ retryAfter, date }) { const seconds = Number(retryAfter); return Math.m…emailDouble 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…emailJoin 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…emailRead 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{…emailCompose 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…emailSplit a published DMARC TXT value into its tags.
function parseDmarc(record) { return Object.fromEntries(record.split(';').map(tag => tag.trim()…emailTry a function in the local code lab, then read its limits before using it in an application.