From ef0191e573d30247bf7ee4edeb8b7e1fd221f81e Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Tue, 12 Dec 2023 19:20:08 -0600 Subject: [PATCH] fix: smtp timeout --- src/Effect.Email.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Effect.Email.js b/src/Effect.Email.js index 444eda2..8eca8aa 100644 --- a/src/Effect.Email.js +++ b/src/Effect.Email.js @@ -1,4 +1,35 @@ -import { checkEmail } from 'qed-mail' +import { checkSyntax, checkMX, checkSMTP } from 'qed-mail' + +const DEFAULT_RESULT = { + reachable: false, + + syntax: { valid: false }, + mx: { valid: false }, + smtp: { valid: false }, +}; + +/** @type {(_: string) => Promise} */ +async function checkEmail(email) { + const result = { email, ...DEFAULT_RESULT }; + + const syntax = checkSyntax(email); + if (!syntax.valid) { + return { ...result, syntax }; + } + + const mx = await checkMX(syntax.domain || ''); + if (!mx.valid || !mx.mxRecords) { + return { ...result, syntax, mx }; + } + + const records = mx.mxRecords.sort((a, b) => a.priority - b.priority); + const smtp = await checkSMTP(email, records[0].exchange, 500); + if (!smtp.valid) { + return { ...result, syntax, mx, smtp }; + } + + return { ...result, reachable: true, syntax, mx, smtp }; +} /** @type {(_: string) => () => Promise} */ export const checkEmailImpl = s => () => checkEmail(s)