Created
July 17, 2011 07:45
-
-
Save erans/1087324 to your computer and use it in GitHub Desktop.
Check if an Email address is Gmail or Google Apps for your domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
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: | |
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 | |
return False | |
print is_gmail("[email protected]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should change line 6 to: gmail_servers_regex = re.compile("(.google.com.|.googlemail.com.|.psmtp.com.)$", re.IGNORECASE)
Some Google Apps domains use Postini.