· 9 years ago · Nov 15, 2016, 07:50 AM
1import sys
2import re
3import dns.resolver # Requires dnspython
4
5email_host_regex = re.compile(".*@(.*)$")
6gmail_servers_regex = re.compile("(.google.com.|.googlemail.com.)$", re.IGNORECASE)
7
8def is_gmail(email):
9 """ Returns True if the supplied Email address is a @gmail.com Email or is a Google Apps for your domain - hosted Gmail address
10Checks are performed by checking the DNS MX records """
11 m = email_host_regex.findall(email)
12 if m and len(m) > 0:
13 host = m[0]
14 if host and host != '':
15 host = host.lower()
16
17 if host == "gmail.com":
18 return True
19 else:
20 answers = dns.resolver.query(host, 'MX')
21 for rdata in answers:
22 m = gmail_servers_regex.findall(str(rdata.exchange))
23 if m and len(m) > 0:
24 return True
25
26 return False
27
28print is_gmail("xxx@gmail.com")