UNPKG

2.64 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.verifyEmail = exports.isFreeEmail = exports.isDisposableEmail = void 0;
4const smtp_1 = require("./smtp");
5const dns_1 = require("./dns");
6const validator_1 = require("./validator");
7let disposableEmailProviders;
8let freeEmailProviders;
9function isDisposableEmail(emailOrDomain) {
10 let [_, emailDomain] = emailOrDomain === null || emailOrDomain === void 0 ? void 0 : emailOrDomain.split('@');
11 if (!emailDomain) {
12 emailDomain = _;
13 }
14 if (!emailDomain) {
15 return false;
16 }
17 if (!disposableEmailProviders) {
18 disposableEmailProviders = require('./disposable-email-providers.json');
19 }
20 return emailDomain && disposableEmailProviders.includes(emailDomain);
21}
22exports.isDisposableEmail = isDisposableEmail;
23function isFreeEmail(emailOrDomain) {
24 let [_, emailDomain] = emailOrDomain === null || emailOrDomain === void 0 ? void 0 : emailOrDomain.split('@');
25 if (!emailDomain) {
26 emailDomain = _;
27 }
28 if (!emailDomain) {
29 return false;
30 }
31 if (!freeEmailProviders) {
32 freeEmailProviders = require('./free-email-providers.json');
33 }
34 return emailDomain && freeEmailProviders.includes(emailDomain);
35}
36exports.isFreeEmail = isFreeEmail;
37const logMethod = console.debug;
38async function verifyEmail(params) {
39 const { emailAddress, timeout = 4000, verifyMx = false, verifySmtp = false, debug = false, smtpPort = 25 } = params;
40 const result = { validFormat: false, validMx: null, validSmtp: null };
41 const log = debug ? logMethod : (...args) => { };
42 let mxRecords;
43 if (!(0, validator_1.isValidEmail)(emailAddress)) {
44 log('Failed on wellFormed check');
45 return result;
46 }
47 const [local, domain] = emailAddress.split('@');
48 if (!domain) {
49 log('Failed on wellFormed check');
50 return result;
51 }
52 result.validFormat = true;
53 // save a DNS call
54 if (!verifyMx && !verifySmtp)
55 return result;
56 try {
57 mxRecords = await (0, dns_1.resolveMxRecords)(domain);
58 log('Found MX records', mxRecords);
59 }
60 catch (err) {
61 log('Failed to resolve MX records', err);
62 mxRecords = [];
63 }
64 if (verifyMx || verifySmtp) {
65 result.validMx = mxRecords && mxRecords.length > 0;
66 }
67 if (verifySmtp) {
68 result.validSmtp = await (0, smtp_1.verifyMailboxSMTP)({
69 local,
70 domain,
71 mxRecords,
72 timeout,
73 debug,
74 port: smtpPort,
75 });
76 }
77 return result;
78}
79exports.verifyEmail = verifyEmail;