Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`email`: Generating MIME documents |
| 2 | --------------------------------------- |
| 3 | |
| 4 | .. module:: email.generator |
| 5 | :synopsis: Generate flat text email messages from a message structure. |
| 6 | |
| 7 | |
| 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. |
| 13 | |
| 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 |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 19 | structure via the :class:`~email.parser.Parser` class, and back to flat text, |
R. David Murray | 101f278 | 2010-01-10 19:18:27 +0000 | [diff] [blame] | 20 | is idempotent (the input is identical to the output). On the other hand, using |
| 21 | the Generator on a :class:`~email.message.Message` constructed by program may |
| 22 | result in changes to the :class:`~email.message.Message` object as defaults are |
| 23 | filled in. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
| 25 | Here are the public methods of the :class:`Generator` class, imported from the |
| 26 | :mod:`email.generator` module: |
| 27 | |
| 28 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 29 | .. class:: Generator(outfp, mangle_from_=True, maxheaderlen=78) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | |
| 31 | The constructor for the :class:`Generator` class takes a file-like object called |
| 32 | *outfp* for an argument. *outfp* must support the :meth:`write` method and be |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 33 | usable as the output file for the :func:`print` function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
| 35 | Optional *mangle_from_* is a flag that, when ``True``, puts a ``>`` character in |
| 36 | front of any line in the body that starts exactly as ``From``, i.e. ``From`` |
| 37 | followed by a space at the beginning of the line. This is the only guaranteed |
| 38 | portable way to avoid having such lines be mistaken for a Unix mailbox format |
| 39 | envelope header separator (see `WHY THE CONTENT-LENGTH FORMAT IS BAD |
| 40 | <http://www.jwz.org/doc/content-length.html>`_ for details). *mangle_from_* |
| 41 | defaults to ``True``, but you might want to set this to ``False`` if you are not |
| 42 | writing Unix mailbox format files. |
| 43 | |
| 44 | Optional *maxheaderlen* specifies the longest length for a non-continued header. |
| 45 | When a header line is longer than *maxheaderlen* (in characters, with tabs |
| 46 | expanded to 8 spaces), the header will be split as defined in the |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 47 | :class:`~email.header.Header` class. Set to zero to disable header wrapping. |
| 48 | The default is 78, as recommended (but not required) by :rfc:`2822`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 50 | The other public :class:`Generator` methods are: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
| 52 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 53 | .. method:: flatten(msg, unixfrom=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 55 | Print the textual representation of the message object structure rooted at |
| 56 | *msg* to the output file specified when the :class:`Generator` instance |
| 57 | was created. Subparts are visited depth-first and the resulting text will |
| 58 | be properly MIME encoded. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 60 | Optional *unixfrom* is a flag that forces the printing of the envelope |
| 61 | header delimiter before the first :rfc:`2822` header of the root message |
| 62 | object. If the root object has no envelope header, a standard one is |
| 63 | crafted. By default, this is set to ``False`` to inhibit the printing of |
| 64 | the envelope delimiter. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 66 | Note that for subparts, no envelope header is ever printed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 68 | .. method:: clone(fp) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 70 | Return an independent clone of this :class:`Generator` instance with the |
| 71 | exact same options. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 73 | .. method:: write(s) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 75 | Write the string *s* to the underlying file object, i.e. *outfp* passed to |
| 76 | :class:`Generator`'s constructor. This provides just enough file-like API |
| 77 | for :class:`Generator` instances to be used in the :func:`print` function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
| 79 | As a convenience, see the methods :meth:`Message.as_string` and |
| 80 | ``str(aMessage)``, a.k.a. :meth:`Message.__str__`, which simplify the generation |
| 81 | of a formatted string representation of a message object. For more detail, see |
| 82 | :mod:`email.message`. |
| 83 | |
| 84 | The :mod:`email.generator` module also provides a derived class, called |
| 85 | :class:`DecodedGenerator` which is like the :class:`Generator` base class, |
| 86 | except that non-\ :mimetype:`text` parts are substituted with a format string |
| 87 | representing the part. |
| 88 | |
| 89 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 90 | .. class:: DecodedGenerator(outfp[, mangle_from_=True, maxheaderlen=78, fmt=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
| 92 | This class, derived from :class:`Generator` walks through all the subparts of a |
| 93 | message. If the subpart is of main type :mimetype:`text`, then it prints the |
| 94 | decoded payload of the subpart. Optional *_mangle_from_* and *maxheaderlen* are |
| 95 | as with the :class:`Generator` base class. |
| 96 | |
| 97 | If the subpart is not of main type :mimetype:`text`, optional *fmt* is a format |
| 98 | string that is used instead of the message payload. *fmt* is expanded with the |
| 99 | following keywords, ``%(keyword)s`` format: |
| 100 | |
| 101 | * ``type`` -- Full MIME type of the non-\ :mimetype:`text` part |
| 102 | |
| 103 | * ``maintype`` -- Main MIME type of the non-\ :mimetype:`text` part |
| 104 | |
| 105 | * ``subtype`` -- Sub-MIME type of the non-\ :mimetype:`text` part |
| 106 | |
| 107 | * ``filename`` -- Filename of the non-\ :mimetype:`text` part |
| 108 | |
| 109 | * ``description`` -- Description associated with the non-\ :mimetype:`text` part |
| 110 | |
| 111 | * ``encoding`` -- Content transfer encoding of the non-\ :mimetype:`text` part |
| 112 | |
| 113 | The default value for *fmt* is ``None``, meaning :: |
| 114 | |
| 115 | [Non-text (%(type)s) part of message omitted, filename %(filename)s] |