|
| 1 | +#!/usr/bin/python |
| 2 | +"""smtptest.py: command-line smtp test mail sender |
| 3 | +https://github.com/turbodog/python-smtp-mail-sending-tester |
| 4 | +
|
| 5 | +Usage: python smtptest.py [options] fromaddress toaddress serveraddress |
| 6 | +
|
| 7 | +Examples: |
| 8 | + python smtptest.py [email protected] [email protected] mail.example.com |
| 9 | + python smtptest.py --debuglevel 1 --usetls -u bob -p xyzzy "Bob <[email protected]>" [email protected] mail.example.com |
| 10 | +
|
| 11 | +At verbose == False and debuglevel == 0, smtptest will either succeed silently or print an error. Setting verbose or a debuglevel to 1 will generate intermediate output. |
| 12 | +
|
| 13 | +See also http://docs.python.org/library/smtplib.html |
| 14 | +
|
| 15 | +""" |
| 16 | + |
| 17 | +__version__ = "1.0" |
| 18 | +__author__ = "Lindsey Smith ([email protected])" |
| 19 | +__copyright__ = "(C) 2010 Lindsey Smith. GNU GPL 2 or 3." |
| 20 | + |
| 21 | +import smtplib |
| 22 | +from time import strftime |
| 23 | +import sys |
| 24 | +from optparse import OptionParser |
| 25 | + |
| 26 | +fromaddr = "" |
| 27 | +toaddr = "" |
| 28 | +serveraddr = "" |
| 29 | + |
| 30 | +usage = "Usage: %prog [options] fromaddress toaddress serveraddress" |
| 31 | +parser = OptionParser(usage=usage) |
| 32 | + |
| 33 | +parser.set_defaults(usetls=False) |
| 34 | +parser.set_defaults(usessl=False) |
| 35 | +parser.set_defaults(serverport=25) |
| 36 | +parser.set_defaults(SMTP_USER="") |
| 37 | +parser.set_defaults(SMTP_PASS="") |
| 38 | +parser.set_defaults(debuglevel=0) |
| 39 | +parser.set_defaults(verbose=False) |
| 40 | + |
| 41 | +parser.add_option("-t", "--usetls", action="store_true", dest="usetls", default=False, help="Connect using TLS, default is false") |
| 42 | +parser.add_option("-s", "--usessl", action="store_true", dest="usessl", default=False, help="Connect using SSL, default is false") |
| 43 | +parser.add_option("-n", "--port", action="store", type="int", dest="serverport", help="SMTP server port", metavar="nnn") |
| 44 | +parser.add_option("-u", "--username", action="store", type="string", dest="SMTP_USER", help="SMTP server auth username", metavar="username") |
| 45 | +parser.add_option("-p", "--password", action="store", type="string", dest="SMTP_PASS", help="SMTP server auth password", metavar="password") |
| 46 | +parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose message printing") |
| 47 | +parser.add_option("-d", "--debuglevel", type="int", dest="debuglevel", help="Set to 1 to print smtplib.send messages", metavar="n") |
| 48 | + |
| 49 | +(options, args) = parser.parse_args() |
| 50 | +if len(args) != 3: |
| 51 | + parser.print_help() |
| 52 | + parser.error("incorrect number of arguments") |
| 53 | + sys.exit(-1) |
| 54 | + |
| 55 | +fromaddr = args[0] |
| 56 | +toaddr = args[1] |
| 57 | +serveraddr = args[2] |
| 58 | + |
| 59 | +now = strftime("%Y-%m-%d %H:%M:%S") |
| 60 | + |
| 61 | +msg = "From: %s\r\nTo: %s\r\nSubject: Test Message from smtptest at %s\r\n\r\nTest message from the smtptest tool sent at %s" % (fromaddr, toaddr, now, now) |
| 62 | + |
| 63 | +if options.verbose: |
| 64 | + print('usetls:', options.usetls) |
| 65 | + print('usessl:', options.usessl) |
| 66 | + print('from address:', fromaddr) |
| 67 | + print('to address:', toaddr) |
| 68 | + print('server address:', serveraddr) |
| 69 | + print('server port:', options.serverport) |
| 70 | + print('smtp username:', options.SMTP_USER) |
| 71 | + print('smtp password: *****') |
| 72 | + print('smtplib debuglevel:', options.debuglevel) |
| 73 | + print("-- Message body ---------------------") |
| 74 | + print(msg) |
| 75 | + print("-------------------------------------") |
| 76 | + |
| 77 | +server = None |
| 78 | +if options.usessl: |
| 79 | + server = smtplib.SMTP_SSL() |
| 80 | +else: |
| 81 | + server = smtplib.SMTP() |
| 82 | + |
| 83 | +server.set_debuglevel(options.debuglevel) |
| 84 | +server.connect(serveraddr, options.serverport) |
| 85 | +server.ehlo() |
| 86 | +if options.usetls: server.starttls() |
| 87 | +server.ehlo() |
| 88 | +if options.SMTP_USER != "": server.login(options.SMTP_USER, options.SMTP_PASS) |
| 89 | +server.sendmail(fromaddr, toaddr, msg) |
| 90 | +server.quit() |
0 commit comments