-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (66 loc) · 2.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// importing the built-in dns module.
var DNS = require('dns');
/* a function to test if email is valid or not. This follows RFC 5322 Official Standard.
refer https://tools.ietf.org/html/rfc5322 */
function validateEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
module.exports = function(emailAddress)
{
return new Promise((resolve, reject) =>
{
if (validateEmail(emailAddress))
{
var splitEmail = emailAddress.split('@');
var user = splitEmail[0];
var domain = splitEmail[1];
// refer https://nodejs.org/api/dns.html#dns_dnspromises_resolvemx_hostname
DNS.resolveMx(domain, (error, mx) =>
{
if (typeof(mx) != 'undefined')
{
if (mx)
{
/* possibility of temporary email, private organization email or lack
of usgae of that domain name. We are making use of number of records
to check for this possibility. */
var mxLength = mx.length;
if (mxLength === 1)
{
resolve({user:user, domain:domain, isEmailValid:true, mxRecords:mx, possibility:true, timeout:false});
}
else if (mxLength > 1)
{
resolve({user:user, domain:domain, isEmailValid:true, mxRecords:mx, possibility:false, timeout:false});
}
else
{
resolve({user:user, domain:domain, isEmailValid:false, mxRecords:[], possibility:true, timeout:false});
}
}
}
else if (error.code == 'ENOTFOUND')
{
resolve({user:user, domain:domain, isEmailValid:false, mxRecords:[], mxRecordExist:false, timeout:false});
}
else if (error.code == 'ENODATA')
{
resolve({user:user, domain:domain, isEmailValid:false, mxRecords:[], possibility:true, timeout:false});
}
else if (error.code == 'ETIMEOUT')
{
resolve({user:user, domain:domain, isEmailValid:false, mxRecords:[], possibility:true, timeout:true});
}
else
{
reject(new Error(error.code));
}
});
}
else
{
reject(new Error('Invalid format of email address'));
}
});
};