Skip to content

Instantly share code, notes, and snippets.

@brunodasilvalenga
Last active May 4, 2016 17:14
Show Gist options
  • Save brunodasilvalenga/d6518cde0dc17899bcc14f14b9e141b8 to your computer and use it in GitHub Desktop.
Save brunodasilvalenga/d6518cde0dc17899bcc14f14b9e141b8 to your computer and use it in GitHub Desktop.
Verify email box exists
#Verify email box exists
#Author: Bruno da Silva Valenga - <brunodasilvalenga.com.br>
#Created: 08/25/2014
#Usage: script.py [email protected]
#Install lib DNS: https://github.com/rthalley/dnspython
import socket, smtplib, re, sys, dns.resolver
addressToVerify = sys.argv[1]
match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', addressToVerify)
domain = addressToVerify.split("@")[1]
if match == None:
print('Bad Syntax')
raise ValueError('Bad Syntax')
records = dns.resolver.query(domain, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
# Get local server hostname
host = socket.gethostname()
# SMTP lib setup (use debug level for full output)
server = smtplib.SMTP()
server.set_debuglevel(0)
# SMTP Conversation
server.connect(mxRecord)
server.helo(host)
server.mail(addressToVerify)
code, message = server.rcpt(str(addressToVerify))
server.quit()
# Assume 250 as Success
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