From 26e67ff5721e722168183044b34fc4ee0321649a Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Tue, 8 Apr 2014 11:59:07 +0200 Subject: [PATCH] If dnslookup return NXDOMAIN, return False A NXDOMAIN error from dnslookup means the domain does not exists. In such a cas, we consider the email address as being invalid. , --- validate_email.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/validate_email.py b/validate_email.py index fb30e59..508b8d8 100644 --- a/validate_email.py +++ b/validate_email.py @@ -90,7 +90,13 @@ MX_DNS_CACHE = {} 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] @@ -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)