Skip to content

Commit

Permalink
If dnslookup return NXDOMAIN, return False
Browse files Browse the repository at this point in the history
A NXDOMAIN error from dnslookup means the domain does not exists. In such a
cas, we consider the email address as being invalid.
,
  • Loading branch information
cdevienne committed Apr 8, 2014
1 parent 25d16d8 commit 26e67ff
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion validate_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ class ServerError(Exception):

def get_mx_ip(hostname):
if hostname not in MX_DNS_CACHE:
MX_DNS_CACHE[hostname] = DNS.mxlookup(hostname)
try:
MX_DNS_CACHE[hostname] = DNS.mxlookup(hostname)
except ServerError, e:
if e.rcode == 3: # NXDOMAIN (Non-Existent Domain)
MX_DNS_CACHE[hostname] = None
else:
raise

return MX_DNS_CACHE[hostname]

Expand Down Expand Up @@ -119,6 +125,8 @@ def validate_email(email, check_mx=False, verify=False, debug=False, smtp_timeou
DNS.DiscoverNameServers()
hostname = email[email.find('@') + 1:]
mx_hosts = get_mx_ip(hostname)
if mx_hosts is None:
return False
for mx in mx_hosts:
try:
smtp = smtplib.SMTP(timeout=smtp_timeout)
Expand Down

0 comments on commit 26e67ff

Please sign in to comment.