Foundations of Python Network Programming

(WallPaper) #1

Chapter 13 ■ SMtp


258


Most outgoing e-mail servers on the Internet do not support authentication. If you are using a server that does
not support authentication, you will receive an Authentication failed error message from the login() attempt. You can
prevent that by checking connection.has_extn('auth') after calling connection.ehlo() provided that the remote
server supports ESMTP.
You can run this program just like the previous examples. If you run it with a server that does support
authentication, you will be prompted for a username and password. If they are accepted, then the program will
proceed to transmit your message.


SMTP Tips


Here are some tips to help you implement SMTP clients:


•    There is no way to guarantee that a message was delivered. Sometimes you’ll know
immediately that your attempt failed, but the lack of an error does not mean that something
else will not go wrong before the message is safely delivered to the recipient.

•    The sendmail() function raises an exception if any of the recipients failed, though the
message may still have been sent to other recipients. Check the exception you get back for
more details. If it is very important for you to know specifics of which addresses failed—say
because you will want to try retransmitting later without producing duplicate copies for the
people who have already received the message—you may need to call sendmail() individually
for each recipient. Note, however, that this more naïve approach will cause the message body
to be transmitted multiple times, once for each recipient.

•    SSL/TLS is insecure without certificate validation: until validation happens, you could be
talking to any old server that has temporarily gotten control of the normal server’s IP address.
To support certificate verification, remember to create an SSL context object, as shown in the
TLS previous example, and provide it as the sole argument to starttls().

•    Python’s smtplib is not meant to be a general-purpose e-mail relay. Rather, you should use it
to send messages to an SMTP server close to you that will handle the actual delivery of e-mail.

Summary


SMTP is used to transmit e-mail messages to e-mail servers. Python provides the smtplib module for SMTP clients to
use. By calling the sendmail() method of SMTP objects, you can transmit messages. The sole means of specifying the
actual recipients of a message is with parameters to sendmail(); the To, Cc, and Bcc message headers in the text of the
message itself are separate from the actual list of recipients.
Several different exceptions could be raised during an SMTP conversation. Interactive programs should check for
and handle them appropriately.
ESMTP is an extension to SMTP. It allows you to discover the maximum message size supported by a remote
SMTP server prior to transmitting a message. ESMTP also permits TLS, which is a way to encrypt your conversation
with a remote server. Fundamentals of TLS were covered in Chapter 6.
Some SMTP servers require authentication. You can authenticate with the login() method. SMTP does not
provide functions for downloading messages from a mailbox to your own computer. To accomplish that, you will need
the protocols discussed in the next two chapters. POP, discussed in Chapter 14, is a simple way to download messages.
IMAP, discussed in Chapter 15, is a more capable and powerful protocol.

Free download pdf