Skip to content

Instantly share code, notes, and snippets.

@mesin
Last active November 14, 2016 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mesin/7b76446b4d48aee50bd463dcb8add79b to your computer and use it in GitHub Desktop.
Save mesin/7b76446b4d48aee50bd463dcb8add79b to your computer and use it in GitHub Desktop.
import re
import dns.resolver # Requires dnspython
email_host_regex = re.compile(".*@(.*)$")
gmail_servers_regex = re.compile("(.google.com.|.googlemail.com.)$", re.IGNORECASE)
def is_gmail(email):
""" Returns True if the supplied Email address is a @gmail.com Email or is a Google Apps for your domain - hosted Gmail address
Checks are performed by checking the DNS MX records """
m = email_host_regex.findall(email)
if m and len(m) > 0:
host = m[0]
if host and host != '':
host = host.lower()
if host == "gmail.com":
return True
else:
try:
answers = dns.resolver.query(host, 'MX')
for rdata in answers:
m = gmail_servers_regex.findall(str(rdata.exchange))
if m and len(m) > 0:
return True
except dns.resolver.NXDOMAIN:
return False
return False
print(is_gmail("user@foodomain223.com"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment