Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Check if an email address exists without sending an email. Technique from:
#!/usr/bin/env python
Check that a particular email address exists.
Adam Blinkinsop <>
Checking email addresses in this way is not recommended, and will lead to
your site being listed in RBLs as a source of abusive traffic. Mail server
admins do like it when they get connections that don't result in email being
sent, because spammers often use this technique to verify email addresses.
(Incidentally, a lot of sites will accept bad addresses and only reject after
the DATA portion of the SMTP transactions, for this very reason.)
Additionally, the check is not good in most situations because it doesn't do
any verification that the owner of the email address wants to receive email
from you. It does not in any way, shape or form conform to standard confirmed
opt-in practices. Anyone who uses this technique and then begins sending
email without going through a confirmation loop is a spammer.
- Zach White
usage = 'usage: %prog [options] email1 [email2 [...]]'
import logging
import os
import re
import smtplib
# Match the mail exchanger line in nslookup output.
MX = re.compile(r'^.*\s+mail exchanger = (?P<priority>\d+) (?P<host>\S+)\s*$')
def verify(addr, local_addr=''):
"""Verify the existance of a single email address."""
logging.debug('Verifying existance of %r', addr)
# Find mail exchanger of this address.
host = addr.rsplit('@', 2)[1]
p = os.popen('nslookup -q=mx %s' % host, 'r')
mxes = list()
for line in p:
m = MX.match(line)
if m is not None:
logging.debug('Found %d mail exchangers for %s.', len(mxes), host)
if len(mxes) == 0:
return False
host = mxes[0]
# Connect to the mail server and check.
logging.debug('Checking address with %s.', host)
server = smtplib.SMTP(host)
code, response = server.docmd('mail from:', '')
logging.debug('MAIL FROM command returned %d: %r', code, response)
code, response = server.docmd('rcpt to:', addr)
logging.debug('RCPT TO command returned %d: %r', code, response)
return (code // 100 == 2)
def main(*args, **opts):
"""Handle execution from the command line."""
for addr in args:
if verify(addr, local_addr=opts['local_addr']):
print '%r exists.' % addr
print '%s DOES NOT exist.' % addr
def flags():
"""Parse options from the command line."""
from optparse import OptionParser
parser = OptionParser(usage=usage)
parser.add_option('-f', '--from',
dest='local_addr', default='',
help='email address to appear to be from')
parser.add_option('-d', '--debug',
action='store_const', const=logging.DEBUG, dest='log_level',
help='log everything')
parser.add_option('-v', '--verbose',
action='store_const', const=logging.INFO, dest='log_level',
help='log everything but debugging messages')
parser.add_option('-q', '--quiet',
action='store_const', const=logging.ERROR, dest='log_level',
help='only log errors')
opts, args = parser.parse_args()
return opts, args
if __name__ == '__main__':
opts, args = flags()
main(*args, **opts.__dict__)
Copy link

potar commented Jan 14, 2014

This script doesn't work for It always returns True.

Copy link

armujahid commented Aug 5, 2014

Not working anymore. Returning false for every email.

Copy link

jvillacortam commented Oct 30, 2014

If you are using Windows then you need to change the regular expression to match the return of os.popen('nslookup -q=mx %s' % host, 'r').

The verify method works well in Linux

Copy link

raveendrau commented Jul 16, 2015

Hi friends can any one suggest how to nslookup for windows

Copy link

stephennancekivell commented Apr 2, 2021

Your IP address will probably get blocked from the mail server if you do this. If that happens you might need to try an API like Real Email.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment