Sending
The smtplib module accepts email content to send as str strings. Internally, mes-
sage text passed in str form is encoded to binary bytes for transmission using the
ascii encoding scheme. Passing an already encoded bytes string to the send call
may allow more explicit control.
Composing
The email package produces Unicode str strings containing plain text when gen-
erating full email text for sending with smtplib and accepts optional encoding
specifications for messages and their parts, which it applies according to email
standard rules. Message headers may also be encoded per email, MIME, and Uni-
code conventions.
Parsing
The email package in 3.1 currently requires raw email byte strings of the type
fetched with poplib to be decoded into Unicode str strings as appropriate before
it can be passed in to be parsed into a message object. This pre-parse decoding
might be done by a default, user preference, mail headers inspection, or intelligent
guess. Because this requirement raises difficult issues for package clients, it may be
dropped in a future version of email and Python.
Navigating
The email package returns most message components as str strings, though parts
content decoded by Base64 and other email encoding schemes may be returned as
bytes strings, parts fetched without such decoding may be str or bytes, and some
str string parts are internally encoded to bytes with scheme raw-unicode-escape
before processing. Message headers may be decoded by the package on request as
well.
If you’re migrating email scripts (or your mindset) from 2.X, you’ll need to treat email
text fetched from a server as byte strings, and encode it before passing it along for
parsing; scripts that send or compose email are generally unaffected (and this may be
the majority of Python email-aware scripts), though content may have to be treated
specially if it may be returned as byte strings.
This is the story in Python 3.1, which is of course prone to change over time. We’ll see
how these email constraints translate into code as we move along in this section. Suffice
it to say, the text on the Internet is not as simple as it used to be, though it probably
shouldn’t have been anyhow.
POP: Fetching Email
I confess: up until just before 2000, I took a lowest-common-denominator approach
to email. I preferred to check my messages by Telnetting to my ISP and using a simple
command-line email interface. Of course, that’s not ideal for mail with attachments,
pictures, and the like, but its portability was staggering—because Telnet runs on almost
POP: Fetching Email | 901