Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 1 | .. _email-examples: |
| 2 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | :mod:`email`: Examples |
| 4 | ---------------------- |
| 5 | |
| 6 | Here are a few examples of how to use the :mod:`email` package to read, write, |
| 7 | and send simple email messages, as well as more complex MIME messages. |
| 8 | |
| 9 | First, let's see how to create and send a simple text message: |
| 10 | |
| 11 | .. literalinclude:: ../includes/email-simple.py |
| 12 | |
| 13 | |
Sean Reifscheider | 78a44c5 | 2010-03-19 23:23:05 +0000 | [diff] [blame] | 14 | And parsing RFC822 headers can easily be done by the parse(filename) or |
| 15 | parsestr(message_as_string) methods of the Parser() class: |
| 16 | |
| 17 | .. literalinclude:: ../includes/email-headers.py |
| 18 | |
| 19 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 | Here's an example of how to send a MIME message containing a bunch of family |
| 21 | pictures that may be residing in a directory: |
| 22 | |
| 23 | .. literalinclude:: ../includes/email-mime.py |
| 24 | |
| 25 | |
| 26 | Here's an example of how to send the entire contents of a directory as an email |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 27 | message: [1]_ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | |
| 29 | .. literalinclude:: ../includes/email-dir.py |
| 30 | |
| 31 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 32 | Here's an example of how to unpack a MIME message like the one |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | above, into a directory of files: |
| 34 | |
| 35 | .. literalinclude:: ../includes/email-unpack.py |
| 36 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 37 | Here's an example of how to create an HTML message with an alternative plain |
| 38 | text version: [2]_ |
| 39 | |
| 40 | .. literalinclude:: ../includes/email-alternative.py |
| 41 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
R David Murray | 02384bf | 2014-02-07 10:55:17 -0500 | [diff] [blame^] | 43 | Examples using the Provision API |
| 44 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 45 | Here is a reworking of the last example using the provisional API. To make |
| 46 | things a bit more interesting, we include a related image in the html part, and |
| 47 | we save a copy of what we are going to send to disk, as well as sending it. |
| 48 | |
| 49 | This example also shows how easy it is to include non-ASCII, and simplifies the |
| 50 | sending of the message using the :meth:`.send_message` method of the |
| 51 | :mod:`smtplib` module. |
| 52 | |
| 53 | .. literalinclude:: ../includes/email-alternative-new-api.py |
| 54 | |
| 55 | If we were instead sent the message from the last example, here is one |
| 56 | way we could process it: |
| 57 | |
| 58 | .. literalinclude:: ../includes/email-read-alternative-new-api.py |
| 59 | |
| 60 | Up to the prompt, the output from the above is:: |
| 61 | |
| 62 | To: Penelope Pussycat <"penelope@example.com">, Fabrette Pussycat <"fabrette@example.com"> |
| 63 | From: Pepé Le Pew <pepe@example.com> |
| 64 | Subject: Ayons asperges pour le déjeuner |
| 65 | |
| 66 | Salut! |
| 67 | |
| 68 | Cela ressemble à un excellent recipie[1] déjeuner. |
| 69 | |
| 70 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | .. rubric:: Footnotes |
| 72 | |
| 73 | .. [1] Thanks to Matthew Dixon Cowles for the original inspiration and examples. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 74 | .. [2] Contributed by Martin Matejek. |