// Email typo check for NEW addresses (2026-10-22, after SendGrid bounces to gmai.com / outlook.comz). // Same file lives in InstantAdPay and LinkSpin. Catches: // 1. near-misses of the big mailbox providers (gmai.com, gmial.com, hotmial.com, yaho.com) -> "Did you mean ...?" // 2. mistyped endings (.con, .cmo, .comz, .nett) -> "Did you mean ...?" // 3. domains that cannot receive mail at all (no MX and no A/AAAA record) // A DNS timeout or server error never blocks anyone (fails open). Only call it where an address enters for the // first time - existing accounts must always be able to sign in with the address they already have. const dns = require('dns').promises; const POPULAR = ['gmail.com', 'googlemail.com', 'yahoo.com', 'ymail.com', 'hotmail.com', 'outlook.com', 'live.com', 'msn.com', 'icloud.com', 'me.com', 'mac.com', 'aol.com', 'protonmail.com', 'proton.me', 'mail.com', 'gmx.com', 'gmx.de', 'yandex.com', 'yandex.ru', 'zoho.com', 'comcast.net', 'att.net', 'verizon.net', 'sbcglobal.net', 'bellsouth.net', 'cox.net', 'charter.net', 'yahoo.co.uk', 'hotmail.co.uk', 'btinternet.com', 'yahoo.fr', 'hotmail.fr', 'orange.fr', 'web.de', 't-online.de', 'libero.it', 'yahoo.com.br', 'outlook.com.br', 'rediffmail.com', 'qq.com', '163.com', 'naver.com', 'seznam.cz', 'wp.pl', 'mail.ru']; const POP = new Set(POPULAR); // endings that are typos of .com/.net/.org (real TLDs like .co, .cm, .om are deliberately NOT here) const BAD_TLD = { con: 'com', cmo: 'com', comm: 'com', comz: 'com', coom: 'com', cpm: 'com', vom: 'com', xom: 'com', ocm: 'com', cmon: 'com', 'com.com': 'com', nett: 'net', nte: 'net', ent: 'net', ogr: 'org', orgg: 'org', rog: 'org' }; // optimal string alignment distance (Damerau-Levenshtein with adjacent swaps), capped function dist(a, b) { if (Math.abs(a.length - b.length) > 2) return 9; const d = []; for (let i = 0; i <= a.length; i++) { d[i] = [i]; } for (let j = 0; j <= b.length; j++) d[0][j] = j; for (let i = 1; i <= a.length; i++) { for (let j = 1; j <= b.length; j++) { const c = a[i - 1] === b[j - 1] ? 0 : 1; d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + c); if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1); } } return d[a.length][b.length]; } // providers that only ever use ONE domain, so gmail.co / gmail.cm / icloud.net are always typos. // (outlook, hotmail, yahoo, live have real national domains like hotmail.it, so they are not listed.) const SINGLE = { gmail: 'gmail.com', googlemail: 'googlemail.com', icloud: 'icloud.com', aol: 'aol.com', protonmail: 'protonmail.com', ymail: 'ymail.com' }; const split = d => { const i = d.indexOf('.'); return i < 0 ? [d, ''] : [d.slice(0, i), d.slice(i + 1)]; }; function suggestDomain(domain) { if (POP.has(domain)) return null; const labels = domain.split('.'); const two = labels.slice(-2).join('.'), last = labels[labels.length - 1]; let d = domain; if (BAD_TLD[two]) d = labels.slice(0, -2).concat(BAD_TLD[two]).join('.'); else if (BAD_TLD[last]) d = labels.slice(0, -1).concat(BAD_TLD[last]).join('.'); if (POP.has(d)) return d; const near = nearestPopular(d); if (near) return near; return d !== domain ? d : null; // ending fixed but not a known provider: still suggest the fixed ending } function nearestPopular(domain) { const [name, suffix] = split(domain); if (SINGLE[name] && domain !== SINGLE[name]) return SINGLE[name]; let best = null, bestD = 9; for (const p of POPULAR) { const [pn, ps] = split(p); if (ps !== suffix) continue; // only compare names that share the same ending const dd = dist(name, pn); if (dd < bestD) { best = p; bestD = dd; } } // 1 edit always counts; 2 edits only for longer names, so short real domains are left alone return (bestD === 1 || (bestD === 2 && name.length >= 6)) ? best : null; } function withTimeout(promise, ms) { return Promise.race([promise, new Promise((_, rej) => setTimeout(() => rej(Object.assign(new Error('timeout'), { code: 'ETIMEOUT' })), ms))]); } // true = has somewhere to deliver, false = definitely cannot receive mail, null = could not tell (fail open) async function domainReceives(domain) { const gone = c => c === 'ENOTFOUND' || c === 'ENODATA' || c === 'ESERVFAIL_NXDOMAIN'; try { const mx = await withTimeout(dns.resolveMx(domain), 3000); if (mx && mx.length && !(mx.length === 1 && mx[0].exchange === '')) return true; // RFC 7505 null MX = no mail if (mx && mx.length) return false; } catch (e) { if (!gone(e.code)) return null; } try { await withTimeout(dns.resolve4(domain), 3000); return true; } catch (e) { if (!gone(e.code)) return null; } try { await withTimeout(dns.resolve6(domain), 3000); return true; } catch (e) { if (!gone(e.code)) return null; } return false; } // -> null when the address is fine, or { error, suggestion } to show the person async function emailIssue(email) { const e = String(email || '').trim().toLowerCase(); const at = e.lastIndexOf('@'); if (at < 1) return null; // the caller's own format check handles this const local = e.slice(0, at), domain = e.slice(at + 1); const fix = suggestDomain(domain); if (fix && fix !== domain) { const suggestion = local + '@' + fix; return { error: 'Did you mean ' + suggestion + '? Please check the email address.', suggestion }; } if (POP.has(domain)) return null; const ok = await domainReceives(domain); if (ok === false) return { error: 'The email domain "' + domain + '" cannot receive mail. Please check the address.', suggestion: null }; return null; } module.exports = { emailIssue, suggestDomain, domainReceives };