Created
January 16, 2009 16:35
-
-
Save blinks/47987 to your computer and use it in GitHub Desktop.
Check if an email address exists without sending an email. Technique from: http://www.webdigi.co.uk/blog/2009/how-to-check-if-an-email-address-exists-without-sending-an-email/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Check that a particular email address exists. | |
Adam Blinkinsop <[email protected]> | |
WARNING: | |
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='[email protected]'): | |
"""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: | |
mxes.append(m.group('host')) | |
logging.debug('Found %d mail exchangers for %s.', len(mxes), host) | |
if len(mxes) == 0: | |
return False | |
else: | |
host = mxes[0] | |
# Connect to the mail server and check. | |
logging.debug('Checking address with %s.', host) | |
server = smtplib.SMTP(host) | |
server.ehlo_or_helo_if_needed() | |
code, response = server.docmd('mail from:', '[email protected]') | |
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) | |
server.quit() | |
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 | |
else: | |
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='[email protected]', | |
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() | |
logging.basicConfig(level=opts.log_level) | |
return opts, args | |
if __name__ == '__main__': | |
opts, args = flags() | |
main(*args, **opts.__dict__) |
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
Hi friends can any one suggest how to nslookup for windows
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
Not working anymore. Returning false for every email.