blob: 32cecf3486c1204cc99dabe062cd7c3932b52a0c [file] [log] [blame]
Georg Brandlac2380b2009-05-20 18:35:27 +00001.. _email-examples:
2
Georg Brandl8ec7f652007-08-15 14:28:01 +00003: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:
10
11.. literalinclude:: ../includes/email-simple.py
12
13
Sean Reifscheider95ce1fc2010-03-19 23:19:55 +000014And parsing RFC822 headers can easily be done by the parse(filename) or
15parsestr(message_as_string) methods of the Parser() class:
16
17.. literalinclude:: ../includes/email-headers.py
18
19
Georg Brandl8ec7f652007-08-15 14:28:01 +000020Here's an example of how to send a MIME message containing a bunch of family
21pictures that may be residing in a directory:
22
23.. literalinclude:: ../includes/email-mime.py
24
25
26Here's an example of how to send the entire contents of a directory as an email
Georg Brandlbbc4fc22008-02-02 10:49:58 +000027message: [1]_
Georg Brandl8ec7f652007-08-15 14:28:01 +000028
29.. literalinclude:: ../includes/email-dir.py
30
31
Georg Brandlbbc4fc22008-02-02 10:49:58 +000032Here's an example of how to unpack a MIME message like the one
Georg Brandl8ec7f652007-08-15 14:28:01 +000033above, into a directory of files:
34
35.. literalinclude:: ../includes/email-unpack.py
36
Georg Brandlbbc4fc22008-02-02 10:49:58 +000037Here's an example of how to create an HTML message with an alternative plain
38text version: [2]_
39
40.. literalinclude:: ../includes/email-alternative.py
41
Georg Brandl8ec7f652007-08-15 14:28:01 +000042
43.. rubric:: Footnotes
44
45.. [1] Thanks to Matthew Dixon Cowles for the original inspiration and examples.
Georg Brandlbbc4fc22008-02-02 10:49:58 +000046.. [2] Contributed by Martin Matejek.