Foundations of Python Network Programming

(WallPaper) #1

Chapter 13 ■ SMtp


250


Listing 13-2. A More Cautious SMTP Client


#!/usr/bin/env python3


Foundations of Python Network Programming, Third Edition


https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter13/debug.py


import sys, smtplib, socket


message_template = """To: {}
From: {}
Subject: Test Message from simple.py


Hello,


This is a test message sent to you from the debug.py program
in Foundations of Python Network Programming.
"""


def main():
if len(sys.argv) < 4:
name = sys.argv[0]
print("usage: {} server fromaddr toaddr [toaddr...]".format(name))
sys.exit(2)


server, fromaddr, toaddrs = sys.argv[1], sys.argv[2], sys.argv[3:]
message = message_template.format(', '.join(toaddrs), fromaddr)


try:
connection = smtplib.SMTP(server)
connection.set_debuglevel(1)
connection.sendmail(fromaddr, toaddrs, message)
except (socket.gaierror, socket.error, socket.herror,
smtplib.SMTPException) as e:
print("Your message may not have been sent!")
print(e)
sys.exit(1)
else:
s = '' if len(toaddrs) == 1 else 's'
print("Message sent to {} recipient{}".format(len(toaddrs), s))
connection.quit()


if name == 'main':
main()


This program looks similar to the previous one; however, the output will be very different. Take a look at
Listing 13-3 for an example.


Listing 13-3. Debugging Output from smtplib


$ python3 debug.py mail.example.com [email protected] [email protected]
send: 'ehlo [127.0.1.1]\r\n'
reply: b'250-guinness\r\n'
reply: b'250-SIZE 33554432\r\n'

Free download pdf