09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 13 ■ SMTP<br />

else:<br />

» print "Message successfully sent to %d recipient(s)" % len(toaddrs)<br />

If you run this program and give it a server that understands TLS, the output will look like this:<br />

$ ./tls.py localhost jgoerzen@complete.org jgoerzen@complete.org<br />

Negotiating TLS....<br />

Using TLS connection.<br />

Message successfully sent to 1 recipient(s)<br />

Notice that the call to sendmail() in these last few listings is the same, regardless <strong>of</strong> whether TLS is<br />

used. Once TLS is started, the system hides that layer <strong>of</strong> complexity from you, so you do not need to<br />

worry about it. Please note that this TLS example is not fully secure, because it does not perform<br />

certificate validation; again, see Chapter 6 for details.<br />

Authenticated SMTP<br />

Finally, we reach the topic <strong>of</strong> Authenticated SMTP, where your ISP, university, or company e-mail server<br />

needs you to log in with a username and password to prove that you are not a spammer before they<br />

allow you to send e-mail.<br />

For maximum security, TLS should be used in conjunction with authentication; otherwise your<br />

password (and username, for that matter) will be visible to anyone observing the connection. The proper<br />

way to do this is to establish the TLS connection first, and then send your authentication information<br />

only over the encrypted communications channel.<br />

But using authentication itself is simple; smtplib provides a login() function that takes a username<br />

and a password. Listing 13–6 shows an example. To avoid repeating code already shown in previous<br />

listings, this listing does not take the advice <strong>of</strong> the previous paragraph, and sends the username and<br />

password over an un-authenticated connection that will send them in the clear.<br />

Listing 13–6. Authenticating over SMTP<br />

#!/usr/bin/env python<br />

# SMTP transmission with authentication - Chapter 13 - login.py<br />

import sys, smtplib, socket<br />

from getpass import getpass<br />

if len(sys.argv) < 4:<br />

» print "Syntax: %s server fromaddr toaddr [toaddr...]" % sys.argv[0]<br />

» sys.exit(2)<br />

server, fromaddr, toaddrs = sys.argv[1], sys.argv[2], sys.argv[3:]<br />

message = """To: %s<br />

From: %s<br />

Subject: Test Message from simple.py<br />

Hello,<br />

This is a test message sent to you from the login.py program<br />

in <strong>Foundations</strong> <strong>of</strong> <strong>Python</strong> <strong>Network</strong> <strong>Programming</strong>.<br />

""" % (', '.join(toaddrs), fromaddr)<br />

sys.stdout.write("Enter username: ")<br />

232

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!