blob: 847d7e4a0fc85d68cbb369b4e6fa23fb62a3318c [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
R David Murray3edd22a2011-04-18 13:59:37 -040035.. class:: Generator(outfp, mangle_from_=True, maxheaderlen=78, *, \
36 policy=policy.default)
Georg Brandl116aa622007-08-15 14:28:22 +000037
Antoine Pitrou11cb9612010-09-15 11:11:28 +000038 The constructor for the :class:`Generator` class takes a :term:`file-like object`
39 called *outfp* for an argument. *outfp* must support the :meth:`write` method
40 and be usable as the output file for the :func:`print` function.
Georg Brandl116aa622007-08-15 14:28:22 +000041
42 Optional *mangle_from_* is a flag that, when ``True``, puts a ``>`` character in
43 front of any line in the body that starts exactly as ``From``, i.e. ``From``
44 followed by a space at the beginning of the line. This is the only guaranteed
45 portable way to avoid having such lines be mistaken for a Unix mailbox format
46 envelope header separator (see `WHY THE CONTENT-LENGTH FORMAT IS BAD
47 <http://www.jwz.org/doc/content-length.html>`_ for details). *mangle_from_*
48 defaults to ``True``, but you might want to set this to ``False`` if you are not
49 writing Unix mailbox format files.
50
51 Optional *maxheaderlen* specifies the longest length for a non-continued header.
52 When a header line is longer than *maxheaderlen* (in characters, with tabs
53 expanded to 8 spaces), the header will be split as defined in the
Georg Brandl3638e482009-04-27 16:46:17 +000054 :class:`~email.header.Header` class. Set to zero to disable header wrapping.
55 The default is 78, as recommended (but not required) by :rfc:`2822`.
Georg Brandl116aa622007-08-15 14:28:22 +000056
R David Murray3edd22a2011-04-18 13:59:37 -040057 The *policy* keyword specifies a :mod:`~email.policy` object that controls a
58 number of aspects of the generator's operation. The default policy
59 maintains backward compatibility.
60
61 .. versionchanged:: 3.3 Added the *policy* keyword.
62
Benjamin Petersone41251e2008-04-25 01:59:09 +000063 The other public :class:`Generator` methods are:
Georg Brandl116aa622007-08-15 14:28:22 +000064
65
R David Murray3edd22a2011-04-18 13:59:37 -040066 .. method:: flatten(msg, unixfrom=False, linesep=None)
Georg Brandl116aa622007-08-15 14:28:22 +000067
Benjamin Petersone41251e2008-04-25 01:59:09 +000068 Print the textual representation of the message object structure rooted at
69 *msg* to the output file specified when the :class:`Generator` instance
70 was created. Subparts are visited depth-first and the resulting text will
71 be properly MIME encoded.
Georg Brandl116aa622007-08-15 14:28:22 +000072
Benjamin Petersone41251e2008-04-25 01:59:09 +000073 Optional *unixfrom* is a flag that forces the printing of the envelope
74 header delimiter before the first :rfc:`2822` header of the root message
75 object. If the root object has no envelope header, a standard one is
76 crafted. By default, this is set to ``False`` to inhibit the printing of
77 the envelope delimiter.
Georg Brandl116aa622007-08-15 14:28:22 +000078
Benjamin Petersone41251e2008-04-25 01:59:09 +000079 Note that for subparts, no envelope header is ever printed.
Georg Brandl116aa622007-08-15 14:28:22 +000080
R. David Murray8451c4b2010-10-23 22:19:56 +000081 Optional *linesep* specifies the line separator character used to
R David Murray3edd22a2011-04-18 13:59:37 -040082 terminate lines in the output. If specified it overrides the value
83 specified by the ``Generator``\'s ``policy``.
R. David Murray8451c4b2010-10-23 22:19:56 +000084
R David Murray3edd22a2011-04-18 13:59:37 -040085 Because strings cannot represent non-ASCII bytes, ``Generator`` ignores
86 the value of the :attr:`~email.policy.Policy.must_be_7bit`
87 :mod:`~email.policy` setting and operates as if it were set ``True``.
88 This means that messages parsed with a Bytes parser that have a
R. David Murray96fd54e2010-10-08 15:55:28 +000089 :mailheader:`Content-Transfer-Encoding` of 8bit will be converted to a
R. David Murray92532142011-01-07 23:25:30 +000090 use a 7bit Content-Transfer-Encoding. Non-ASCII bytes in the headers
91 will be :rfc:`2047` encoded with a charset of `unknown-8bit`.
R. David Murray96fd54e2010-10-08 15:55:28 +000092
R. David Murray8451c4b2010-10-23 22:19:56 +000093 .. versionchanged:: 3.2
Georg Brandl872a7022010-10-24 14:32:45 +000094 Added support for re-encoding 8bit message bodies, and the *linesep*
95 argument.
R. David Murray96fd54e2010-10-08 15:55:28 +000096
Benjamin Petersone41251e2008-04-25 01:59:09 +000097 .. method:: clone(fp)
Georg Brandl116aa622007-08-15 14:28:22 +000098
Benjamin Petersone41251e2008-04-25 01:59:09 +000099 Return an independent clone of this :class:`Generator` instance with the
100 exact same options.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
Benjamin Petersone41251e2008-04-25 01:59:09 +0000102 .. method:: write(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Benjamin Petersone41251e2008-04-25 01:59:09 +0000104 Write the string *s* to the underlying file object, i.e. *outfp* passed to
105 :class:`Generator`'s constructor. This provides just enough file-like API
106 for :class:`Generator` instances to be used in the :func:`print` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
R. David Murray96fd54e2010-10-08 15:55:28 +0000108As a convenience, see the :class:`~email.message.Message` methods
109:meth:`~email.message.Message.as_string` and ``str(aMessage)``, a.k.a.
110:meth:`~email.message.Message.__str__`, which simplify the generation of a
111formatted string representation of a message object. For more detail, see
Georg Brandl116aa622007-08-15 14:28:22 +0000112:mod:`email.message`.
113
R David Murray3edd22a2011-04-18 13:59:37 -0400114.. class:: BytesGenerator(outfp, mangle_from_=True, maxheaderlen=78, *, \
115 policy=policy.default)
R. David Murray96fd54e2010-10-08 15:55:28 +0000116
R. David Murray8451c4b2010-10-23 22:19:56 +0000117 The constructor for the :class:`BytesGenerator` class takes a binary
118 :term:`file-like object` called *outfp* for an argument. *outfp* must
119 support a :meth:`write` method that accepts binary data.
R. David Murray96fd54e2010-10-08 15:55:28 +0000120
R. David Murray8451c4b2010-10-23 22:19:56 +0000121 Optional *mangle_from_* is a flag that, when ``True``, puts a ``>``
122 character in front of any line in the body that starts exactly as ``From``,
123 i.e. ``From`` followed by a space at the beginning of the line. This is the
124 only guaranteed portable way to avoid having such lines be mistaken for a
125 Unix mailbox format envelope header separator (see `WHY THE CONTENT-LENGTH
126 FORMAT IS BAD <http://www.jwz.org/doc/content-length.html>`_ for details).
127 *mangle_from_* defaults to ``True``, but you might want to set this to
128 ``False`` if you are not writing Unix mailbox format files.
129
130 Optional *maxheaderlen* specifies the longest length for a non-continued
131 header. When a header line is longer than *maxheaderlen* (in characters,
132 with tabs expanded to 8 spaces), the header will be split as defined in the
133 :class:`~email.header.Header` class. Set to zero to disable header
134 wrapping. The default is 78, as recommended (but not required) by
135 :rfc:`2822`.
136
R David Murray3edd22a2011-04-18 13:59:37 -0400137 The *policy* keyword specifies a :mod:`~email.policy` object that controls a
138 number of aspects of the generator's operation. The default policy
139 maintains backward compatibility.
140
141 .. versionchanged:: 3.3 Added the *policy* keyword.
142
R. David Murray8451c4b2010-10-23 22:19:56 +0000143 The other public :class:`BytesGenerator` methods are:
144
145
R David Murray3edd22a2011-04-18 13:59:37 -0400146 .. method:: flatten(msg, unixfrom=False, linesep=None)
R. David Murray8451c4b2010-10-23 22:19:56 +0000147
148 Print the textual representation of the message object structure rooted
149 at *msg* to the output file specified when the :class:`BytesGenerator`
150 instance was created. Subparts are visited depth-first and the resulting
R David Murray3edd22a2011-04-18 13:59:37 -0400151 text will be properly MIME encoded. If the :mod:`~email.policy` option
152 :attr:`~email.policy.Policy.must_be_7bit` is ``False`` (the default),
153 then any bytes with the high bit set in the original parsed message that
154 have not been modified will be copied faithfully to the output. If
155 ``must_be_7bit`` is true, the bytes will be converted as needed using an
156 ASCII content-transfer-encoding. In particular, RFC-invalid non-ASCII
157 bytes in headers will be encoded using the MIME ``unknown-8bit``
158 character set, thus rendering them RFC-compliant.
159
160 .. XXX: There should be a complementary option that just does the RFC
161 compliance transformation but leaves CTE 8bit parts alone.
R. David Murray8451c4b2010-10-23 22:19:56 +0000162
163 Messages parsed with a Bytes parser that have a
164 :mailheader:`Content-Transfer-Encoding` of 8bit will be reconstructed
165 as 8bit if they have not been modified.
166
167 Optional *unixfrom* is a flag that forces the printing of the envelope
168 header delimiter before the first :rfc:`2822` header of the root message
169 object. If the root object has no envelope header, a standard one is
170 crafted. By default, this is set to ``False`` to inhibit the printing of
171 the envelope delimiter.
172
173 Note that for subparts, no envelope header is ever printed.
174
175 Optional *linesep* specifies the line separator character used to
R David Murray3edd22a2011-04-18 13:59:37 -0400176 terminate lines in the output. If specified it overrides the value
177 specified by the ``Generator``\ 's ``policy``.
R. David Murray8451c4b2010-10-23 22:19:56 +0000178
179 .. method:: clone(fp)
180
181 Return an independent clone of this :class:`BytesGenerator` instance with
182 the exact same options.
183
184 .. method:: write(s)
185
186 Write the string *s* to the underlying file object. *s* is encoded using
187 the ``ASCII`` codec and written to the *write* method of the *outfp*
188 *outfp* passed to the :class:`BytesGenerator`'s constructor. This
189 provides just enough file-like API for :class:`BytesGenerator` instances
190 to be used in the :func:`print` function.
R. David Murray96fd54e2010-10-08 15:55:28 +0000191
192 .. versionadded:: 3.2
193
Georg Brandl116aa622007-08-15 14:28:22 +0000194The :mod:`email.generator` module also provides a derived class, called
195:class:`DecodedGenerator` which is like the :class:`Generator` base class,
196except that non-\ :mimetype:`text` parts are substituted with a format string
197representing the part.
198
199
Georg Brandl3f076d82009-05-17 11:28:33 +0000200.. class:: DecodedGenerator(outfp[, mangle_from_=True, maxheaderlen=78, fmt=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000201
202 This class, derived from :class:`Generator` walks through all the subparts of a
203 message. If the subpart is of main type :mimetype:`text`, then it prints the
204 decoded payload of the subpart. Optional *_mangle_from_* and *maxheaderlen* are
205 as with the :class:`Generator` base class.
206
207 If the subpart is not of main type :mimetype:`text`, optional *fmt* is a format
208 string that is used instead of the message payload. *fmt* is expanded with the
209 following keywords, ``%(keyword)s`` format:
210
211 * ``type`` -- Full MIME type of the non-\ :mimetype:`text` part
212
213 * ``maintype`` -- Main MIME type of the non-\ :mimetype:`text` part
214
215 * ``subtype`` -- Sub-MIME type of the non-\ :mimetype:`text` part
216
217 * ``filename`` -- Filename of the non-\ :mimetype:`text` part
218
219 * ``description`` -- Description associated with the non-\ :mimetype:`text` part
220
221 * ``encoding`` -- Content transfer encoding of the non-\ :mimetype:`text` part
222
223 The default value for *fmt* is ``None``, meaning ::
224
225 [Non-text (%(type)s) part of message omitted, filename %(filename)s]