Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`email`: Representing an email message |
| 2 | ------------------------------------------- |
| 3 | |
| 4 | .. module:: email.message |
| 5 | :synopsis: The base class representing email messages. |
| 6 | |
| 7 | |
| 8 | The central class in the :mod:`email` package is the :class:`Message` class, |
| 9 | imported from the :mod:`email.message` module. It is the base class for the |
| 10 | :mod:`email` object model. :class:`Message` provides the core functionality for |
| 11 | setting and querying header fields, and for accessing message bodies. |
| 12 | |
| 13 | Conceptually, a :class:`Message` object consists of *headers* and *payloads*. |
| 14 | Headers are :rfc:`2822` style field names and values where the field name and |
| 15 | value are separated by a colon. The colon is not part of either the field name |
| 16 | or the field value. |
| 17 | |
| 18 | Headers are stored and returned in case-preserving form but are matched |
| 19 | case-insensitively. There may also be a single envelope header, also known as |
| 20 | the *Unix-From* header or the ``From_`` header. The payload is either a string |
| 21 | in the case of simple message objects or a list of :class:`Message` objects for |
| 22 | MIME container documents (e.g. :mimetype:`multipart/\*` and |
| 23 | :mimetype:`message/rfc822`). |
| 24 | |
| 25 | :class:`Message` objects provide a mapping style interface for accessing the |
| 26 | message headers, and an explicit interface for accessing both the headers and |
| 27 | the payload. It provides convenience methods for generating a flat text |
| 28 | representation of the message object tree, for accessing commonly used header |
| 29 | parameters, and for recursively walking over the object tree. |
| 30 | |
| 31 | Here are the methods of the :class:`Message` class: |
| 32 | |
| 33 | |
| 34 | .. class:: Message() |
| 35 | |
| 36 | The constructor takes no arguments. |
| 37 | |
| 38 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 39 | .. method:: as_string(unixfrom=False, maxheaderlen=0) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 41 | Return the entire message flattened as a string. When optional *unixfrom* |
| 42 | is ``True``, the envelope header is included in the returned string. |
R. David Murray | 101f278 | 2010-01-10 19:18:27 +0000 | [diff] [blame] | 43 | *unixfrom* defaults to ``False``. Flattening the message may trigger |
| 44 | changes to the :class:`Message` if defaults need to be filled in to |
| 45 | complete the transformation to a string (for example, MIME boundaries may |
| 46 | be generated or modified). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 48 | Note that this method is provided as a convenience and may not always |
| 49 | format the message the way you want. For example, by default it mangles |
| 50 | lines that begin with ``From``. For more flexibility, instantiate a |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 51 | :class:`~email.generator.Generator` instance and use its :meth:`flatten` |
| 52 | method directly. For example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
Georg Brandl | 0312494 | 2008-06-10 15:50:56 +0000 | [diff] [blame] | 54 | from io import StringIO |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 55 | from email.generator import Generator |
| 56 | fp = StringIO() |
| 57 | g = Generator(fp, mangle_from_=False, maxheaderlen=60) |
| 58 | g.flatten(msg) |
| 59 | text = fp.getvalue() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
| 61 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 62 | .. method:: __str__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 64 | Equivalent to ``as_string(unixfrom=True)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
| 66 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 67 | .. method:: is_multipart() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 69 | Return ``True`` if the message's payload is a list of sub-\ |
| 70 | :class:`Message` objects, otherwise return ``False``. When |
| 71 | :meth:`is_multipart` returns False, the payload should be a string object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | |
| 73 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 74 | .. method:: set_unixfrom(unixfrom) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 76 | Set the message's envelope header to *unixfrom*, which should be a string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | |
| 78 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 79 | .. method:: get_unixfrom() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 81 | Return the message's envelope header. Defaults to ``None`` if the |
| 82 | envelope header was never set. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
| 84 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 85 | .. method:: attach(payload) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 87 | Add the given *payload* to the current payload, which must be ``None`` or |
| 88 | a list of :class:`Message` objects before the call. After the call, the |
| 89 | payload will always be a list of :class:`Message` objects. If you want to |
| 90 | set the payload to a scalar object (e.g. a string), use |
| 91 | :meth:`set_payload` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | |
| 93 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 94 | .. method:: get_payload(i=None, decode=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 96 | Return the current payload, which will be a list of |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 97 | :class:`Message` objects when :meth:`is_multipart` is ``True``, or a |
| 98 | string when :meth:`is_multipart` is ``False``. If the payload is a list |
| 99 | and you mutate the list object, you modify the message's payload in place. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 101 | With optional argument *i*, :meth:`get_payload` will return the *i*-th |
| 102 | element of the payload, counting from zero, if :meth:`is_multipart` is |
| 103 | ``True``. An :exc:`IndexError` will be raised if *i* is less than 0 or |
| 104 | greater than or equal to the number of items in the payload. If the |
| 105 | payload is a string (i.e. :meth:`is_multipart` is ``False``) and *i* is |
| 106 | given, a :exc:`TypeError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 108 | Optional *decode* is a flag indicating whether the payload should be |
| 109 | decoded or not, according to the :mailheader:`Content-Transfer-Encoding` |
| 110 | header. When ``True`` and the message is not a multipart, the payload will |
| 111 | be decoded if this header's value is ``quoted-printable`` or ``base64``. |
| 112 | If some other encoding is used, or :mailheader:`Content-Transfer-Encoding` |
| 113 | header is missing, or if the payload has bogus base64 data, the payload is |
| 114 | returned as-is (undecoded). If the message is a multipart and the |
| 115 | *decode* flag is ``True``, then ``None`` is returned. The default for |
| 116 | *decode* is ``False``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | |
| 118 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 119 | .. method:: set_payload(payload, charset=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 120 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 121 | Set the entire message object's payload to *payload*. It is the client's |
| 122 | responsibility to ensure the payload invariants. Optional *charset* sets |
| 123 | the message's default character set; see :meth:`set_charset` for details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 125 | .. method:: set_charset(charset) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 126 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 127 | Set the character set of the payload to *charset*, which can either be a |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 128 | :class:`~email.charset.Charset` instance (see :mod:`email.charset`), a |
| 129 | string naming a character set, or ``None``. If it is a string, it will |
| 130 | be converted to a :class:`~email.charset.Charset` instance. If *charset* |
| 131 | is ``None``, the ``charset`` parameter will be removed from the |
| 132 | :mailheader:`Content-Type` header. Anything else will generate a |
| 133 | :exc:`TypeError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 134 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 135 | The message will be assumed to be of type :mimetype:`text/\*` encoded with |
| 136 | *charset.input_charset*. It will be converted to *charset.output_charset* |
| 137 | and encoded properly, if needed, when generating the plain text |
| 138 | representation of the message. MIME headers (:mailheader:`MIME-Version`, |
| 139 | :mailheader:`Content-Type`, :mailheader:`Content-Transfer-Encoding`) will |
| 140 | be added as needed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 141 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 142 | .. method:: get_charset() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 144 | Return the :class:`~email.charset.Charset` instance associated with the |
| 145 | message's payload. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 147 | The following methods implement a mapping-like interface for accessing the |
| 148 | message's :rfc:`2822` headers. Note that there are some semantic differences |
| 149 | between these methods and a normal mapping (i.e. dictionary) interface. For |
| 150 | example, in a dictionary there are no duplicate keys, but here there may be |
| 151 | duplicate message headers. Also, in dictionaries there is no guaranteed |
| 152 | order to the keys returned by :meth:`keys`, but in a :class:`Message` object, |
| 153 | headers are always returned in the order they appeared in the original |
| 154 | message, or were added to the message later. Any header deleted and then |
| 155 | re-added are always appended to the end of the header list. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 157 | These semantic differences are intentional and are biased toward maximal |
| 158 | convenience. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 160 | Note that in all cases, any envelope header present in the message is not |
| 161 | included in the mapping interface. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
| 163 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 164 | .. method:: __len__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 166 | Return the total number of headers, including duplicates. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 | |
| 168 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 169 | .. method:: __contains__(name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 171 | Return true if the message object has a field named *name*. Matching is |
| 172 | done case-insensitively and *name* should not include the trailing colon. |
| 173 | Used for the ``in`` operator, e.g.:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 175 | if 'message-id' in myMessage: |
| 176 | print('Message-ID:', myMessage['message-id']) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 | |
| 178 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 179 | .. method:: __getitem__(name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 180 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 181 | Return the value of the named header field. *name* should not include the |
| 182 | colon field separator. If the header is missing, ``None`` is returned; a |
| 183 | :exc:`KeyError` is never raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 184 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 185 | Note that if the named field appears more than once in the message's |
| 186 | headers, exactly which of those field values will be returned is |
| 187 | undefined. Use the :meth:`get_all` method to get the values of all the |
| 188 | extant named headers. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 189 | |
| 190 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 191 | .. method:: __setitem__(name, val) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 192 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 193 | Add a header to the message with field name *name* and value *val*. The |
| 194 | field is appended to the end of the message's existing fields. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 195 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 196 | Note that this does *not* overwrite or delete any existing header with the same |
| 197 | name. If you want to ensure that the new header is the only one present in the |
| 198 | message with field name *name*, delete the field first, e.g.:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 199 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 200 | del msg['subject'] |
| 201 | msg['subject'] = 'Python roolz!' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | |
| 203 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 204 | .. method:: __delitem__(name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 205 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 206 | Delete all occurrences of the field with name *name* from the message's |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 207 | headers. No exception is raised if the named field isn't present in the |
| 208 | headers. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 209 | |
| 210 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 211 | .. method:: Message.__contains__(name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 212 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 213 | Return true if the message contains a header field named *name*, otherwise |
| 214 | return false. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 215 | |
| 216 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 217 | .. method:: keys() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 219 | Return a list of all the message's header field names. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 220 | |
| 221 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 222 | .. method:: values() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 224 | Return a list of all the message's field values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 225 | |
| 226 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 227 | .. method:: items() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 228 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 229 | Return a list of 2-tuples containing all the message's field headers and |
| 230 | values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | |
| 232 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 233 | .. method:: get(name, failobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 234 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 235 | Return the value of the named header field. This is identical to |
| 236 | :meth:`__getitem__` except that optional *failobj* is returned if the |
| 237 | named header is missing (defaults to ``None``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 238 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 239 | Here are some additional useful methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 | |
| 241 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 242 | .. method:: get_all(name, failobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 243 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 244 | Return a list of all the values for the field named *name*. If there are |
| 245 | no such named headers in the message, *failobj* is returned (defaults to |
| 246 | ``None``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 247 | |
| 248 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 249 | .. method:: add_header(_name, _value, **_params) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 250 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 251 | Extended header setting. This method is similar to :meth:`__setitem__` |
| 252 | except that additional header parameters can be provided as keyword |
| 253 | arguments. *_name* is the header field to add and *_value* is the |
| 254 | *primary* value for the header. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 255 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 256 | For each item in the keyword argument dictionary *_params*, the key is |
| 257 | taken as the parameter name, with underscores converted to dashes (since |
| 258 | dashes are illegal in Python identifiers). Normally, the parameter will |
| 259 | be added as ``key="value"`` unless the value is ``None``, in which case |
| 260 | only the key will be added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 261 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 262 | Here's an example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 263 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 264 | msg.add_header('Content-Disposition', 'attachment', filename='bud.gif') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 265 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 266 | This will add a header that looks like :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 268 | Content-Disposition: attachment; filename="bud.gif" |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 269 | |
| 270 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 271 | .. method:: replace_header(_name, _value) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 272 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 273 | Replace a header. Replace the first header found in the message that |
| 274 | matches *_name*, retaining header order and field name case. If no |
| 275 | matching header was found, a :exc:`KeyError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 276 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 277 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 278 | .. method:: get_content_type() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 279 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 280 | Return the message's content type. The returned string is coerced to |
| 281 | lower case of the form :mimetype:`maintype/subtype`. If there was no |
| 282 | :mailheader:`Content-Type` header in the message the default type as given |
| 283 | by :meth:`get_default_type` will be returned. Since according to |
| 284 | :rfc:`2045`, messages always have a default type, :meth:`get_content_type` |
| 285 | will always return a value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 286 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 287 | :rfc:`2045` defines a message's default type to be :mimetype:`text/plain` |
| 288 | unless it appears inside a :mimetype:`multipart/digest` container, in |
| 289 | which case it would be :mimetype:`message/rfc822`. If the |
| 290 | :mailheader:`Content-Type` header has an invalid type specification, |
| 291 | :rfc:`2045` mandates that the default type be :mimetype:`text/plain`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 293 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 294 | .. method:: get_content_maintype() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 295 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 296 | Return the message's main content type. This is the :mimetype:`maintype` |
| 297 | part of the string returned by :meth:`get_content_type`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 298 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 299 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 300 | .. method:: get_content_subtype() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 301 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 302 | Return the message's sub-content type. This is the :mimetype:`subtype` |
| 303 | part of the string returned by :meth:`get_content_type`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 304 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 305 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 306 | .. method:: get_default_type() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 307 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 308 | Return the default content type. Most messages have a default content |
| 309 | type of :mimetype:`text/plain`, except for messages that are subparts of |
| 310 | :mimetype:`multipart/digest` containers. Such subparts have a default |
| 311 | content type of :mimetype:`message/rfc822`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 312 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 313 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 314 | .. method:: set_default_type(ctype) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 315 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 316 | Set the default content type. *ctype* should either be |
| 317 | :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is not |
| 318 | enforced. The default content type is not stored in the |
| 319 | :mailheader:`Content-Type` header. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 320 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 321 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 322 | .. method:: get_params(failobj=None, header='content-type', unquote=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 323 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 324 | Return the message's :mailheader:`Content-Type` parameters, as a list. |
| 325 | The elements of the returned list are 2-tuples of key/value pairs, as |
| 326 | split on the ``'='`` sign. The left hand side of the ``'='`` is the key, |
| 327 | while the right hand side is the value. If there is no ``'='`` sign in |
| 328 | the parameter the value is the empty string, otherwise the value is as |
| 329 | described in :meth:`get_param` and is unquoted if optional *unquote* is |
| 330 | ``True`` (the default). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 331 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 332 | Optional *failobj* is the object to return if there is no |
| 333 | :mailheader:`Content-Type` header. Optional *header* is the header to |
| 334 | search instead of :mailheader:`Content-Type`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 335 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 336 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 337 | .. method:: get_param(param, failobj=None, header='content-type', unquote=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 338 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 339 | Return the value of the :mailheader:`Content-Type` header's parameter |
| 340 | *param* as a string. If the message has no :mailheader:`Content-Type` |
| 341 | header or if there is no such parameter, then *failobj* is returned |
| 342 | (defaults to ``None``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 343 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 344 | Optional *header* if given, specifies the message header to use instead of |
| 345 | :mailheader:`Content-Type`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 346 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 347 | Parameter keys are always compared case insensitively. The return value |
| 348 | can either be a string, or a 3-tuple if the parameter was :rfc:`2231` |
| 349 | encoded. When it's a 3-tuple, the elements of the value are of the form |
| 350 | ``(CHARSET, LANGUAGE, VALUE)``. Note that both ``CHARSET`` and |
| 351 | ``LANGUAGE`` can be ``None``, in which case you should consider ``VALUE`` |
| 352 | to be encoded in the ``us-ascii`` charset. You can usually ignore |
| 353 | ``LANGUAGE``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 354 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 355 | If your application doesn't care whether the parameter was encoded as in |
| 356 | :rfc:`2231`, you can collapse the parameter value by calling |
Georg Brandl | 540b45c | 2009-04-27 16:45:26 +0000 | [diff] [blame] | 357 | :func:`email.utils.collapse_rfc2231_value`, passing in the return value |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 358 | from :meth:`get_param`. This will return a suitably decoded Unicode |
| 359 | string whn the value is a tuple, or the original string unquoted if it |
| 360 | isn't. For example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 361 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 362 | rawparam = msg.get_param('foo') |
Georg Brandl | 540b45c | 2009-04-27 16:45:26 +0000 | [diff] [blame] | 363 | param = email.utils.collapse_rfc2231_value(rawparam) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 364 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 365 | In any case, the parameter value (either the returned string, or the |
| 366 | ``VALUE`` item in the 3-tuple) is always unquoted, unless *unquote* is set |
| 367 | to ``False``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 368 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 369 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 370 | .. method:: set_param(param, value, header='Content-Type', requote=True, charset=None, language='') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 371 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 372 | Set a parameter in the :mailheader:`Content-Type` header. If the |
| 373 | parameter already exists in the header, its value will be replaced with |
| 374 | *value*. If the :mailheader:`Content-Type` header as not yet been defined |
| 375 | for this message, it will be set to :mimetype:`text/plain` and the new |
| 376 | parameter value will be appended as per :rfc:`2045`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 377 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 378 | Optional *header* specifies an alternative header to |
| 379 | :mailheader:`Content-Type`, and all parameters will be quoted as necessary |
| 380 | unless optional *requote* is ``False`` (the default is ``True``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 381 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 382 | If optional *charset* is specified, the parameter will be encoded |
| 383 | according to :rfc:`2231`. Optional *language* specifies the RFC 2231 |
| 384 | language, defaulting to the empty string. Both *charset* and *language* |
| 385 | should be strings. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 386 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 387 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 388 | .. method:: del_param(param, header='content-type', requote=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 389 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 390 | Remove the given parameter completely from the :mailheader:`Content-Type` |
| 391 | header. The header will be re-written in place without the parameter or |
| 392 | its value. All values will be quoted as necessary unless *requote* is |
| 393 | ``False`` (the default is ``True``). Optional *header* specifies an |
| 394 | alternative to :mailheader:`Content-Type`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 395 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 396 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 397 | .. method:: set_type(type, header='Content-Type', requote=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 398 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 399 | Set the main type and subtype for the :mailheader:`Content-Type` |
| 400 | header. *type* must be a string in the form :mimetype:`maintype/subtype`, |
| 401 | otherwise a :exc:`ValueError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 402 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 403 | This method replaces the :mailheader:`Content-Type` header, keeping all |
| 404 | the parameters in place. If *requote* is ``False``, this leaves the |
| 405 | existing header's quoting as is, otherwise the parameters will be quoted |
| 406 | (the default). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 407 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 408 | An alternative header can be specified in the *header* argument. When the |
| 409 | :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version` |
| 410 | header is also added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 411 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 412 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 413 | .. method:: get_filename(failobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 414 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 415 | Return the value of the ``filename`` parameter of the |
| 416 | :mailheader:`Content-Disposition` header of the message. If the header |
| 417 | does not have a ``filename`` parameter, this method falls back to looking |
R. David Murray | 9ed34be | 2010-03-04 17:38:18 +0000 | [diff] [blame] | 418 | for the ``name`` parameter on the :mailheader:`Content-Type` header. If |
| 419 | neither is found, or the header is missing, then *failobj* is returned. |
| 420 | The returned string will always be unquoted as per |
| 421 | :func:`email.utils.unquote`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 422 | |
| 423 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 424 | .. method:: get_boundary(failobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 425 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 426 | Return the value of the ``boundary`` parameter of the |
| 427 | :mailheader:`Content-Type` header of the message, or *failobj* if either |
| 428 | the header is missing, or has no ``boundary`` parameter. The returned |
Georg Brandl | 540b45c | 2009-04-27 16:45:26 +0000 | [diff] [blame] | 429 | string will always be unquoted as per :func:`email.utils.unquote`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 430 | |
| 431 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 432 | .. method:: set_boundary(boundary) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 433 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 434 | Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to |
| 435 | *boundary*. :meth:`set_boundary` will always quote *boundary* if |
| 436 | necessary. A :exc:`HeaderParseError` is raised if the message object has |
| 437 | no :mailheader:`Content-Type` header. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 438 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 439 | Note that using this method is subtly different than deleting the old |
| 440 | :mailheader:`Content-Type` header and adding a new one with the new |
| 441 | boundary via :meth:`add_header`, because :meth:`set_boundary` preserves |
| 442 | the order of the :mailheader:`Content-Type` header in the list of |
| 443 | headers. However, it does *not* preserve any continuation lines which may |
| 444 | have been present in the original :mailheader:`Content-Type` header. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 445 | |
| 446 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 447 | .. method:: get_content_charset(failobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 448 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 449 | Return the ``charset`` parameter of the :mailheader:`Content-Type` header, |
| 450 | coerced to lower case. If there is no :mailheader:`Content-Type` header, or if |
| 451 | that header has no ``charset`` parameter, *failobj* is returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 452 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 453 | Note that this method differs from :meth:`get_charset` which returns the |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 454 | :class:`~email.charset.Charset` instance for the default encoding of the message body. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 455 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 456 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 457 | .. method:: get_charsets(failobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 458 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 459 | Return a list containing the character set names in the message. If the |
| 460 | message is a :mimetype:`multipart`, then the list will contain one element |
| 461 | for each subpart in the payload, otherwise, it will be a list of length 1. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 462 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 463 | Each item in the list will be a string which is the value of the |
| 464 | ``charset`` parameter in the :mailheader:`Content-Type` header for the |
| 465 | represented subpart. However, if the subpart has no |
| 466 | :mailheader:`Content-Type` header, no ``charset`` parameter, or is not of |
| 467 | the :mimetype:`text` main MIME type, then that item in the returned list |
| 468 | will be *failobj*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 469 | |
| 470 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 471 | .. method:: walk() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 472 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 473 | The :meth:`walk` method is an all-purpose generator which can be used to |
| 474 | iterate over all the parts and subparts of a message object tree, in |
| 475 | depth-first traversal order. You will typically use :meth:`walk` as the |
| 476 | iterator in a ``for`` loop; each iteration returns the next subpart. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 477 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 478 | Here's an example that prints the MIME type of every part of a multipart |
| 479 | message structure:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 480 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 481 | >>> for part in msg.walk(): |
| 482 | ... print(part.get_content_type()) |
| 483 | multipart/report |
| 484 | text/plain |
| 485 | message/delivery-status |
| 486 | text/plain |
| 487 | text/plain |
| 488 | message/rfc822 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 489 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 490 | :class:`Message` objects can also optionally contain two instance attributes, |
| 491 | which can be used when generating the plain text of a MIME message. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 492 | |
| 493 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 494 | .. attribute:: preamble |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 495 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 496 | The format of a MIME document allows for some text between the blank line |
| 497 | following the headers, and the first multipart boundary string. Normally, |
| 498 | this text is never visible in a MIME-aware mail reader because it falls |
| 499 | outside the standard MIME armor. However, when viewing the raw text of |
| 500 | the message, or when viewing the message in a non-MIME aware reader, this |
| 501 | text can become visible. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 502 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 503 | The *preamble* attribute contains this leading extra-armor text for MIME |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 504 | documents. When the :class:`~email.parser.Parser` discovers some text |
| 505 | after the headers but before the first boundary string, it assigns this |
| 506 | text to the message's *preamble* attribute. When the |
| 507 | :class:`~email.generator.Generator` is writing out the plain text |
| 508 | representation of a MIME message, and it finds the |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 509 | message has a *preamble* attribute, it will write this text in the area |
| 510 | between the headers and the first boundary. See :mod:`email.parser` and |
| 511 | :mod:`email.generator` for details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 512 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 513 | Note that if the message object has no preamble, the *preamble* attribute |
| 514 | will be ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 515 | |
| 516 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 517 | .. attribute:: epilogue |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 518 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 519 | The *epilogue* attribute acts the same way as the *preamble* attribute, |
| 520 | except that it contains text that appears between the last boundary and |
| 521 | the end of the message. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 522 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 523 | You do not need to set the epilogue to the empty string in order for the |
| 524 | :class:`Generator` to print a newline at the end of the file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 525 | |
| 526 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 527 | .. attribute:: defects |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 528 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 529 | The *defects* attribute contains a list of all the problems found when |
| 530 | parsing this message. See :mod:`email.errors` for a detailed description |
| 531 | of the possible parsing defects. |