Foundations of Python Network Programming

(WallPaper) #1
Chapter 12 ■ Building and parsing e-Mail

225

to pele, which probably performed e-mail transmission either for a department or for the entire campus. Finally, pele
made an SMTP connection to the europa machine on my desk at Georgia Tech, which wrote the message to disk so
that I could read it later.
I will pause to introduce a few specific e-mail headers at this point; see the standards for a complete list.


•    From names the author of the e-mail message. Like the headers that follow, it supports both
an actual name as well as that person’s e-mail address inside angle brackets.

•    Reply-to specifies where replies should be destined, if not to the author listed in the From
header.

•    To is a list of one or more primary recipients.

•    Cc lists one or more recipients who should receive “carbon copies” of the e-mail, but who are
not directly addressed by the communication.

•    Bcc lists recipients who should be given secret carbon copies of the e-mail but without any of
the other recipients knowing this. Careful e-mail clients therefore strip Bcc off before actually
transmitting an e-mail.

•    Subject is a human-readable summary of the message contents written by the message author.

•    Date specifies when the message was sent or received. Typically, if the sender’s e-mail client
includes a date, then the receiving e-mail server and reader will not overwrite it. But if the sender
does not include a date, then it might be added later for completeness when the e-mail is received.

•    Message-Id is a unique string for identifying the e-mail.

•    In-Reply-To are the unique Message-Id’s of the previous messages to which this message is a
reply. These can be very useful if you are asked to build a threaded display that places reply
messages beneath the e-mails to which they are replies.

•    Received is added each time the e-mail arrives at another “hop” on its way across the Internet
via SMTP. E-mail server administrators often pore through these tree rings in order to
determine why a message was or was not delivered correctly.

You can see that the plain-text limitation on e-mail has repercussions for both the headers and the body: both
of them are limited to being ASCII in a simple example like this. In the sections that follow,I will explain both the
standards that govern how a header can include international characters and the standards that set how the e-mail
body can include international or binary data.


Building an E-Mail Message


The primary interface in Python for building e-mail messages is the EmailMessage class, which will be used in every
program listing in this chapter. It is the result of hard work by Python email module guru R. David Murray, whom I
want to thank for his guidance and advice as I put together the scripts in this chapter. The simplest example is shown
in Listing 12-2.


Listing 12-2. Generating a Simple Text E-Mail Message


#!/usr/bin/env python3


Foundations of Python Network Programming, Third Edition


https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter12/build_basic_email.py


import email.message, email.policy, email.utils, sys

Free download pdf