Skip to content

Instantly share code, notes, and snippets.

@etes
Forked from blinks/smtpcheck.py
Last active March 18, 2020 12:44
Show Gist options
  • Save etes/5ee3b9b51a2e1c3b0208 to your computer and use it in GitHub Desktop.
Save etes/5ee3b9b51a2e1c3b0208 to your computer and use it in GitHub Desktop.

Revisions

  1. Adam Blinkinsop revised this gist Jan 16, 2009. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions smtpcheck.py
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,8 @@
    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
  2. Adam Blinkinsop revised this gist Jan 16, 2009. 1 changed file with 18 additions and 2 deletions.
    20 changes: 18 additions & 2 deletions smtpcheck.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,22 @@
    #!/usr/bin/env python
    # Check that a particular email address exists.
    # Adam Blinkinsop <[email protected]>
    """
    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.
    """
    usage = 'usage: %prog [options] email1 [email2 [...]]'
    import logging
    import os
  3. @invalid-email-address Anonymous created this gist Jan 16, 2009.
    71 changes: 71 additions & 0 deletions smtpcheck.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    #!/usr/bin/env python
    # Check that a particular email address exists.
    # Adam Blinkinsop <[email protected]>
    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__)