Skip to content

Latest commit

 

History

History
239 lines (174 loc) · 11.5 KB

email.generator.rst

File metadata and controls

239 lines (174 loc) · 11.5 KB

:mod:`email.generator`: Generating MIME documents

.. module:: email.generator
   :synopsis: Generate flat text email messages from a message structure.

Source code: :source:`Lib/email/generator.py`


One of the most common tasks is to generate the flat text of the email message represented by a message object structure. You will need to do this if you want to send your message via the :mod:`smtplib` module or the :mod:`nntplib` module, or print the message on the console. Taking a message object structure and producing a flat text document is the job of the :class:`Generator` class.

Again, as with the :mod:`email.parser` module, you aren't limited to the functionality of the bundled generator; you could write one from scratch yourself. However the bundled generator knows how to generate most email in a standards-compliant way, should handle MIME and non-MIME email messages just fine, and is designed so that the transformation from flat text, to a message structure via the :class:`~email.parser.Parser` class, and back to flat text, is idempotent (the input is identical to the output) [1]. On the other hand, using the Generator on a :class:`~email.message.Message` constructed by program may result in changes to the :class:`~email.message.Message` object as defaults are filled in.

:class:`bytes` output can be generated using the :class:`BytesGenerator` class. If the message object structure contains non-ASCII bytes, this generator's :meth:`~BytesGenerator.flatten` method will emit the original bytes. Parsing a binary message and then flattening it with :class:`BytesGenerator` should be idempotent for standards compliant messages.

Here are the public methods of the :class:`Generator` class, imported from the :mod:`email.generator` module:

As a convenience, see the :class:`~email.message.Message` methods :meth:`~email.message.Message.as_string` and str(aMessage), a.k.a. :meth:`~email.message.Message.__str__`, which simplify the generation of a formatted string representation of a message object. For more detail, see :mod:`email.message`.

The :mod:`email.generator` module also provides a derived class, called :class:`DecodedGenerator` which is like the :class:`Generator` base class, except that non-:mimetype:`text` parts are substituted with a format string representing the part.

This class, derived from :class:`Generator` walks through all the subparts of a message. If the subpart is of main type :mimetype:`text`, then it prints the decoded payload of the subpart. Optional _mangle_from_ and maxheaderlen are as with the :class:`Generator` base class.

If the subpart is not of main type :mimetype:`text`, optional fmt is a format string that is used instead of the message payload. fmt is expanded with the following keywords, %(keyword)s format:

The default value for fmt is None, meaning

[Non-text (%(type)s) part of message omitted, filename %(filename)s]

Footnotes

[1]This statement assumes that you use the appropriate setting for the unixfrom argument, and that you set maxheaderlen=0 (which will preserve whatever the input line lengths were). It is also not strictly true, since in many cases runs of whitespace in headers are collapsed into single blanks. The latter is a bug that will eventually be fixed.