Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions validate_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class ServerError(Exception):
# even when it's not strictly necessary. This way we don't forget
# when it is necessary.)
#
# Add-on:
# catchall option added to check if this mailhost is
# serving catch-all addresses. NOT FOUL PROOF
#
WSP = r'[\s]' # see 2.2.2. Structured Header Field Bodies
CRLF = r'(?:\r\n)' # see 2.2.3. Long Header Fields
NO_WS_CTL = r'\x01-\x08\x0b\x0c\x0f-\x1f\x7f' # see 3.2.1. Primitive Tokens
Expand Down Expand Up @@ -109,7 +113,7 @@ def get_mx_ip(hostname):
return MX_DNS_CACHE[hostname]


def validate_email(email, check_mx=False, verify=False, debug=False, smtp_timeout=10):
def validate_email(email, check_mx=False, verify=False, catchall=False, debug=False, smtp_timeout=10):
"""Indicate whether the given string is a valid email address
according to the 'addr-spec' portion of RFC 2822 (see section
3.4.1). Parts of the spec that are marked obsolete are *not*
Expand Down Expand Up @@ -155,6 +159,19 @@ def validate_email(email, check_mx=False, verify=False, debug=False, smtp_timeou
continue
smtp.mail('')
status, _ = smtp.rcpt(email)
if catchall:
import random, string
smtp.quit()
smtp.connect(mx[1])
smtp.helo()
smtp.mail('')
status, _ = smtp.rcpt(''.join(random.choice(string.lowercase) for i in range(25)) + '@' + email.split('@')[1])
if status == 250:
smtp.quit()
return False
if status == 550:
smtp.quit()
return True
if status == 250:
smtp.quit()
return True
Expand Down Expand Up @@ -193,9 +210,15 @@ def validate_email(email, check_mx=False, verify=False, debug=False, smtp_timeou
else:
validate = False

noca = raw_input('Accept catch-all? [yN] ')
if noca.strip().lower() == 'y':
noca = False
else:
noca = True

logging.basicConfig()

result = validate_email(email, mx, validate, debug=True, smtp_timeout=1)
result = validate_email(email, mx, validate, noca, debug=False, smtp_timeout=1)
if result:
print("Valid!")
elif result is None:
Expand Down