-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
136 lines (97 loc) · 5.22 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict'
const dns = require('dns')
const net = require('net')
/* http://emailregex.com/
Checking email syntax is not a trivial problem and
possibly impractical (even a 100%-accurate validation
does not account for mistakenly typing a valid, but
wrong address.)
As-simple-as-possible "real world" email syntax check:
Check that the address includes at least 1 '@'-sign,
which is not the last character (so we can extract the
domain part). No overly-complicated, unmaintainable,
only 99%-accurate regex needed. Especially since,
in practice, what is consider valid is a server-side
decision: So if we later send an invalid address to the
server (which we looked up with the domain part), and
the server replies with "553" (invalid syntax), that
should be the definitive answer. */
const mayBeValidSyntax = email =>
email.includes('@') && !email.endsWith('@')
const x = require('throw-if-missing')
module.exports = ({
sender = x`sender`,
recipient = x`recipient`,
timeout = 3000,
debug = false
} = {}) => {
return new Promise((resolve, reject) => {
if (!mayBeValidSyntax(sender)) reject('INVALID_SYNTAX')
else if (!mayBeValidSyntax(recipient)) reject('INVALID_SYNTAX')
else {
/* https://en.wikipedia.org/wiki/Email_address#Syntax
One or more '@'-signs are allowed in the local part inside of
a quoted string. Therefore, use the last '@'-sign found when
splitting the address. */
const split = recipient.split('@')
let domain = split[split.length - 1]
let local = split.splice(0, split.length - 1).join('')
/* Comments may exist at the beginning or the end
of the domain part, so remove them. */
// TODO: Implement support for nested comments
if (domain.startsWith('('))
domain = domain.substring(domain.indexOf(')') + 1, domain.length)
if (domain.endsWith(')'))
domain = domain.substring(0, domain.indexOf('('))
// TODO: Implement support for IPv4 and IPv6 address literals
dns.resolveMx(domain, (err, addresses) => {
if (err) reject(err)
else {
/* https://en.wikipedia.org/wiki/MX_record#Priority
The MX priority determines the order in which the servers
are supposed to be contacted: The servers with the highest
priority (and the lowest preference number) shall be tried
first. Node, however, erroneously labels the preference number
"priority". Therefore, sort the addresses by priority in
ascending order, and then contact the first exchange. */
const sortedAddresses = addresses.sort((a, b) => a.priority - b.priority)
const exchange = sortedAddresses[0].exchange
/* https://technet.microsoft.com/en-us/library/aa995718
Since Telnet can be used for testing SMTP, we don't even
need an external SMTP library but can simply use Node's
built-in net client. */
const TELNET_PORT = 25
const conn = net.createConnection(TELNET_PORT, exchange)
conn.setTimeout(timeout)
conn.on('error', reject)
conn.on('timeout', () => reject('TIMEOUT'))
conn.on('connect', () => {
const EOL = '\r\n'
conn.write('HELO hi' + EOL)
conn.write(`MAIL FROM: <${sender}>` + EOL)
conn.write(`RCPT TO: <${local}@${domain}>` + EOL)
conn.write('QUIT' + EOL)
conn.on('data', data => {
const response = data.toString().trim()
if (debug) console.log(response)
/* https://tools.ietf.org/html/rfc5321#section-4.2.3
A "550" indicates that the mailbox is unavailable,
cannot be found, or may not be accessed. */
if (response.startsWith('550')) resolve('NOT_FOUND')
/* https://tools.ietf.org/html/rfc5321#section-4.2.3
A "553" indicates that the mailbox name is not allowed
due to invalid syntax. (Note: This means that either
the sender's or the recipient's email address is invalid.) */
if (response.startsWith('553')) resolve('INVALID_SYNTAX')
})
/* https://tools.ietf.org/html/rfc1123#section-5.2.7
Cannot check whether an address actually exists:
Servers may send "250 OK" false positives to prevent
malware from discovering all available addresses. */
conn.on('end', () => resolve('MAY_EXIST'))
})
}
})
}
})
}