Chapter 12 ■ Building and parsing e-Mail
237
You can exercise this script against any of the e-mail messages that the earlier scripts can generate. (Or, of course,
you could try feeding it a real-life e-mail of your own.) Running it against the most complex message that can be
generated using the above scripts produces the following results.
$ python3 build_mime_email.py -i attachment.txt attachment.gz > email.txt
$ python3 display_structure.py email.txt
type=multipart/mixed
.0 type=multipart/alternative
.0.0 type=multipart/related
.0.0.0 type=text/html str len=215
.0.0.1 type=image/gif bytes len=35 attachment filename='blue-dot.gif'
.0.1 type=text/plain str len=59
.1 type=text/plain str len=15 attachment filename='attachment.txt'
.2 type=application/octet-stream bytes len=33 attachment filename='attachment.gz'
The part numbers that introduce each line of output can be used in further code that you write in order to dive
directly into the message to fetch the particular part in which you are interested by providing each integer index to
the get_payload() method. For example, if you wanted to fetch the blue dot GIF image from inside this message,
you would call:
part = message.get_payload(0).get_payload(0).get_payload(1)
Note again that only multipart parts are allowed to have further MIME subparts inside. Every part with a
nonmultipart content type is a leaf node in the tree above, containing simple content with no further e-mail-relevant
structure beneath.
Header Encodings
The parsing scripts above, thanks to the email module, will correctly handle internationalized headers that encode
special characters using the conventions of RFC 2047 without any modification. Listing 12-6 generates such an e-mail
with which you can perform tests. Note that because Python 3 source code is UTF-8 encoded by default, you can
include international characters without needing a -- coding: utf-8 -- declaration at the top, as was necessary
with Python 2.
Listing 12-6. Generate an Internationalized E-Mail to Test the Parsing Script
#!/usr/bin/env python3
Foundations of Python Network Programming, Third Edition
https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter12/build_unicode_email.py
import email.message, email.policy, sys
text = """\
Hwær cwom mearg? Hwær cwom mago?
Hwær cwom maþþumgyfa?
Hwær cwom symbla gesetu?
Hwær sindon seledreamas?"""