1.. _email-examples: 2 3:mod:`email`: Examples 4---------------------- 5 6Here are a few examples of how to use the :mod:`email` package to read, write, 7and send simple email messages, as well as more complex MIME messages. 8 9First, let's see how to create and send a simple text message (both the 10text content and the addresses may contain unicode characters): 11 12.. literalinclude:: ../includes/email-simple.py 13 14 15Parsing :rfc:`822` headers can easily be done by the using the classes 16from the :mod:`~email.parser` module: 17 18.. literalinclude:: ../includes/email-headers.py 19 20 21Here's an example of how to send a MIME message containing a bunch of family 22pictures that may be residing in a directory: 23 24.. literalinclude:: ../includes/email-mime.py 25 26 27Here's an example of how to send the entire contents of a directory as an email 28message: [1]_ 29 30.. literalinclude:: ../includes/email-dir.py 31 32 33Here's an example of how to unpack a MIME message like the one 34above, into a directory of files: 35 36.. literalinclude:: ../includes/email-unpack.py 37 38 39Here's an example of how to create an HTML message with an alternative plain 40text version. To make things a bit more interesting, we include a related 41image in the html part, and we save a copy of what we are going to send to 42disk, as well as sending it. 43 44.. literalinclude:: ../includes/email-alternative.py 45 46 47If we were sent the message from the last example, here is one way we could 48process it: 49 50.. literalinclude:: ../includes/email-read-alternative.py 51 52Up to the prompt, the output from the above is: 53 54.. code-block:: none 55 56 To: Penelope Pussycat <penelope@example.com>, Fabrette Pussycat <fabrette@example.com> 57 From: Pepé Le Pew <pepe@example.com> 58 Subject: Ayons asperges pour le déjeuner 59 60 Salut! 61 62 Cela ressemble à un excellent recipie[1] déjeuner. 63 64 65.. rubric:: Footnotes 66 67.. [1] Thanks to Matthew Dixon Cowles for the original inspiration and examples. 68