blob: 954f17508b0723b5e27911d4608a2624470fd933 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`email`: Generating MIME documents
2---------------------------------------
3
4.. module:: email.generator
5 :synopsis: Generate flat text email messages from a message structure.
6
7
8One of the most common tasks is to generate the flat text of the email message
9represented by a message object structure. You will need to do this if you want
10to send your message via the :mod:`smtplib` module or the :mod:`nntplib` module,
11or print the message on the console. Taking a message object structure and
12producing a flat text document is the job of the :class:`Generator` class.
13
14Again, as with the :mod:`email.parser` module, you aren't limited to the
15functionality of the bundled generator; you could write one from scratch
16yourself. However the bundled generator knows how to generate most email in a
17standards-compliant way, should handle MIME and non-MIME email messages just
18fine, and is designed so that the transformation from flat text, to a message
Georg Brandl3638e482009-04-27 16:46:17 +000019structure via the :class:`~email.parser.Parser` class, and back to flat text,
R. David Murray101f2782010-01-10 19:18:27 +000020is idempotent (the input is identical to the output). On the other hand, using
21the Generator on a :class:`~email.message.Message` constructed by program may
22result in changes to the :class:`~email.message.Message` object as defaults are
23filled in.
Georg Brandl116aa622007-08-15 14:28:22 +000024
R. David Murray96fd54e2010-10-08 15:55:28 +000025:class:`bytes` output can be generated using the :class:`BytesGenerator` class.
26If the message object structure contains non-ASCII bytes, this generator's
27:meth:`~BytesGenerator.flatten` method will emit the original bytes. Parsing a
28binary message and then flattening it with :class:`BytesGenerator` should be
29idempotent for standards compliant messages.
30
Georg Brandl116aa622007-08-15 14:28:22 +000031Here are the public methods of the :class:`Generator` class, imported from the
32:mod:`email.generator` module:
33
34
Georg Brandl3f076d82009-05-17 11:28:33 +000035.. class:: Generator(outfp, mangle_from_=True, maxheaderlen=78)
Georg Brandl116aa622007-08-15 14:28:22 +000036
Antoine Pitrou11cb9612010-09-15 11:11:28 +000037 The constructor for the :class:`Generator` class takes a :term:`file-like object`
38 called *outfp* for an argument. *outfp* must support the :meth:`write` method
39 and be usable as the output file for the :func:`print` function.
Georg Brandl116aa622007-08-15 14:28:22 +000040
41 Optional *mangle_from_* is a flag that, when ``True``, puts a ``>`` character in
42 front of any line in the body that starts exactly as ``From``, i.e. ``From``
43 followed by a space at the beginning of the line. This is the only guaranteed
44 portable way to avoid having such lines be mistaken for a Unix mailbox format
45 envelope header separator (see `WHY THE CONTENT-LENGTH FORMAT IS BAD
46 <http://www.jwz.org/doc/content-length.html>`_ for details). *mangle_from_*
47 defaults to ``True``, but you might want to set this to ``False`` if you are not
48 writing Unix mailbox format files.
49
50 Optional *maxheaderlen* specifies the longest length for a non-continued header.
51 When a header line is longer than *maxheaderlen* (in characters, with tabs
52 expanded to 8 spaces), the header will be split as defined in the
Georg Brandl3638e482009-04-27 16:46:17 +000053 :class:`~email.header.Header` class. Set to zero to disable header wrapping.
54 The default is 78, as recommended (but not required) by :rfc:`2822`.
Georg Brandl116aa622007-08-15 14:28:22 +000055
Benjamin Petersone41251e2008-04-25 01:59:09 +000056 The other public :class:`Generator` methods are:
Georg Brandl116aa622007-08-15 14:28:22 +000057
58
Georg Brandl3f076d82009-05-17 11:28:33 +000059 .. method:: flatten(msg, unixfrom=False)
Georg Brandl116aa622007-08-15 14:28:22 +000060
Benjamin Petersone41251e2008-04-25 01:59:09 +000061 Print the textual representation of the message object structure rooted at
62 *msg* to the output file specified when the :class:`Generator` instance
63 was created. Subparts are visited depth-first and the resulting text will
64 be properly MIME encoded.
Georg Brandl116aa622007-08-15 14:28:22 +000065
Benjamin Petersone41251e2008-04-25 01:59:09 +000066 Optional *unixfrom* is a flag that forces the printing of the envelope
67 header delimiter before the first :rfc:`2822` header of the root message
68 object. If the root object has no envelope header, a standard one is
69 crafted. By default, this is set to ``False`` to inhibit the printing of
70 the envelope delimiter.
Georg Brandl116aa622007-08-15 14:28:22 +000071
Benjamin Petersone41251e2008-04-25 01:59:09 +000072 Note that for subparts, no envelope header is ever printed.
Georg Brandl116aa622007-08-15 14:28:22 +000073
R. David Murray96fd54e2010-10-08 15:55:28 +000074 Messages parsed with a Bytes parser that have a
75 :mailheader:`Content-Transfer-Encoding` of 8bit will be converted to a
76 use a 7bit Content-Transfer-Encoding. Any other non-ASCII bytes in the
77 message structure will be converted to '?' characters.
78
79 .. versionchanged:: 3.2 added support for re-encoding 8bit message bodies.
80
Benjamin Petersone41251e2008-04-25 01:59:09 +000081 .. method:: clone(fp)
Georg Brandl116aa622007-08-15 14:28:22 +000082
Benjamin Petersone41251e2008-04-25 01:59:09 +000083 Return an independent clone of this :class:`Generator` instance with the
84 exact same options.
Georg Brandl116aa622007-08-15 14:28:22 +000085
Benjamin Petersone41251e2008-04-25 01:59:09 +000086 .. method:: write(s)
Georg Brandl116aa622007-08-15 14:28:22 +000087
Benjamin Petersone41251e2008-04-25 01:59:09 +000088 Write the string *s* to the underlying file object, i.e. *outfp* passed to
89 :class:`Generator`'s constructor. This provides just enough file-like API
90 for :class:`Generator` instances to be used in the :func:`print` function.
Georg Brandl116aa622007-08-15 14:28:22 +000091
R. David Murray96fd54e2010-10-08 15:55:28 +000092As a convenience, see the :class:`~email.message.Message` methods
93:meth:`~email.message.Message.as_string` and ``str(aMessage)``, a.k.a.
94:meth:`~email.message.Message.__str__`, which simplify the generation of a
95formatted string representation of a message object. For more detail, see
Georg Brandl116aa622007-08-15 14:28:22 +000096:mod:`email.message`.
97
R. David Murray96fd54e2010-10-08 15:55:28 +000098.. class:: BytesGenerator(outfp, mangle_from_=True, maxheaderlen=78, fmt=None)
99
100 This class has the same API as the :class:`Generator` class, except that
101 *outfp* must be a file like object that will accept :class`bytes` input to
102 its `write` method. If the message object structure contains non-ASCII
103 bytes, this generator's :meth:`~BytesGenerator.flatten` method will produce
104 them as-is, including preserving parts with a
105 :mailheader:`Content-Transfer-Encoding` of ``8bit``.
106
107 Note that even the :meth:`write` method API is identical: it expects
108 strings as input, and converts them to bytes by encoding them using
109 the ASCII codec.
110
111 .. versionadded:: 3.2
112
Georg Brandl116aa622007-08-15 14:28:22 +0000113The :mod:`email.generator` module also provides a derived class, called
114:class:`DecodedGenerator` which is like the :class:`Generator` base class,
115except that non-\ :mimetype:`text` parts are substituted with a format string
116representing the part.
117
118
Georg Brandl3f076d82009-05-17 11:28:33 +0000119.. class:: DecodedGenerator(outfp[, mangle_from_=True, maxheaderlen=78, fmt=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121 This class, derived from :class:`Generator` walks through all the subparts of a
122 message. If the subpart is of main type :mimetype:`text`, then it prints the
123 decoded payload of the subpart. Optional *_mangle_from_* and *maxheaderlen* are
124 as with the :class:`Generator` base class.
125
126 If the subpart is not of main type :mimetype:`text`, optional *fmt* is a format
127 string that is used instead of the message payload. *fmt* is expanded with the
128 following keywords, ``%(keyword)s`` format:
129
130 * ``type`` -- Full MIME type of the non-\ :mimetype:`text` part
131
132 * ``maintype`` -- Main MIME type of the non-\ :mimetype:`text` part
133
134 * ``subtype`` -- Sub-MIME type of the non-\ :mimetype:`text` part
135
136 * ``filename`` -- Filename of the non-\ :mimetype:`text` part
137
138 * ``description`` -- Description associated with the non-\ :mimetype:`text` part
139
140 * ``encoding`` -- Content transfer encoding of the non-\ :mimetype:`text` part
141
142 The default value for *fmt* is ``None``, meaning ::
143
144 [Non-text (%(type)s) part of message omitted, filename %(filename)s]