Added section on passing contextual information to logging and documentation for...
[python.git] / Doc / library / email.generator.rst
blobbb1f57d0dfd04b7c677a290767e04381e65cec01
1 :mod:`email`: Generating MIME documents
2 ---------------------------------------
4 .. module:: email.generator
5    :synopsis: Generate flat text email messages from a message structure.
8 One of the most common tasks is to generate the flat text of the email message
9 represented by a message object structure.  You will need to do this if you want
10 to send your message via the :mod:`smtplib` module or the :mod:`nntplib` module,
11 or print the message on the console.  Taking a message object structure and
12 producing a flat text document is the job of the :class:`Generator` class.
14 Again, as with the :mod:`email.parser` module, you aren't limited to the
15 functionality of the bundled generator; you could write one from scratch
16 yourself.  However the bundled generator knows how to generate most email in a
17 standards-compliant way, should handle MIME and non-MIME email messages just
18 fine, and is designed so that the transformation from flat text, to a message
19 structure via the :class:`Parser` class, and back to flat text, is idempotent
20 (the input is identical to the output).
22 Here are the public methods of the :class:`Generator` class, imported from the
23 :mod:`email.generator` module:
26 .. class:: Generator(outfp[, mangle_from_[, maxheaderlen]])
28    The constructor for the :class:`Generator` class takes a file-like object called
29    *outfp* for an argument.  *outfp* must support the :meth:`write` method and be
30    usable as the output file in a Python extended print statement.
32    Optional *mangle_from_* is a flag that, when ``True``, puts a ``>`` character in
33    front of any line in the body that starts exactly as ``From``, i.e. ``From``
34    followed by a space at the beginning of the line.  This is the only guaranteed
35    portable way to avoid having such lines be mistaken for a Unix mailbox format
36    envelope header separator (see `WHY THE CONTENT-LENGTH FORMAT IS BAD
37    <http://www.jwz.org/doc/content-length.html>`_ for details).  *mangle_from_*
38    defaults to ``True``, but you might want to set this to ``False`` if you are not
39    writing Unix mailbox format files.
41    Optional *maxheaderlen* specifies the longest length for a non-continued header.
42    When a header line is longer than *maxheaderlen* (in characters, with tabs
43    expanded to 8 spaces), the header will be split as defined in the
44    :mod:`email.header.Header` class.  Set to zero to disable header wrapping.  The
45    default is 78, as recommended (but not required) by :rfc:`2822`.
47 The other public :class:`Generator` methods are:
50 .. method:: Generator.flatten(msg[, unixfrom])
52    Print the textual representation of the message object structure rooted at *msg*
53    to the output file specified when the :class:`Generator` instance was created.
54    Subparts are visited depth-first and the resulting text will be properly MIME
55    encoded.
57    Optional *unixfrom* is a flag that forces the printing of the envelope header
58    delimiter before the first :rfc:`2822` header of the root message object.  If
59    the root object has no envelope header, a standard one is crafted.  By default,
60    this is set to ``False`` to inhibit the printing of the envelope delimiter.
62    Note that for subparts, no envelope header is ever printed.
64    .. versionadded:: 2.2.2
67 .. method:: Generator.clone(fp)
69    Return an independent clone of this :class:`Generator` instance with the exact
70    same options.
72    .. versionadded:: 2.2.2
75 .. method:: Generator.write(s)
77    Write the string *s* to the underlying file object, i.e. *outfp* passed to
78    :class:`Generator`'s constructor.  This provides just enough file-like API for
79    :class:`Generator` instances to be used in extended print statements.
81 As a convenience, see the methods :meth:`Message.as_string` and
82 ``str(aMessage)``, a.k.a. :meth:`Message.__str__`, which simplify the generation
83 of a formatted string representation of a message object.  For more detail, see
84 :mod:`email.message`.
86 The :mod:`email.generator` module also provides a derived class, called
87 :class:`DecodedGenerator` which is like the :class:`Generator` base class,
88 except that non-\ :mimetype:`text` parts are substituted with a format string
89 representing the part.
92 .. class:: DecodedGenerator(outfp[, mangle_from_[, maxheaderlen[, fmt]]])
94    This class, derived from :class:`Generator` walks through all the subparts of a
95    message.  If the subpart is of main type :mimetype:`text`, then it prints the
96    decoded payload of the subpart. Optional *_mangle_from_* and *maxheaderlen* are
97    as with the :class:`Generator` base class.
99    If the subpart is not of main type :mimetype:`text`, optional *fmt* is a format
100    string that is used instead of the message payload. *fmt* is expanded with the
101    following keywords, ``%(keyword)s`` format:
103    * ``type`` -- Full MIME type of the non-\ :mimetype:`text` part
105    * ``maintype`` -- Main MIME type of the non-\ :mimetype:`text` part
107    * ``subtype`` -- Sub-MIME type of the non-\ :mimetype:`text` part
109    * ``filename`` -- Filename of the non-\ :mimetype:`text` part
111    * ``description`` -- Description associated with the non-\ :mimetype:`text` part
113    * ``encoding`` -- Content transfer encoding of the non-\ :mimetype:`text` part
115    The default value for *fmt* is ``None``, meaning ::
117       [Non-text (%(type)s) part of message omitted, filename %(filename)s]
119    .. versionadded:: 2.2.2
121 .. versionchanged:: 2.5
122    The previously deprecated method :meth:`__call__` was removed.