R David Murray | 79cf3ba | 2012-05-27 17:10:36 -0400 | [diff] [blame] | 1 | :mod:`email.message`: Representing an email message |
| 2 | --------------------------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: email.message |
| 5 | :synopsis: The base class representing email messages. |
| 6 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | **Source code:** :source:`Lib/email/message.py` |
| 8 | |
| 9 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
| 11 | The central class in the :mod:`email` package is the :class:`Message` class, |
| 12 | imported from the :mod:`email.message` module. It is the base class for the |
| 13 | :mod:`email` object model. :class:`Message` provides the core functionality for |
| 14 | setting and querying header fields, and for accessing message bodies. |
| 15 | |
| 16 | Conceptually, a :class:`Message` object consists of *headers* and *payloads*. |
| 17 | Headers are :rfc:`2822` style field names and values where the field name and |
| 18 | value are separated by a colon. The colon is not part of either the field name |
| 19 | or the field value. |
| 20 | |
| 21 | Headers are stored and returned in case-preserving form but are matched |
| 22 | case-insensitively. There may also be a single envelope header, also known as |
| 23 | the *Unix-From* header or the ``From_`` header. The payload is either a string |
| 24 | in the case of simple message objects or a list of :class:`Message` objects for |
| 25 | MIME container documents (e.g. :mimetype:`multipart/\*` and |
| 26 | :mimetype:`message/rfc822`). |
| 27 | |
| 28 | :class:`Message` objects provide a mapping style interface for accessing the |
| 29 | message headers, and an explicit interface for accessing both the headers and |
| 30 | the payload. It provides convenience methods for generating a flat text |
| 31 | representation of the message object tree, for accessing commonly used header |
| 32 | parameters, and for recursively walking over the object tree. |
| 33 | |
| 34 | Here are the methods of the :class:`Message` class: |
| 35 | |
| 36 | |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 37 | .. class:: Message(policy=compat32) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 39 | If *policy* is specified (it must be an instance of a :mod:`~email.policy` |
R David Murray | a83ade1 | 2014-05-08 10:05:47 -0400 | [diff] [blame] | 40 | class) use the rules it specifies to update and serialize the representation |
Georg Brandl | ed007d5 | 2013-11-24 16:09:26 +0100 | [diff] [blame] | 41 | of the message. If *policy* is not set, use the :class:`compat32 |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 42 | <email.policy.Compat32>` policy, which maintains backward compatibility with |
| 43 | the Python 3.2 version of the email package. For more information see the |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 44 | :mod:`~email.policy` documentation. |
| 45 | |
| 46 | .. versionchanged:: 3.3 The *policy* keyword argument was added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | |
| 48 | |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 49 | .. method:: as_string(unixfrom=False, maxheaderlen=0, policy=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 51 | Return the entire message flattened as a string. When optional *unixfrom* |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 52 | is true, the envelope header is included in the returned string. |
| 53 | *unixfrom* defaults to ``False``. For backward compabitility reasons, |
| 54 | *maxheaderlen* defaults to ``0``, so if you want a different value you |
| 55 | must override it explicitly (the value specified for *max_line_length* in |
| 56 | the policy will be ignored by this method). The *policy* argument may be |
| 57 | used to override the default policy obtained from the message instance. |
| 58 | This can be used to control some of the formatting produced by the |
| 59 | method, since the specified *policy* will be passed to the ``Generator``. |
| 60 | |
| 61 | Flattening the message may trigger changes to the :class:`Message` if |
| 62 | defaults need to be filled in to complete the transformation to a string |
| 63 | (for example, MIME boundaries may be generated or modified). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 65 | Note that this method is provided as a convenience and may not always |
R David Murray | 7dedcb4 | 2011-03-15 14:01:18 -0400 | [diff] [blame] | 66 | format the message the way you want. For example, by default it does |
| 67 | not do the mangling of lines that begin with ``From`` that is |
| 68 | required by the unix mbox format. For more flexibility, instantiate a |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 69 | :class:`~email.generator.Generator` instance and use its |
| 70 | :meth:`~email.generator.Generator.flatten` method directly. For example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
Georg Brandl | 0312494 | 2008-06-10 15:50:56 +0000 | [diff] [blame] | 72 | from io import StringIO |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 73 | from email.generator import Generator |
| 74 | fp = StringIO() |
R David Murray | 7dedcb4 | 2011-03-15 14:01:18 -0400 | [diff] [blame] | 75 | g = Generator(fp, mangle_from_=True, maxheaderlen=60) |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 76 | g.flatten(msg) |
| 77 | text = fp.getvalue() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 79 | If the message object contains binary data that is not encoded according |
| 80 | to RFC standards, the non-compliant data will be replaced by unicode |
| 81 | "unknown character" code points. (See also :meth:`.as_bytes` and |
| 82 | :class:`~email.generator.BytesGenerator`.) |
| 83 | |
| 84 | .. versionchanged:: 3.4 the *policy* keyword argument was added. |
| 85 | |
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 | .. method:: __str__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 89 | Equivalent to :meth:`.as_string()`. Allows ``str(msg)`` to produce a |
| 90 | string containing the formatted message. |
| 91 | |
| 92 | |
| 93 | .. method:: as_bytes(unixfrom=False, policy=None) |
| 94 | |
| 95 | Return the entire message flattened as a bytes object. When optional |
| 96 | *unixfrom* is true, the envelope header is included in the returned |
| 97 | string. *unixfrom* defaults to ``False``. The *policy* argument may be |
| 98 | used to override the default policy obtained from the message instance. |
| 99 | This can be used to control some of the formatting produced by the |
| 100 | method, since the specified *policy* will be passed to the |
| 101 | ``BytesGenerator``. |
| 102 | |
| 103 | Flattening the message may trigger changes to the :class:`Message` if |
| 104 | defaults need to be filled in to complete the transformation to a string |
| 105 | (for example, MIME boundaries may be generated or modified). |
| 106 | |
| 107 | Note that this method is provided as a convenience and may not always |
| 108 | format the message the way you want. For example, by default it does |
| 109 | not do the mangling of lines that begin with ``From`` that is |
| 110 | required by the unix mbox format. For more flexibility, instantiate a |
| 111 | :class:`~email.generator.BytesGenerator` instance and use its |
Serhiy Storchaka | 319f3a1 | 2013-08-19 10:03:11 +0300 | [diff] [blame] | 112 | :meth:`~email.generator.BytesGenerator.flatten` method directly. |
| 113 | For example:: |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 114 | |
| 115 | from io import BytesIO |
| 116 | from email.generator import BytesGenerator |
| 117 | fp = BytesIO() |
| 118 | g = BytesGenerator(fp, mangle_from_=True, maxheaderlen=60) |
| 119 | g.flatten(msg) |
| 120 | text = fp.getvalue() |
| 121 | |
| 122 | .. versionadded:: 3.4 |
| 123 | |
| 124 | |
| 125 | .. method:: __bytes__() |
| 126 | |
| 127 | Equivalent to :meth:`.as_bytes()`. Allows ``bytes(msg)`` to produce a |
| 128 | bytes object containing the formatted message. |
| 129 | |
| 130 | .. versionadded:: 3.4 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 | |
| 132 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 133 | .. method:: is_multipart() |
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 | Return ``True`` if the message's payload is a list of sub-\ |
| 136 | :class:`Message` objects, otherwise return ``False``. When |
R David Murray | 9cc5fd7 | 2014-09-27 15:37:40 -0400 | [diff] [blame] | 137 | :meth:`is_multipart` returns ``False``, the payload should be a string |
| 138 | object. (Note that :meth:`is_multipart` returning ``True`` does not |
| 139 | necessarily mean that "msg.get_content_maintype() == 'multipart'" will |
| 140 | return the ``True``. For example, ``is_multipart`` will return ``True`` |
| 141 | when the :class:`Message` is of type ``message/rfc822``.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 142 | |
| 143 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 144 | .. method:: set_unixfrom(unixfrom) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 146 | 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] | 147 | |
| 148 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 149 | .. method:: get_unixfrom() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 151 | Return the message's envelope header. Defaults to ``None`` if the |
| 152 | envelope header was never set. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | |
| 154 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 155 | .. method:: attach(payload) |
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 | Add the given *payload* to the current payload, which must be ``None`` or |
| 158 | a list of :class:`Message` objects before the call. After the call, the |
| 159 | payload will always be a list of :class:`Message` objects. If you want to |
| 160 | set the payload to a scalar object (e.g. a string), use |
| 161 | :meth:`set_payload` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
| 163 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 164 | .. method:: get_payload(i=None, decode=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 166 | Return the current payload, which will be a list of |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 167 | :class:`Message` objects when :meth:`is_multipart` is ``True``, or a |
| 168 | string when :meth:`is_multipart` is ``False``. If the payload is a list |
| 169 | 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] | 170 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 171 | With optional argument *i*, :meth:`get_payload` will return the *i*-th |
| 172 | element of the payload, counting from zero, if :meth:`is_multipart` is |
| 173 | ``True``. An :exc:`IndexError` will be raised if *i* is less than 0 or |
| 174 | greater than or equal to the number of items in the payload. If the |
| 175 | payload is a string (i.e. :meth:`is_multipart` is ``False``) and *i* is |
| 176 | given, a :exc:`TypeError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 178 | Optional *decode* is a flag indicating whether the payload should be |
| 179 | decoded or not, according to the :mailheader:`Content-Transfer-Encoding` |
| 180 | header. When ``True`` and the message is not a multipart, the payload will |
| 181 | be decoded if this header's value is ``quoted-printable`` or ``base64``. |
| 182 | If some other encoding is used, or :mailheader:`Content-Transfer-Encoding` |
R David Murray | 80e0aee | 2012-05-27 21:23:34 -0400 | [diff] [blame] | 183 | header is missing, the payload is |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 184 | returned as-is (undecoded). In all cases the returned value is binary |
| 185 | data. If the message is a multipart and the *decode* flag is ``True``, |
R David Murray | 80e0aee | 2012-05-27 21:23:34 -0400 | [diff] [blame] | 186 | then ``None`` is returned. If the payload is base64 and it was not |
| 187 | perfectly formed (missing padding, characters outside the base64 |
| 188 | alphabet), then an appropriate defect will be added to the message's |
| 189 | defect property (:class:`~email.errors.InvalidBase64PaddingDefect` or |
| 190 | :class:`~email.errors.InvalidBase64CharactersDefect`, respectively). |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 191 | |
| 192 | When *decode* is ``False`` (the default) the body is returned as a string |
| 193 | without decoding the :mailheader:`Content-Transfer-Encoding`. However, |
| 194 | for a :mailheader:`Content-Transfer-Encoding` of 8bit, an attempt is made |
Senthil Kumaran | 8227045 | 2010-10-15 13:29:33 +0000 | [diff] [blame] | 195 | to decode the original bytes using the ``charset`` specified by the |
| 196 | :mailheader:`Content-Type` header, using the ``replace`` error handler. |
| 197 | If no ``charset`` is specified, or if the ``charset`` given is not |
| 198 | recognized by the email package, the body is decoded using the default |
| 199 | ASCII charset. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 200 | |
| 201 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 202 | .. method:: set_payload(payload, charset=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 204 | Set the entire message object's payload to *payload*. It is the client's |
| 205 | responsibility to ensure the payload invariants. Optional *charset* sets |
R David Murray | 27e9de6 | 2014-02-07 12:40:37 -0500 | [diff] [blame] | 206 | the message's default character set; see :meth:`set_charset` for details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 207 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 208 | .. method:: set_charset(charset) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 209 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 210 | 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] | 211 | :class:`~email.charset.Charset` instance (see :mod:`email.charset`), a |
| 212 | string naming a character set, or ``None``. If it is a string, it will |
| 213 | be converted to a :class:`~email.charset.Charset` instance. If *charset* |
| 214 | is ``None``, the ``charset`` parameter will be removed from the |
R David Murray | e3d09ff | 2011-03-15 17:41:13 -0400 | [diff] [blame] | 215 | :mailheader:`Content-Type` header (the message will not be otherwise |
| 216 | modified). Anything else will generate a :exc:`TypeError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 | |
R David Murray | e3d09ff | 2011-03-15 17:41:13 -0400 | [diff] [blame] | 218 | If there is no existing :mailheader:`MIME-Version` header one will be |
| 219 | added. If there is no existing :mailheader:`Content-Type` header, one |
| 220 | will be added with a value of :mimetype:`text/plain`. Whether the |
| 221 | :mailheader:`Content-Type` header already exists or not, its ``charset`` |
| 222 | parameter will be set to *charset.output_charset*. If |
| 223 | *charset.input_charset* and *charset.output_charset* differ, the payload |
| 224 | will be re-encoded to the *output_charset*. If there is no existing |
| 225 | :mailheader:`Content-Transfer-Encoding` header, then the payload will be |
| 226 | transfer-encoded, if needed, using the specified |
| 227 | :class:`~email.charset.Charset`, and a header with the appropriate value |
| 228 | will be added. If a :mailheader:`Content-Transfer-Encoding` header |
| 229 | already exists, the payload is assumed to already be correctly encoded |
| 230 | using that :mailheader:`Content-Transfer-Encoding` and is not modified. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 232 | .. method:: get_charset() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 234 | Return the :class:`~email.charset.Charset` instance associated with the |
| 235 | message's payload. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 237 | The following methods implement a mapping-like interface for accessing the |
| 238 | message's :rfc:`2822` headers. Note that there are some semantic differences |
| 239 | between these methods and a normal mapping (i.e. dictionary) interface. For |
| 240 | example, in a dictionary there are no duplicate keys, but here there may be |
| 241 | duplicate message headers. Also, in dictionaries there is no guaranteed |
| 242 | order to the keys returned by :meth:`keys`, but in a :class:`Message` object, |
| 243 | headers are always returned in the order they appeared in the original |
| 244 | message, or were added to the message later. Any header deleted and then |
| 245 | re-added are always appended to the end of the header list. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 246 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 247 | These semantic differences are intentional and are biased toward maximal |
| 248 | convenience. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 249 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 250 | Note that in all cases, any envelope header present in the message is not |
| 251 | included in the mapping interface. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 252 | |
R. David Murray | 9253214 | 2011-01-07 23:25:30 +0000 | [diff] [blame] | 253 | In a model generated from bytes, any header values that (in contravention of |
| 254 | the RFCs) contain non-ASCII bytes will, when retrieved through this |
| 255 | interface, be represented as :class:`~email.header.Header` objects with |
| 256 | a charset of `unknown-8bit`. |
R. David Murray | 96fd54e | 2010-10-08 15:55:28 +0000 | [diff] [blame] | 257 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 258 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 259 | .. method:: __len__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 261 | Return the total number of headers, including duplicates. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 262 | |
| 263 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 264 | .. method:: __contains__(name) |
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 | Return true if the message object has a field named *name*. Matching is |
| 267 | done case-insensitively and *name* should not include the trailing colon. |
| 268 | Used for the ``in`` operator, e.g.:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 269 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 270 | if 'message-id' in myMessage: |
| 271 | print('Message-ID:', myMessage['message-id']) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 272 | |
| 273 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 274 | .. method:: __getitem__(name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 275 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 276 | Return the value of the named header field. *name* should not include the |
| 277 | colon field separator. If the header is missing, ``None`` is returned; a |
| 278 | :exc:`KeyError` is never raised. |
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 | Note that if the named field appears more than once in the message's |
| 281 | headers, exactly which of those field values will be returned is |
| 282 | undefined. Use the :meth:`get_all` method to get the values of all the |
| 283 | extant named headers. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | |
| 285 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 286 | .. method:: __setitem__(name, val) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 287 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 288 | Add a header to the message with field name *name* and value *val*. The |
| 289 | field is appended to the end of the message's existing fields. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 290 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 291 | Note that this does *not* overwrite or delete any existing header with the same |
| 292 | name. If you want to ensure that the new header is the only one present in the |
| 293 | message with field name *name*, delete the field first, e.g.:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 294 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 295 | del msg['subject'] |
| 296 | msg['subject'] = 'Python roolz!' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 297 | |
| 298 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 299 | .. method:: __delitem__(name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 300 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 301 | 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] | 302 | headers. No exception is raised if the named field isn't present in the |
| 303 | headers. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 304 | |
| 305 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 306 | .. method:: keys() |
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 a list of all the message's header field names. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 309 | |
| 310 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 311 | .. method:: values() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 312 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 313 | Return a list of all the message's field values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 314 | |
| 315 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 316 | .. method:: items() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 317 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 318 | Return a list of 2-tuples containing all the message's field headers and |
| 319 | values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 320 | |
| 321 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 322 | .. method:: get(name, failobj=None) |
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 value of the named header field. This is identical to |
| 325 | :meth:`__getitem__` except that optional *failobj* is returned if the |
| 326 | named header is missing (defaults to ``None``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 327 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 328 | Here are some additional useful methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 329 | |
| 330 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 331 | .. method:: get_all(name, failobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 332 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 333 | Return a list of all the values for the field named *name*. If there are |
| 334 | no such named headers in the message, *failobj* is returned (defaults to |
| 335 | ``None``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 336 | |
| 337 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 338 | .. method:: add_header(_name, _value, **_params) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 339 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 340 | Extended header setting. This method is similar to :meth:`__setitem__` |
| 341 | except that additional header parameters can be provided as keyword |
| 342 | arguments. *_name* is the header field to add and *_value* is the |
| 343 | *primary* value for the header. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 344 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 345 | For each item in the keyword argument dictionary *_params*, the key is |
| 346 | taken as the parameter name, with underscores converted to dashes (since |
| 347 | dashes are illegal in Python identifiers). Normally, the parameter will |
| 348 | be added as ``key="value"`` unless the value is ``None``, in which case |
R. David Murray | 7ec754b | 2010-12-13 23:51:19 +0000 | [diff] [blame] | 349 | only the key will be added. If the value contains non-ASCII characters, |
| 350 | it can be specified as a three tuple in the format |
| 351 | ``(CHARSET, LANGUAGE, VALUE)``, where ``CHARSET`` is a string naming the |
| 352 | charset to be used to encode the value, ``LANGUAGE`` can usually be set |
Georg Brandl | c884350 | 2010-12-19 10:28:46 +0000 | [diff] [blame] | 353 | to ``None`` or the empty string (see :rfc:`2231` for other possibilities), |
R. David Murray | 7ec754b | 2010-12-13 23:51:19 +0000 | [diff] [blame] | 354 | and ``VALUE`` is the string value containing non-ASCII code points. If |
| 355 | a three tuple is not passed and the value contains non-ASCII characters, |
Georg Brandl | c884350 | 2010-12-19 10:28:46 +0000 | [diff] [blame] | 356 | it is automatically encoded in :rfc:`2231` format using a ``CHARSET`` |
R. David Murray | 7ec754b | 2010-12-13 23:51:19 +0000 | [diff] [blame] | 357 | of ``utf-8`` and a ``LANGUAGE`` of ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 358 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 359 | Here's an example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 360 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 361 | msg.add_header('Content-Disposition', 'attachment', filename='bud.gif') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 362 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 363 | This will add a header that looks like :: |
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 | Content-Disposition: attachment; filename="bud.gif" |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 366 | |
Ezio Melotti | e130a52 | 2011-10-19 10:58:56 +0300 | [diff] [blame] | 367 | An example with non-ASCII characters:: |
R. David Murray | 7ec754b | 2010-12-13 23:51:19 +0000 | [diff] [blame] | 368 | |
| 369 | msg.add_header('Content-Disposition', 'attachment', |
| 370 | filename=('iso-8859-1', '', 'Fußballer.ppt')) |
| 371 | |
| 372 | Which produces :: |
| 373 | |
| 374 | Content-Disposition: attachment; filename*="iso-8859-1''Fu%DFballer.ppt" |
| 375 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 376 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 377 | .. method:: replace_header(_name, _value) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 378 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 379 | Replace a header. Replace the first header found in the message that |
| 380 | matches *_name*, retaining header order and field name case. If no |
| 381 | matching header was found, a :exc:`KeyError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 382 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 383 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 384 | .. method:: get_content_type() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 385 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 386 | Return the message's content type. The returned string is coerced to |
| 387 | lower case of the form :mimetype:`maintype/subtype`. If there was no |
| 388 | :mailheader:`Content-Type` header in the message the default type as given |
| 389 | by :meth:`get_default_type` will be returned. Since according to |
| 390 | :rfc:`2045`, messages always have a default type, :meth:`get_content_type` |
| 391 | will always return a value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 392 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 393 | :rfc:`2045` defines a message's default type to be :mimetype:`text/plain` |
| 394 | unless it appears inside a :mimetype:`multipart/digest` container, in |
| 395 | which case it would be :mimetype:`message/rfc822`. If the |
| 396 | :mailheader:`Content-Type` header has an invalid type specification, |
| 397 | :rfc:`2045` mandates that the default type be :mimetype:`text/plain`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 398 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 399 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 400 | .. method:: get_content_maintype() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 401 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 402 | Return the message's main content type. This is the :mimetype:`maintype` |
| 403 | part of the string returned by :meth:`get_content_type`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 404 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 405 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 406 | .. method:: get_content_subtype() |
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 | Return the message's sub-content type. This is the :mimetype:`subtype` |
| 409 | part of the string returned by :meth:`get_content_type`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 410 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 411 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 412 | .. method:: get_default_type() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 413 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 414 | Return the default content type. Most messages have a default content |
| 415 | type of :mimetype:`text/plain`, except for messages that are subparts of |
| 416 | :mimetype:`multipart/digest` containers. Such subparts have a default |
| 417 | content type of :mimetype:`message/rfc822`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 418 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 420 | .. method:: set_default_type(ctype) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 421 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 422 | Set the default content type. *ctype* should either be |
| 423 | :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is not |
| 424 | enforced. The default content type is not stored in the |
| 425 | :mailheader:`Content-Type` header. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 426 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 428 | .. method:: get_params(failobj=None, header='content-type', unquote=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 429 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 430 | Return the message's :mailheader:`Content-Type` parameters, as a list. |
| 431 | The elements of the returned list are 2-tuples of key/value pairs, as |
| 432 | split on the ``'='`` sign. The left hand side of the ``'='`` is the key, |
| 433 | while the right hand side is the value. If there is no ``'='`` sign in |
| 434 | the parameter the value is the empty string, otherwise the value is as |
| 435 | described in :meth:`get_param` and is unquoted if optional *unquote* is |
| 436 | ``True`` (the default). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 437 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 438 | Optional *failobj* is the object to return if there is no |
| 439 | :mailheader:`Content-Type` header. Optional *header* is the header to |
| 440 | search instead of :mailheader:`Content-Type`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 441 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 442 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 443 | .. method:: get_param(param, failobj=None, header='content-type', unquote=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 444 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 445 | Return the value of the :mailheader:`Content-Type` header's parameter |
| 446 | *param* as a string. If the message has no :mailheader:`Content-Type` |
| 447 | header or if there is no such parameter, then *failobj* is returned |
| 448 | (defaults to ``None``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 449 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 450 | Optional *header* if given, specifies the message header to use instead of |
| 451 | :mailheader:`Content-Type`. |
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 | Parameter keys are always compared case insensitively. The return value |
| 454 | can either be a string, or a 3-tuple if the parameter was :rfc:`2231` |
| 455 | encoded. When it's a 3-tuple, the elements of the value are of the form |
| 456 | ``(CHARSET, LANGUAGE, VALUE)``. Note that both ``CHARSET`` and |
| 457 | ``LANGUAGE`` can be ``None``, in which case you should consider ``VALUE`` |
| 458 | to be encoded in the ``us-ascii`` charset. You can usually ignore |
| 459 | ``LANGUAGE``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 460 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 461 | If your application doesn't care whether the parameter was encoded as in |
| 462 | :rfc:`2231`, you can collapse the parameter value by calling |
Georg Brandl | 540b45c | 2009-04-27 16:45:26 +0000 | [diff] [blame] | 463 | :func:`email.utils.collapse_rfc2231_value`, passing in the return value |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 464 | from :meth:`get_param`. This will return a suitably decoded Unicode |
R. David Murray | 7ec754b | 2010-12-13 23:51:19 +0000 | [diff] [blame] | 465 | string when the value is a tuple, or the original string unquoted if it |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 466 | isn't. For example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 467 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 468 | rawparam = msg.get_param('foo') |
Georg Brandl | 540b45c | 2009-04-27 16:45:26 +0000 | [diff] [blame] | 469 | param = email.utils.collapse_rfc2231_value(rawparam) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 470 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 471 | In any case, the parameter value (either the returned string, or the |
| 472 | ``VALUE`` item in the 3-tuple) is always unquoted, unless *unquote* is set |
| 473 | to ``False``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 474 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 475 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 476 | .. method:: set_param(param, value, header='Content-Type', requote=True, \ |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 477 | charset=None, language='', replace=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 478 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 479 | Set a parameter in the :mailheader:`Content-Type` header. If the |
| 480 | parameter already exists in the header, its value will be replaced with |
| 481 | *value*. If the :mailheader:`Content-Type` header as not yet been defined |
| 482 | for this message, it will be set to :mimetype:`text/plain` and the new |
| 483 | parameter value will be appended as per :rfc:`2045`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 484 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 485 | Optional *header* specifies an alternative header to |
| 486 | :mailheader:`Content-Type`, and all parameters will be quoted as necessary |
| 487 | unless optional *requote* is ``False`` (the default is ``True``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 488 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 489 | If optional *charset* is specified, the parameter will be encoded |
| 490 | according to :rfc:`2231`. Optional *language* specifies the RFC 2231 |
| 491 | language, defaulting to the empty string. Both *charset* and *language* |
| 492 | should be strings. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 493 | |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 494 | If *replace* is ``False`` (the default) the header is moved to the |
| 495 | end of the list of headers. If *replace* is ``True``, the header |
| 496 | will be updated in place. |
| 497 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 498 | .. versionchanged:: 3.4 ``replace`` keyword was added. |
R David Murray | 3da240f | 2013-10-16 22:48:40 -0400 | [diff] [blame] | 499 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 500 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 501 | .. method:: del_param(param, header='content-type', requote=True) |
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 | Remove the given parameter completely from the :mailheader:`Content-Type` |
| 504 | header. The header will be re-written in place without the parameter or |
| 505 | its value. All values will be quoted as necessary unless *requote* is |
| 506 | ``False`` (the default is ``True``). Optional *header* specifies an |
| 507 | alternative to :mailheader:`Content-Type`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 508 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 509 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 510 | .. method:: set_type(type, header='Content-Type', requote=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 511 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 512 | Set the main type and subtype for the :mailheader:`Content-Type` |
| 513 | header. *type* must be a string in the form :mimetype:`maintype/subtype`, |
| 514 | otherwise a :exc:`ValueError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 515 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 516 | This method replaces the :mailheader:`Content-Type` header, keeping all |
| 517 | the parameters in place. If *requote* is ``False``, this leaves the |
| 518 | existing header's quoting as is, otherwise the parameters will be quoted |
| 519 | (the default). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 520 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 521 | An alternative header can be specified in the *header* argument. When the |
| 522 | :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version` |
| 523 | header is also added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 524 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 525 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 526 | .. method:: get_filename(failobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 527 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 528 | Return the value of the ``filename`` parameter of the |
| 529 | :mailheader:`Content-Disposition` header of the message. If the header |
| 530 | 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] | 531 | for the ``name`` parameter on the :mailheader:`Content-Type` header. If |
| 532 | neither is found, or the header is missing, then *failobj* is returned. |
| 533 | The returned string will always be unquoted as per |
| 534 | :func:`email.utils.unquote`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 535 | |
| 536 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 537 | .. method:: get_boundary(failobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 538 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 539 | Return the value of the ``boundary`` parameter of the |
| 540 | :mailheader:`Content-Type` header of the message, or *failobj* if either |
| 541 | the header is missing, or has no ``boundary`` parameter. The returned |
Georg Brandl | 540b45c | 2009-04-27 16:45:26 +0000 | [diff] [blame] | 542 | string will always be unquoted as per :func:`email.utils.unquote`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 543 | |
| 544 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 545 | .. method:: set_boundary(boundary) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 546 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 547 | Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to |
| 548 | *boundary*. :meth:`set_boundary` will always quote *boundary* if |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 549 | necessary. A :exc:`~email.errors.HeaderParseError` is raised if the |
| 550 | message object has no :mailheader:`Content-Type` header. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 551 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 552 | Note that using this method is subtly different than deleting the old |
| 553 | :mailheader:`Content-Type` header and adding a new one with the new |
| 554 | boundary via :meth:`add_header`, because :meth:`set_boundary` preserves |
| 555 | the order of the :mailheader:`Content-Type` header in the list of |
| 556 | headers. However, it does *not* preserve any continuation lines which may |
| 557 | have been present in the original :mailheader:`Content-Type` header. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 558 | |
| 559 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 560 | .. method:: get_content_charset(failobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 561 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 562 | Return the ``charset`` parameter of the :mailheader:`Content-Type` header, |
| 563 | coerced to lower case. If there is no :mailheader:`Content-Type` header, or if |
| 564 | that header has no ``charset`` parameter, *failobj* is returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 565 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 566 | Note that this method differs from :meth:`get_charset` which returns the |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 567 | :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] | 568 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 569 | |
Georg Brandl | 3f076d8 | 2009-05-17 11:28:33 +0000 | [diff] [blame] | 570 | .. method:: get_charsets(failobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 571 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 572 | Return a list containing the character set names in the message. If the |
| 573 | message is a :mimetype:`multipart`, then the list will contain one element |
| 574 | 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] | 575 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 576 | Each item in the list will be a string which is the value of the |
| 577 | ``charset`` parameter in the :mailheader:`Content-Type` header for the |
| 578 | represented subpart. However, if the subpart has no |
| 579 | :mailheader:`Content-Type` header, no ``charset`` parameter, or is not of |
| 580 | the :mimetype:`text` main MIME type, then that item in the returned list |
| 581 | will be *failobj*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 582 | |
| 583 | |
R David Murray | b744f3a | 2015-05-16 15:41:07 -0400 | [diff] [blame] | 584 | .. method:: get_content_disposition() |
| 585 | |
| 586 | Return the lowercased value (without parameters) of the message's |
| 587 | :mailheader:`Content-Disposition` header if it has one, or ``None``. The |
| 588 | possible values for this method are *inline*, *attachment* or ``None`` |
| 589 | if the message follows :rfc:`2183`. |
| 590 | |
| 591 | .. versionadded:: 3.5 |
| 592 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 593 | .. method:: walk() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 594 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 595 | The :meth:`walk` method is an all-purpose generator which can be used to |
| 596 | iterate over all the parts and subparts of a message object tree, in |
| 597 | depth-first traversal order. You will typically use :meth:`walk` as the |
| 598 | iterator in a ``for`` loop; each iteration returns the next subpart. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 599 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 600 | Here's an example that prints the MIME type of every part of a multipart |
R David Murray | fdfb005 | 2013-07-29 15:49:58 -0400 | [diff] [blame] | 601 | message structure: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 602 | |
R David Murray | 9cc5fd7 | 2014-09-27 15:37:40 -0400 | [diff] [blame] | 603 | .. testsetup:: |
R David Murray | fdfb005 | 2013-07-29 15:49:58 -0400 | [diff] [blame] | 604 | |
R David Murray | 9cc5fd7 | 2014-09-27 15:37:40 -0400 | [diff] [blame] | 605 | >>> from email import message_from_binary_file |
| 606 | >>> with open('Lib/test/test_email/data/msg_16.txt', 'rb') as f: |
| 607 | ... msg = message_from_binary_file(f) |
| 608 | >>> from email.iterators import _structure |
R David Murray | fdfb005 | 2013-07-29 15:49:58 -0400 | [diff] [blame] | 609 | |
R David Murray | 9cc5fd7 | 2014-09-27 15:37:40 -0400 | [diff] [blame] | 610 | .. doctest:: |
R David Murray | fdfb005 | 2013-07-29 15:49:58 -0400 | [diff] [blame] | 611 | |
R David Murray | 9cc5fd7 | 2014-09-27 15:37:40 -0400 | [diff] [blame] | 612 | >>> for part in msg.walk(): |
| 613 | ... print(part.get_content_type()) |
| 614 | multipart/report |
| 615 | text/plain |
| 616 | message/delivery-status |
| 617 | text/plain |
| 618 | text/plain |
| 619 | message/rfc822 |
| 620 | text/plain |
| 621 | |
| 622 | ``walk`` iterates over the subparts of any part where |
| 623 | :meth:`is_multipart` returns ``True``, even though |
| 624 | ``msg.get_content_maintype() == 'multipart'`` may return ``False``. We |
| 625 | can see this in our example by making use of the ``_structure`` debug |
| 626 | helper function: |
| 627 | |
| 628 | .. doctest:: |
| 629 | |
| 630 | >>> for part in msg.walk(): |
| 631 | ... print(part.get_content_maintype() == 'multipart'), |
| 632 | ... part.is_multipart()) |
| 633 | True True |
| 634 | False False |
| 635 | False True |
| 636 | False False |
| 637 | False False |
| 638 | False True |
| 639 | False False |
| 640 | >>> _structure(msg) |
| 641 | multipart/report |
| 642 | text/plain |
| 643 | message/delivery-status |
| 644 | text/plain |
| 645 | text/plain |
| 646 | message/rfc822 |
| 647 | text/plain |
| 648 | |
| 649 | Here the ``message`` parts are not ``multiparts``, but they do contain |
| 650 | subparts. ``is_multipart()`` returns ``True`` and ``walk`` descends |
| 651 | into the subparts. |
| 652 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 653 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 654 | :class:`Message` objects can also optionally contain two instance attributes, |
| 655 | 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] | 656 | |
| 657 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 658 | .. attribute:: preamble |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 659 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 660 | The format of a MIME document allows for some text between the blank line |
| 661 | following the headers, and the first multipart boundary string. Normally, |
| 662 | this text is never visible in a MIME-aware mail reader because it falls |
| 663 | outside the standard MIME armor. However, when viewing the raw text of |
| 664 | the message, or when viewing the message in a non-MIME aware reader, this |
| 665 | text can become visible. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 666 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 667 | The *preamble* attribute contains this leading extra-armor text for MIME |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 668 | documents. When the :class:`~email.parser.Parser` discovers some text |
| 669 | after the headers but before the first boundary string, it assigns this |
| 670 | text to the message's *preamble* attribute. When the |
| 671 | :class:`~email.generator.Generator` is writing out the plain text |
| 672 | representation of a MIME message, and it finds the |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 673 | message has a *preamble* attribute, it will write this text in the area |
| 674 | between the headers and the first boundary. See :mod:`email.parser` and |
| 675 | :mod:`email.generator` for details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 676 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 677 | Note that if the message object has no preamble, the *preamble* attribute |
| 678 | will be ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 679 | |
| 680 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 681 | .. attribute:: epilogue |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 682 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 683 | The *epilogue* attribute acts the same way as the *preamble* attribute, |
| 684 | except that it contains text that appears between the last boundary and |
| 685 | the end of the message. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 686 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 687 | You do not need to set the epilogue to the empty string in order for the |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 688 | :class:`~email.generator.Generator` to print a newline at the end of the |
| 689 | file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 690 | |
| 691 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 692 | .. attribute:: defects |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 693 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 694 | The *defects* attribute contains a list of all the problems found when |
| 695 | parsing this message. See :mod:`email.errors` for a detailed description |
| 696 | of the possible parsing defects. |