Skip to content

Instantly share code, notes, and snippets.

@MayankFawkes
Last active October 1, 2019 19:53
Show Gist options
  • Save MayankFawkes/3a5935c5825ce4adf9811355383a6f9a to your computer and use it in GitHub Desktop.
Save MayankFawkes/3a5935c5825ce4adf9811355383a6f9a to your computer and use it in GitHub Desktop.
verify email of gmails and some other email provider
#pip install dnspython
import re
import smtplib
import dns.resolver
fromAddress = 'noreply@verifyemailaddress.org'
regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$'
inputAddress = "YOUR EMAIL HERE"
addressToVerify = str(inputAddress)
match = re.match(regex, addressToVerify)
if match == None:
print('Bad Syntax')
raise ValueError('Bad Syntax')
splitAddress = addressToVerify.split('@')
domain = str(splitAddress[1])
print('Domain:', domain)
records = dns.resolver.query(domain, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
print("mxRecord :"+mxRecord)
server = smtplib.SMTP()
server.set_debuglevel(0)
server.connect(mxRecord)
server.helo(server.local_hostname)
server.mail(fromAddress)
code, message = server.rcpt(str(addressToVerify))
server.quit()
if code == 250:
print('Success')
else:
print('Bad')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment