blob: 48d41e1dc78e962968f0beb1276130eb9c871cb1 [file] [log] [blame]
R David Murray79cf3ba2012-05-27 17:10:36 -04001:mod:`email.generator`: Generating MIME documents
2-------------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +00003
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 Murrayea1badb2012-05-15 22:07:52 -040020is idempotent (the input is identical to the output) [#]_. On the other hand,
R David Murray28e68ea2012-05-15 22:13:29 -040021using the Generator on a :class:`~email.message.Message` constructed by program
22may result in changes to the :class:`~email.message.Message` object as defaults
23are filled 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 Murrayc27e5222012-05-25 15:01:48 -040035.. class:: Generator(outfp, mangle_from_=True, maxheaderlen=78, *, policy=None)
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
R David Murray3edd22a2011-04-18 13:59:37 -040056 The *policy* keyword specifies a :mod:`~email.policy` object that controls a
R David Murrayc27e5222012-05-25 15:01:48 -040057 number of aspects of the generator's operation. If no *policy* is specified,
Georg Brandl3539afd2012-05-30 22:03:20 +020058 then the *policy* attached to the message object passed to :attr:`flatten`
R David Murrayc27e5222012-05-25 15:01:48 -040059 is used.
R David Murray3edd22a2011-04-18 13:59:37 -040060
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
R David Murrayc27e5222012-05-25 15:01:48 -040083 specified by the *msg*\'s or ``Generator``\'s ``policy``.
R. David Murray8451c4b2010-10-23 22:19:56 +000084
R David Murrayc27e5222012-05-25 15:01:48 -040085 Because strings cannot represent non-ASCII bytes, if the policy that
86 applies when ``flatten`` is run has :attr:`~email.policy.Policy.cte_type`
87 set to ``8bit``, ``Generator`` will operate as if it were set to
88 ``7bit``. This means that messages parsed with a Bytes parser that have
89 a :mailheader:`Content-Transfer-Encoding` of ``8bit`` will be converted
90 to a use a ``7bit`` Content-Transfer-Encoding. Non-ASCII bytes in the
91 headers 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
R David Murrayc27e5222012-05-25 15:01:48 -040094 Added support for re-encoding ``8bit`` message bodies, and the
95 *linesep* 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, *, \
R David Murraye2524462014-05-06 21:33:18 -0400115 policy=None)
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 Murraye2524462014-05-06 21:33:18 -0400137
R David Murray3edd22a2011-04-18 13:59:37 -0400138 The *policy* keyword specifies a :mod:`~email.policy` object that controls a
R David Murraye2524462014-05-06 21:33:18 -0400139 number of aspects of the generator's operation. If no *policy* is specified,
140 then the *policy* attached to the message object passed to :attr:`flatten`
141 is used.
R David Murray3edd22a2011-04-18 13:59:37 -0400142
143 .. versionchanged:: 3.3 Added the *policy* keyword.
144
R. David Murray8451c4b2010-10-23 22:19:56 +0000145 The other public :class:`BytesGenerator` methods are:
146
147
R David Murray3edd22a2011-04-18 13:59:37 -0400148 .. method:: flatten(msg, unixfrom=False, linesep=None)
R. David Murray8451c4b2010-10-23 22:19:56 +0000149
150 Print the textual representation of the message object structure rooted
151 at *msg* to the output file specified when the :class:`BytesGenerator`
152 instance was created. Subparts are visited depth-first and the resulting
R David Murray3edd22a2011-04-18 13:59:37 -0400153 text will be properly MIME encoded. If the :mod:`~email.policy` option
R David Murrayc27e5222012-05-25 15:01:48 -0400154 :attr:`~email.policy.Policy.cte_type` is ``8bit`` (the default),
R David Murray3edd22a2011-04-18 13:59:37 -0400155 then any bytes with the high bit set in the original parsed message that
156 have not been modified will be copied faithfully to the output. If
R David Murrayc27e5222012-05-25 15:01:48 -0400157 ``cte_type`` is ``7bit``, the bytes will be converted as needed
158 using an ASCII-compatible Content-Transfer-Encoding. In particular,
159 RFC-invalid non-ASCII bytes in headers will be encoded using the MIME
160 ``unknown-8bit`` character set, thus rendering them RFC-compliant.
R David Murray3edd22a2011-04-18 13:59:37 -0400161
162 .. XXX: There should be a complementary option that just does the RFC
163 compliance transformation but leaves CTE 8bit parts alone.
R. David Murray8451c4b2010-10-23 22:19:56 +0000164
165 Messages parsed with a Bytes parser that have a
166 :mailheader:`Content-Transfer-Encoding` of 8bit will be reconstructed
167 as 8bit if they have not been modified.
168
169 Optional *unixfrom* is a flag that forces the printing of the envelope
170 header delimiter before the first :rfc:`2822` header of the root message
171 object. If the root object has no envelope header, a standard one is
172 crafted. By default, this is set to ``False`` to inhibit the printing of
173 the envelope delimiter.
174
175 Note that for subparts, no envelope header is ever printed.
176
177 Optional *linesep* specifies the line separator character used to
R David Murray3edd22a2011-04-18 13:59:37 -0400178 terminate lines in the output. If specified it overrides the value
R David Murraye2524462014-05-06 21:33:18 -0400179 specified by the ``Generator``\ or *msg*\ 's ``policy``.
R. David Murray8451c4b2010-10-23 22:19:56 +0000180
181 .. method:: clone(fp)
182
183 Return an independent clone of this :class:`BytesGenerator` instance with
184 the exact same options.
185
186 .. method:: write(s)
187
188 Write the string *s* to the underlying file object. *s* is encoded using
189 the ``ASCII`` codec and written to the *write* method of the *outfp*
190 *outfp* passed to the :class:`BytesGenerator`'s constructor. This
191 provides just enough file-like API for :class:`BytesGenerator` instances
192 to be used in the :func:`print` function.
R. David Murray96fd54e2010-10-08 15:55:28 +0000193
194 .. versionadded:: 3.2
195
Georg Brandl116aa622007-08-15 14:28:22 +0000196The :mod:`email.generator` module also provides a derived class, called
197:class:`DecodedGenerator` which is like the :class:`Generator` base class,
198except that non-\ :mimetype:`text` parts are substituted with a format string
199representing the part.
200
201
Hynek Schlawackdfa46522012-05-21 11:01:54 +0200202.. class:: DecodedGenerator(outfp, mangle_from_=True, maxheaderlen=78, fmt=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000203
204 This class, derived from :class:`Generator` walks through all the subparts of a
205 message. If the subpart is of main type :mimetype:`text`, then it prints the
206 decoded payload of the subpart. Optional *_mangle_from_* and *maxheaderlen* are
207 as with the :class:`Generator` base class.
208
209 If the subpart is not of main type :mimetype:`text`, optional *fmt* is a format
210 string that is used instead of the message payload. *fmt* is expanded with the
211 following keywords, ``%(keyword)s`` format:
212
213 * ``type`` -- Full MIME type of the non-\ :mimetype:`text` part
214
215 * ``maintype`` -- Main MIME type of the non-\ :mimetype:`text` part
216
217 * ``subtype`` -- Sub-MIME type of the non-\ :mimetype:`text` part
218
219 * ``filename`` -- Filename of the non-\ :mimetype:`text` part
220
221 * ``description`` -- Description associated with the non-\ :mimetype:`text` part
222
223 * ``encoding`` -- Content transfer encoding of the non-\ :mimetype:`text` part
224
225 The default value for *fmt* is ``None``, meaning ::
226
227 [Non-text (%(type)s) part of message omitted, filename %(filename)s]
R David Murrayea1badb2012-05-15 22:07:52 -0400228
229
230.. rubric:: Footnotes
231
232.. [#] This statement assumes that you use the appropriate setting for the
233 ``unixfrom`` argument, and that you set maxheaderlen=0 (which will
234 preserve whatever the input line lengths were). It is also not strictly
235 true, since in many cases runs of whitespace in headers are collapsed
236 into single blanks. The latter is a bug that will eventually be fixed.