public
Last active

Check if an Email address is Gmail or Google Apps for your domain

  • Download Gist
gmail_email.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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("xxx@gmail.com")

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.