[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

email: Parsing and Composing Mail Content


The second edition of this book used a handful of standard library modules (rfc822,
StringIO, and more) to parse the contents of messages, and simple text processing to
compose them. Additionally, that edition included a section on extracting and decoding
attached parts of a message using modules such as mhlib, mimetools, and base64.


In the third edition, those tools were still available, but were, frankly, a bit clumsy and
error-prone. Parsing attachments from messages, for example, was tricky, and com-
posing even basic messages was tedious (in fact, an early printing of the prior edition
contained a potential bug, because it omitted one \n character in a string formatting
operation). Adding attachments to sent messages wasn’t even attempted, due to the
complexity of the formatting involved. Most of these tools are gone completely in Py-
thon 3.X as I write this fourth edition, partly because of their complexity, and partly
because they’ve been made obsolete.


Luckily, things are much simpler today. After the second edition, Python sprouted a
new email package—a powerful collection of tools that automate most of the work
behind parsing and composing email messages. This module gives us an object-based
message interface and handles all the textual message structure details, both analyzing
and creating it. Not only does this eliminate a whole class of potential bugs, it also
promotes more advanced mail processing.


Things like attachments, for instance, become accessible to mere mortals (and authors
with limited book real estate). In fact, an entire original section on manual attachment
parsing and decoding was deleted in the third edition—it’s essentially automatic with
email. The new package parses and constructs headers and attachments; generates
correct email text; decodes and encodes Base64, quoted-printable, and uuencoded
data; and much more.


We won’t cover the email package in its entirety in this book; it is well documented in
Python’s library manual. Our goal here is to explore some example usage code, which
you can study in conjunction with the manuals. But to help get you started, let’s begin
with a quick overview. In a nutshell, the email package is based around the Message
object it provides:


Parsing mail
A mail’s full text, fetched from poplib or imaplib, is parsed into a new Message
object, with an API for accessing its components. In the object, mail headers be-
come dictionary-like keys, and components become a “payload” that can be
walked with a generator interface (more on payloads in a moment).


Creating mail
New mails are composed by creating a new Message object, using an API to attach
headers and parts, and asking the object for its print representation—a correctly
formatted mail message text, ready to be passed to the smtplib module for delivery.
Headers are added by key assignment and attachments by method calls.


email: Parsing and Composing Mail Content | 921
Free download pdf