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 | ea1badb | 2012-05-15 22:07:52 -0400 | [diff] [blame^] | 20 | is idempotent (the input is identical to the output) [#]_. On the other hand, |
| 21 | using |
R. David Murray | 101f278 | 2010-01-10 19:18:27 +0000 | [diff] [blame] | 22 | the Generator on a :class:`~email.message.Message` constructed by program may |
| 23 | result in changes to the :class:`~email.message.Message` object as defaults are |
| 24 | filled in. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 26 | :class:`bytes` output can be generated using the :class:`BytesGenerator` class. |
| 27 | If the message object structure contains non-ASCII bytes, this generator's |
| 28 | :meth:`~BytesGenerator.flatten` method will emit the original bytes. Parsing a |
| 29 | binary message and then flattening it with :class:`BytesGenerator` should be |
| 30 | idempotent for standards compliant messages. |
| 31 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | Here are the public methods of the :class:`Generator` class, imported from the |
| 33 | :mod:`email.generator` module: |
| 34 | |
| 35 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 36 | .. class:: Generator(outfp, mangle_from_=True, maxheaderlen=78) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 | |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 38 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | |
| 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 Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 54 | :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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 57 | The other public :class:`Generator` methods are: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
| 59 | |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 60 | .. method:: flatten(msg, unixfrom=False, linesep='\\n') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 62 | Print the textual representation of the message object structure rooted at |
| 63 | *msg* to the output file specified when the :class:`Generator` instance |
| 64 | was created. Subparts are visited depth-first and the resulting text will |
| 65 | be properly MIME encoded. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 67 | Optional *unixfrom* is a flag that forces the printing of the envelope |
| 68 | header delimiter before the first :rfc:`2822` header of the root message |
| 69 | object. If the root object has no envelope header, a standard one is |
| 70 | crafted. By default, this is set to ``False`` to inhibit the printing of |
| 71 | the envelope delimiter. |
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 | Note that for subparts, no envelope header is ever printed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 75 | Optional *linesep* specifies the line separator character used to |
| 76 | terminate lines in the output. It defaults to ``\n`` because that is |
| 77 | the most useful value for Python application code (other library packages |
| 78 | expect ``\n`` separated lines). ``linesep=\r\n`` can be used to |
| 79 | generate output with RFC-compliant line separators. |
| 80 | |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 81 | Messages parsed with a Bytes parser that have a |
| 82 | :mailheader:`Content-Transfer-Encoding` of 8bit will be converted to a |
R. David Murray | 9253214 | 2011-01-07 23:25:30 +0000 | [diff] [blame] | 83 | use a 7bit Content-Transfer-Encoding. Non-ASCII bytes in the headers |
| 84 | will be :rfc:`2047` encoded with a charset of `unknown-8bit`. |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 85 | |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 86 | .. versionchanged:: 3.2 |
Georg Brandl | 872a702 | 2010-10-24 14:32:45 +0000 | [diff] [blame] | 87 | Added support for re-encoding 8bit message bodies, and the *linesep* |
| 88 | argument. |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 89 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 90 | .. method:: clone(fp) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 92 | Return an independent clone of this :class:`Generator` instance with the |
| 93 | exact same options. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 95 | .. method:: write(s) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 97 | Write the string *s* to the underlying file object, i.e. *outfp* passed to |
| 98 | :class:`Generator`'s constructor. This provides just enough file-like API |
| 99 | for :class:`Generator` instances to be used in the :func:`print` function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 101 | As a convenience, see the :class:`~email.message.Message` methods |
| 102 | :meth:`~email.message.Message.as_string` and ``str(aMessage)``, a.k.a. |
| 103 | :meth:`~email.message.Message.__str__`, which simplify the generation of a |
| 104 | formatted string representation of a message object. For more detail, see |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | :mod:`email.message`. |
| 106 | |
R. David Murray | f19076e | 2010-10-19 23:05:35 +0000 | [diff] [blame] | 107 | .. class:: BytesGenerator(outfp, mangle_from_=True, maxheaderlen=78) |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 108 | |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 109 | The constructor for the :class:`BytesGenerator` class takes a binary |
| 110 | :term:`file-like object` called *outfp* for an argument. *outfp* must |
| 111 | support a :meth:`write` method that accepts binary data. |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 112 | |
R. David Murray | 8451c4b | 2010-10-23 22:19:56 +0000 | [diff] [blame] | 113 | Optional *mangle_from_* is a flag that, when ``True``, puts a ``>`` |
| 114 | character in front of any line in the body that starts exactly as ``From``, |
| 115 | i.e. ``From`` followed by a space at the beginning of the line. This is the |
| 116 | only guaranteed portable way to avoid having such lines be mistaken for a |
| 117 | Unix mailbox format envelope header separator (see `WHY THE CONTENT-LENGTH |
| 118 | FORMAT IS BAD <http://www.jwz.org/doc/content-length.html>`_ for details). |
| 119 | *mangle_from_* defaults to ``True``, but you might want to set this to |
| 120 | ``False`` if you are not writing Unix mailbox format files. |
| 121 | |
| 122 | Optional *maxheaderlen* specifies the longest length for a non-continued |
| 123 | header. When a header line is longer than *maxheaderlen* (in characters, |
| 124 | with tabs expanded to 8 spaces), the header will be split as defined in the |
| 125 | :class:`~email.header.Header` class. Set to zero to disable header |
| 126 | wrapping. The default is 78, as recommended (but not required) by |
| 127 | :rfc:`2822`. |
| 128 | |
| 129 | The other public :class:`BytesGenerator` methods are: |
| 130 | |
| 131 | |
| 132 | .. method:: flatten(msg, unixfrom=False, linesep='\n') |
| 133 | |
| 134 | Print the textual representation of the message object structure rooted |
| 135 | at *msg* to the output file specified when the :class:`BytesGenerator` |
| 136 | instance was created. Subparts are visited depth-first and the resulting |
| 137 | text will be properly MIME encoded. If the input that created the *msg* |
| 138 | contained bytes with the high bit set and those bytes have not been |
| 139 | modified, they will be copied faithfully to the output, even if doing so |
| 140 | is not strictly RFC compliant. (To produce strictly RFC compliant |
| 141 | output, use the :class:`Generator` class.) |
| 142 | |
| 143 | Messages parsed with a Bytes parser that have a |
| 144 | :mailheader:`Content-Transfer-Encoding` of 8bit will be reconstructed |
| 145 | as 8bit if they have not been modified. |
| 146 | |
| 147 | Optional *unixfrom* is a flag that forces the printing of the envelope |
| 148 | header delimiter before the first :rfc:`2822` header of the root message |
| 149 | object. If the root object has no envelope header, a standard one is |
| 150 | crafted. By default, this is set to ``False`` to inhibit the printing of |
| 151 | the envelope delimiter. |
| 152 | |
| 153 | Note that for subparts, no envelope header is ever printed. |
| 154 | |
| 155 | Optional *linesep* specifies the line separator character used to |
| 156 | terminate lines in the output. It defaults to ``\n`` because that is |
| 157 | the most useful value for Python application code (other library packages |
| 158 | expect ``\n`` separated lines). ``linesep=\r\n`` can be used to |
| 159 | generate output with RFC-compliant line separators. |
| 160 | |
| 161 | .. method:: clone(fp) |
| 162 | |
| 163 | Return an independent clone of this :class:`BytesGenerator` instance with |
| 164 | the exact same options. |
| 165 | |
| 166 | .. method:: write(s) |
| 167 | |
| 168 | Write the string *s* to the underlying file object. *s* is encoded using |
| 169 | the ``ASCII`` codec and written to the *write* method of the *outfp* |
| 170 | *outfp* passed to the :class:`BytesGenerator`'s constructor. This |
| 171 | provides just enough file-like API for :class:`BytesGenerator` instances |
| 172 | to be used in the :func:`print` function. |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 173 | |
| 174 | .. versionadded:: 3.2 |
| 175 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 | The :mod:`email.generator` module also provides a derived class, called |
| 177 | :class:`DecodedGenerator` which is like the :class:`Generator` base class, |
| 178 | except that non-\ :mimetype:`text` parts are substituted with a format string |
| 179 | representing the part. |
| 180 | |
| 181 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 182 | .. class:: DecodedGenerator(outfp[, mangle_from_=True, maxheaderlen=78, fmt=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 183 | |
| 184 | This class, derived from :class:`Generator` walks through all the subparts of a |
| 185 | message. If the subpart is of main type :mimetype:`text`, then it prints the |
| 186 | decoded payload of the subpart. Optional *_mangle_from_* and *maxheaderlen* are |
| 187 | as with the :class:`Generator` base class. |
| 188 | |
| 189 | If the subpart is not of main type :mimetype:`text`, optional *fmt* is a format |
| 190 | string that is used instead of the message payload. *fmt* is expanded with the |
| 191 | following keywords, ``%(keyword)s`` format: |
| 192 | |
| 193 | * ``type`` -- Full MIME type of the non-\ :mimetype:`text` part |
| 194 | |
| 195 | * ``maintype`` -- Main MIME type of the non-\ :mimetype:`text` part |
| 196 | |
| 197 | * ``subtype`` -- Sub-MIME type of the non-\ :mimetype:`text` part |
| 198 | |
| 199 | * ``filename`` -- Filename of the non-\ :mimetype:`text` part |
| 200 | |
| 201 | * ``description`` -- Description associated with the non-\ :mimetype:`text` part |
| 202 | |
| 203 | * ``encoding`` -- Content transfer encoding of the non-\ :mimetype:`text` part |
| 204 | |
| 205 | The default value for *fmt* is ``None``, meaning :: |
| 206 | |
| 207 | [Non-text (%(type)s) part of message omitted, filename %(filename)s] |
R David Murray | ea1badb | 2012-05-15 22:07:52 -0400 | [diff] [blame^] | 208 | |
| 209 | |
| 210 | .. rubric:: Footnotes |
| 211 | |
| 212 | .. [#] This statement assumes that you use the appropriate setting for the |
| 213 | ``unixfrom`` argument, and that you set maxheaderlen=0 (which will |
| 214 | preserve whatever the input line lengths were). It is also not strictly |
| 215 | true, since in many cases runs of whitespace in headers are collapsed |
| 216 | into single blanks. The latter is a bug that will eventually be fixed. |