-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_validation.py
50 lines (42 loc) · 1.38 KB
/
email_validation.py
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
import dns.exception
import dns.resolver
import errno
import socket
import smtplib
def email_address_exists_according_to_server(address):
"""
Returns a boolean indicating whether the given email address exists according to its mail server. This does
not actually send mail, and may give false positives or false negatives.
"""
(_, domain) = address.split('@')
try:
mx_records = dns.resolver.query(domain, 'MX')
except dns.exception.DNSException:
return False
mx_records = sorted(mx_records, key=lambda x: x.preference)
for record in mx_records:
try:
smtp = smtplib.SMTP(str(record.exchange))
except smtplib.SMTPConnectError:
continue
except socket.error as e:
if e.errno in [errno.ECONNREFUSED, errno.EHOSTUNREACH, errno.ETIMEDOUT]:
continue
raise
try:
helo = smtp.helo('example.com')
if helo[0] != 250:
return False
mailfrom = smtp.mail('[email protected]')
if mailfrom[0] != 250:
return False
rcptto = smtp.rcpt(address)
if rcptto[0] != 250:
return False
return True
finally:
try:
smtp.quit()
except smtplib.SMTPServerDisconnected:
pass
return False