· 9 years ago · Nov 14, 2016, 10:12 PM
1import re
2import dns.resolver # Requires dnspython
3
4email_host_regex = re.compile(".*@(.*)$")
5gmail_servers_regex = re.compile("(.google.com.|.googlemail.com.)$", re.IGNORECASE)
6
7def is_gmail(email):
8 """ Returns True if the supplied Email address is a @gmail.com Email or is a Google Apps for your domain - hosted Gmail address
9Checks are performed by checking the DNS MX records """
10 m = email_host_regex.findall(email)
11 if m and len(m) > 0:
12 host = m[0]
13 if host and host != '':
14 host = host.lower()
15
16 if host == "gmail.com":
17 return True
18 else:
19 try:
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 except dns.resolver.NXDOMAIN:
26 return False
27
28 return False
29
30print(is_gmail("user@foodomain223.com"))