blob: 4a34c3683c077ebf944cd3709b2e57aa9eb2a170 [file] [log] [blame]
R David Murray79cf3ba2012-05-27 17:10:36 -04001:mod:`email.message`: Representing an email message
2---------------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: email.message
5 :synopsis: The base class representing email messages.
6
7
8The central class in the :mod:`email` package is the :class:`Message` class,
9imported 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
11setting and querying header fields, and for accessing message bodies.
12
13Conceptually, a :class:`Message` object consists of *headers* and *payloads*.
14Headers are :rfc:`2822` style field names and values where the field name and
15value are separated by a colon. The colon is not part of either the field name
16or the field value.
17
18Headers are stored and returned in case-preserving form but are matched
19case-insensitively. There may also be a single envelope header, also known as
20the *Unix-From* header or the ``From_`` header. The payload is either a string
21in the case of simple message objects or a list of :class:`Message` objects for
22MIME container documents (e.g. :mimetype:`multipart/\*` and
23:mimetype:`message/rfc822`).
24
25:class:`Message` objects provide a mapping style interface for accessing the
26message headers, and an explicit interface for accessing both the headers and
27the payload. It provides convenience methods for generating a flat text
28representation of the message object tree, for accessing commonly used header
29parameters, and for recursively walking over the object tree.
30
31Here are the methods of the :class:`Message` class:
32
33
R David Murraybb17d2b2013-08-09 16:15:28 -040034.. class:: Message(policy=compat32)
Georg Brandl116aa622007-08-15 14:28:22 +000035
R David Murraybb17d2b2013-08-09 16:15:28 -040036 The *policy* argument determiens the :mod:`~email.policy` that will be used
37 to update the message model. The default value, :class:`compat32
38 <email.policy.Compat32>` maintains backward compatibility with the
39 Python 3.2 version of the email package. For more information see the
40 :mod:`~email.policy` documentation.
41
42 .. versionchanged:: 3.3 The *policy* keyword argument was added.
Georg Brandl116aa622007-08-15 14:28:22 +000043
44
R David Murraybb17d2b2013-08-09 16:15:28 -040045 .. method:: as_string(unixfrom=False, maxheaderlen=0, policy=None)
Georg Brandl116aa622007-08-15 14:28:22 +000046
Benjamin Petersone41251e2008-04-25 01:59:09 +000047 Return the entire message flattened as a string. When optional *unixfrom*
R David Murraybb17d2b2013-08-09 16:15:28 -040048 is true, the envelope header is included in the returned string.
49 *unixfrom* defaults to ``False``. For backward compabitility reasons,
50 *maxheaderlen* defaults to ``0``, so if you want a different value you
51 must override it explicitly (the value specified for *max_line_length* in
52 the policy will be ignored by this method). The *policy* argument may be
53 used to override the default policy obtained from the message instance.
54 This can be used to control some of the formatting produced by the
55 method, since the specified *policy* will be passed to the ``Generator``.
56
57 Flattening the message may trigger changes to the :class:`Message` if
58 defaults need to be filled in to complete the transformation to a string
59 (for example, MIME boundaries may be generated or modified).
Georg Brandl116aa622007-08-15 14:28:22 +000060
Benjamin Petersone41251e2008-04-25 01:59:09 +000061 Note that this method is provided as a convenience and may not always
R David Murray7dedcb42011-03-15 14:01:18 -040062 format the message the way you want. For example, by default it does
63 not do the mangling of lines that begin with ``From`` that is
64 required by the unix mbox format. For more flexibility, instantiate a
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030065 :class:`~email.generator.Generator` instance and use its
66 :meth:`~email.generator.Generator.flatten` method directly. For example::
Georg Brandl116aa622007-08-15 14:28:22 +000067
Georg Brandl03124942008-06-10 15:50:56 +000068 from io import StringIO
Benjamin Petersone41251e2008-04-25 01:59:09 +000069 from email.generator import Generator
70 fp = StringIO()
R David Murray7dedcb42011-03-15 14:01:18 -040071 g = Generator(fp, mangle_from_=True, maxheaderlen=60)
Benjamin Petersone41251e2008-04-25 01:59:09 +000072 g.flatten(msg)
73 text = fp.getvalue()
Georg Brandl116aa622007-08-15 14:28:22 +000074
R David Murraybb17d2b2013-08-09 16:15:28 -040075 If the message object contains binary data that is not encoded according
76 to RFC standards, the non-compliant data will be replaced by unicode
77 "unknown character" code points. (See also :meth:`.as_bytes` and
78 :class:`~email.generator.BytesGenerator`.)
79
80 .. versionchanged:: 3.4 the *policy* keyword argument was added.
81
Georg Brandl116aa622007-08-15 14:28:22 +000082
Benjamin Petersone41251e2008-04-25 01:59:09 +000083 .. method:: __str__()
Georg Brandl116aa622007-08-15 14:28:22 +000084
R David Murraybb17d2b2013-08-09 16:15:28 -040085 Equivalent to :meth:`.as_string()`. Allows ``str(msg)`` to produce a
86 string containing the formatted message.
87
88
89 .. method:: as_bytes(unixfrom=False, policy=None)
90
91 Return the entire message flattened as a bytes object. When optional
92 *unixfrom* is true, the envelope header is included in the returned
93 string. *unixfrom* defaults to ``False``. The *policy* argument may be
94 used to override the default policy obtained from the message instance.
95 This can be used to control some of the formatting produced by the
96 method, since the specified *policy* will be passed to the
97 ``BytesGenerator``.
98
99 Flattening the message may trigger changes to the :class:`Message` if
100 defaults need to be filled in to complete the transformation to a string
101 (for example, MIME boundaries may be generated or modified).
102
103 Note that this method is provided as a convenience and may not always
104 format the message the way you want. For example, by default it does
105 not do the mangling of lines that begin with ``From`` that is
106 required by the unix mbox format. For more flexibility, instantiate a
107 :class:`~email.generator.BytesGenerator` instance and use its
Serhiy Storchaka319f3a12013-08-19 10:03:11 +0300108 :meth:`~email.generator.BytesGenerator.flatten` method directly.
109 For example::
R David Murraybb17d2b2013-08-09 16:15:28 -0400110
111 from io import BytesIO
112 from email.generator import BytesGenerator
113 fp = BytesIO()
114 g = BytesGenerator(fp, mangle_from_=True, maxheaderlen=60)
115 g.flatten(msg)
116 text = fp.getvalue()
117
118 .. versionadded:: 3.4
119
120
121 .. method:: __bytes__()
122
123 Equivalent to :meth:`.as_bytes()`. Allows ``bytes(msg)`` to produce a
124 bytes object containing the formatted message.
125
126 .. versionadded:: 3.4
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128
Benjamin Petersone41251e2008-04-25 01:59:09 +0000129 .. method:: is_multipart()
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Benjamin Petersone41251e2008-04-25 01:59:09 +0000131 Return ``True`` if the message's payload is a list of sub-\
132 :class:`Message` objects, otherwise return ``False``. When
133 :meth:`is_multipart` returns False, the payload should be a string object.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
Benjamin Petersone41251e2008-04-25 01:59:09 +0000136 .. method:: set_unixfrom(unixfrom)
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Benjamin Petersone41251e2008-04-25 01:59:09 +0000138 Set the message's envelope header to *unixfrom*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140
Benjamin Petersone41251e2008-04-25 01:59:09 +0000141 .. method:: get_unixfrom()
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Benjamin Petersone41251e2008-04-25 01:59:09 +0000143 Return the message's envelope header. Defaults to ``None`` if the
144 envelope header was never set.
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146
Benjamin Petersone41251e2008-04-25 01:59:09 +0000147 .. method:: attach(payload)
Georg Brandl116aa622007-08-15 14:28:22 +0000148
Benjamin Petersone41251e2008-04-25 01:59:09 +0000149 Add the given *payload* to the current payload, which must be ``None`` or
150 a list of :class:`Message` objects before the call. After the call, the
151 payload will always be a list of :class:`Message` objects. If you want to
152 set the payload to a scalar object (e.g. a string), use
153 :meth:`set_payload` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000154
155
Georg Brandl3f076d82009-05-17 11:28:33 +0000156 .. method:: get_payload(i=None, decode=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000157
Benjamin Petersond6313712008-07-31 16:23:04 +0000158 Return the current payload, which will be a list of
Benjamin Petersone41251e2008-04-25 01:59:09 +0000159 :class:`Message` objects when :meth:`is_multipart` is ``True``, or a
160 string when :meth:`is_multipart` is ``False``. If the payload is a list
161 and you mutate the list object, you modify the message's payload in place.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Benjamin Petersone41251e2008-04-25 01:59:09 +0000163 With optional argument *i*, :meth:`get_payload` will return the *i*-th
164 element of the payload, counting from zero, if :meth:`is_multipart` is
165 ``True``. An :exc:`IndexError` will be raised if *i* is less than 0 or
166 greater than or equal to the number of items in the payload. If the
167 payload is a string (i.e. :meth:`is_multipart` is ``False``) and *i* is
168 given, a :exc:`TypeError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000169
Benjamin Petersone41251e2008-04-25 01:59:09 +0000170 Optional *decode* is a flag indicating whether the payload should be
171 decoded or not, according to the :mailheader:`Content-Transfer-Encoding`
172 header. When ``True`` and the message is not a multipart, the payload will
173 be decoded if this header's value is ``quoted-printable`` or ``base64``.
174 If some other encoding is used, or :mailheader:`Content-Transfer-Encoding`
R David Murray80e0aee2012-05-27 21:23:34 -0400175 header is missing, the payload is
R. David Murray96fd54e2010-10-08 15:55:28 +0000176 returned as-is (undecoded). In all cases the returned value is binary
177 data. If the message is a multipart and the *decode* flag is ``True``,
R David Murray80e0aee2012-05-27 21:23:34 -0400178 then ``None`` is returned. If the payload is base64 and it was not
179 perfectly formed (missing padding, characters outside the base64
180 alphabet), then an appropriate defect will be added to the message's
181 defect property (:class:`~email.errors.InvalidBase64PaddingDefect` or
182 :class:`~email.errors.InvalidBase64CharactersDefect`, respectively).
R. David Murray96fd54e2010-10-08 15:55:28 +0000183
184 When *decode* is ``False`` (the default) the body is returned as a string
185 without decoding the :mailheader:`Content-Transfer-Encoding`. However,
186 for a :mailheader:`Content-Transfer-Encoding` of 8bit, an attempt is made
Senthil Kumaran82270452010-10-15 13:29:33 +0000187 to decode the original bytes using the ``charset`` specified by the
188 :mailheader:`Content-Type` header, using the ``replace`` error handler.
189 If no ``charset`` is specified, or if the ``charset`` given is not
190 recognized by the email package, the body is decoded using the default
191 ASCII charset.
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193
Georg Brandl3f076d82009-05-17 11:28:33 +0000194 .. method:: set_payload(payload, charset=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000195
Benjamin Petersone41251e2008-04-25 01:59:09 +0000196 Set the entire message object's payload to *payload*. It is the client's
197 responsibility to ensure the payload invariants. Optional *charset* sets
198 the message's default character set; see :meth:`set_charset` for details.
Georg Brandl116aa622007-08-15 14:28:22 +0000199
Benjamin Petersone41251e2008-04-25 01:59:09 +0000200 .. method:: set_charset(charset)
Georg Brandl116aa622007-08-15 14:28:22 +0000201
Benjamin Petersone41251e2008-04-25 01:59:09 +0000202 Set the character set of the payload to *charset*, which can either be a
Georg Brandl3638e482009-04-27 16:46:17 +0000203 :class:`~email.charset.Charset` instance (see :mod:`email.charset`), a
204 string naming a character set, or ``None``. If it is a string, it will
205 be converted to a :class:`~email.charset.Charset` instance. If *charset*
206 is ``None``, the ``charset`` parameter will be removed from the
R David Murraye3d09ff2011-03-15 17:41:13 -0400207 :mailheader:`Content-Type` header (the message will not be otherwise
208 modified). Anything else will generate a :exc:`TypeError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000209
R David Murraye3d09ff2011-03-15 17:41:13 -0400210 If there is no existing :mailheader:`MIME-Version` header one will be
211 added. If there is no existing :mailheader:`Content-Type` header, one
212 will be added with a value of :mimetype:`text/plain`. Whether the
213 :mailheader:`Content-Type` header already exists or not, its ``charset``
214 parameter will be set to *charset.output_charset*. If
215 *charset.input_charset* and *charset.output_charset* differ, the payload
216 will be re-encoded to the *output_charset*. If there is no existing
217 :mailheader:`Content-Transfer-Encoding` header, then the payload will be
218 transfer-encoded, if needed, using the specified
219 :class:`~email.charset.Charset`, and a header with the appropriate value
220 will be added. If a :mailheader:`Content-Transfer-Encoding` header
221 already exists, the payload is assumed to already be correctly encoded
222 using that :mailheader:`Content-Transfer-Encoding` and is not modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Benjamin Petersone41251e2008-04-25 01:59:09 +0000224 .. method:: get_charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000225
Georg Brandl3638e482009-04-27 16:46:17 +0000226 Return the :class:`~email.charset.Charset` instance associated with the
227 message's payload.
Georg Brandl116aa622007-08-15 14:28:22 +0000228
Benjamin Petersone41251e2008-04-25 01:59:09 +0000229 The following methods implement a mapping-like interface for accessing the
230 message's :rfc:`2822` headers. Note that there are some semantic differences
231 between these methods and a normal mapping (i.e. dictionary) interface. For
232 example, in a dictionary there are no duplicate keys, but here there may be
233 duplicate message headers. Also, in dictionaries there is no guaranteed
234 order to the keys returned by :meth:`keys`, but in a :class:`Message` object,
235 headers are always returned in the order they appeared in the original
236 message, or were added to the message later. Any header deleted and then
237 re-added are always appended to the end of the header list.
Georg Brandl116aa622007-08-15 14:28:22 +0000238
Benjamin Petersone41251e2008-04-25 01:59:09 +0000239 These semantic differences are intentional and are biased toward maximal
240 convenience.
Georg Brandl116aa622007-08-15 14:28:22 +0000241
Benjamin Petersone41251e2008-04-25 01:59:09 +0000242 Note that in all cases, any envelope header present in the message is not
243 included in the mapping interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000244
R. David Murray92532142011-01-07 23:25:30 +0000245 In a model generated from bytes, any header values that (in contravention of
246 the RFCs) contain non-ASCII bytes will, when retrieved through this
247 interface, be represented as :class:`~email.header.Header` objects with
248 a charset of `unknown-8bit`.
R. David Murray96fd54e2010-10-08 15:55:28 +0000249
Georg Brandl116aa622007-08-15 14:28:22 +0000250
Benjamin Petersone41251e2008-04-25 01:59:09 +0000251 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000252
Benjamin Petersone41251e2008-04-25 01:59:09 +0000253 Return the total number of headers, including duplicates.
Georg Brandl116aa622007-08-15 14:28:22 +0000254
255
Benjamin Petersone41251e2008-04-25 01:59:09 +0000256 .. method:: __contains__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000257
Benjamin Petersone41251e2008-04-25 01:59:09 +0000258 Return true if the message object has a field named *name*. Matching is
259 done case-insensitively and *name* should not include the trailing colon.
260 Used for the ``in`` operator, e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000261
Benjamin Petersone41251e2008-04-25 01:59:09 +0000262 if 'message-id' in myMessage:
263 print('Message-ID:', myMessage['message-id'])
Georg Brandl116aa622007-08-15 14:28:22 +0000264
265
Benjamin Petersone41251e2008-04-25 01:59:09 +0000266 .. method:: __getitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000267
Benjamin Petersone41251e2008-04-25 01:59:09 +0000268 Return the value of the named header field. *name* should not include the
269 colon field separator. If the header is missing, ``None`` is returned; a
270 :exc:`KeyError` is never raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000271
Benjamin Petersone41251e2008-04-25 01:59:09 +0000272 Note that if the named field appears more than once in the message's
273 headers, exactly which of those field values will be returned is
274 undefined. Use the :meth:`get_all` method to get the values of all the
275 extant named headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000276
277
Benjamin Petersone41251e2008-04-25 01:59:09 +0000278 .. method:: __setitem__(name, val)
Georg Brandl116aa622007-08-15 14:28:22 +0000279
Benjamin Petersone41251e2008-04-25 01:59:09 +0000280 Add a header to the message with field name *name* and value *val*. The
281 field is appended to the end of the message's existing fields.
Georg Brandl116aa622007-08-15 14:28:22 +0000282
Benjamin Petersone41251e2008-04-25 01:59:09 +0000283 Note that this does *not* overwrite or delete any existing header with the same
284 name. If you want to ensure that the new header is the only one present in the
285 message with field name *name*, delete the field first, e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000286
Benjamin Petersone41251e2008-04-25 01:59:09 +0000287 del msg['subject']
288 msg['subject'] = 'Python roolz!'
Georg Brandl116aa622007-08-15 14:28:22 +0000289
290
Benjamin Petersone41251e2008-04-25 01:59:09 +0000291 .. method:: __delitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000292
Benjamin Petersone41251e2008-04-25 01:59:09 +0000293 Delete all occurrences of the field with name *name* from the message's
Georg Brandl3f076d82009-05-17 11:28:33 +0000294 headers. No exception is raised if the named field isn't present in the
295 headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000296
297
Benjamin Petersone41251e2008-04-25 01:59:09 +0000298 .. method:: keys()
Georg Brandl116aa622007-08-15 14:28:22 +0000299
Benjamin Petersone41251e2008-04-25 01:59:09 +0000300 Return a list of all the message's header field names.
Georg Brandl116aa622007-08-15 14:28:22 +0000301
302
Benjamin Petersone41251e2008-04-25 01:59:09 +0000303 .. method:: values()
Georg Brandl116aa622007-08-15 14:28:22 +0000304
Benjamin Petersone41251e2008-04-25 01:59:09 +0000305 Return a list of all the message's field values.
Georg Brandl116aa622007-08-15 14:28:22 +0000306
307
Benjamin Petersone41251e2008-04-25 01:59:09 +0000308 .. method:: items()
Georg Brandl116aa622007-08-15 14:28:22 +0000309
Benjamin Petersone41251e2008-04-25 01:59:09 +0000310 Return a list of 2-tuples containing all the message's field headers and
311 values.
Georg Brandl116aa622007-08-15 14:28:22 +0000312
313
Georg Brandl3f076d82009-05-17 11:28:33 +0000314 .. method:: get(name, failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000315
Benjamin Petersone41251e2008-04-25 01:59:09 +0000316 Return the value of the named header field. This is identical to
317 :meth:`__getitem__` except that optional *failobj* is returned if the
318 named header is missing (defaults to ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000319
Benjamin Petersone41251e2008-04-25 01:59:09 +0000320 Here are some additional useful methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000321
322
Georg Brandl3f076d82009-05-17 11:28:33 +0000323 .. method:: get_all(name, failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000324
Benjamin Petersone41251e2008-04-25 01:59:09 +0000325 Return a list of all the values for the field named *name*. If there are
326 no such named headers in the message, *failobj* is returned (defaults to
327 ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000328
329
Benjamin Petersone41251e2008-04-25 01:59:09 +0000330 .. method:: add_header(_name, _value, **_params)
Georg Brandl116aa622007-08-15 14:28:22 +0000331
Benjamin Petersone41251e2008-04-25 01:59:09 +0000332 Extended header setting. This method is similar to :meth:`__setitem__`
333 except that additional header parameters can be provided as keyword
334 arguments. *_name* is the header field to add and *_value* is the
335 *primary* value for the header.
Georg Brandl116aa622007-08-15 14:28:22 +0000336
Benjamin Petersone41251e2008-04-25 01:59:09 +0000337 For each item in the keyword argument dictionary *_params*, the key is
338 taken as the parameter name, with underscores converted to dashes (since
339 dashes are illegal in Python identifiers). Normally, the parameter will
340 be added as ``key="value"`` unless the value is ``None``, in which case
R. David Murray7ec754b2010-12-13 23:51:19 +0000341 only the key will be added. If the value contains non-ASCII characters,
342 it can be specified as a three tuple in the format
343 ``(CHARSET, LANGUAGE, VALUE)``, where ``CHARSET`` is a string naming the
344 charset to be used to encode the value, ``LANGUAGE`` can usually be set
Georg Brandlc8843502010-12-19 10:28:46 +0000345 to ``None`` or the empty string (see :rfc:`2231` for other possibilities),
R. David Murray7ec754b2010-12-13 23:51:19 +0000346 and ``VALUE`` is the string value containing non-ASCII code points. If
347 a three tuple is not passed and the value contains non-ASCII characters,
Georg Brandlc8843502010-12-19 10:28:46 +0000348 it is automatically encoded in :rfc:`2231` format using a ``CHARSET``
R. David Murray7ec754b2010-12-13 23:51:19 +0000349 of ``utf-8`` and a ``LANGUAGE`` of ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000350
Benjamin Petersone41251e2008-04-25 01:59:09 +0000351 Here's an example::
Georg Brandl116aa622007-08-15 14:28:22 +0000352
Benjamin Petersone41251e2008-04-25 01:59:09 +0000353 msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
Georg Brandl116aa622007-08-15 14:28:22 +0000354
Benjamin Petersone41251e2008-04-25 01:59:09 +0000355 This will add a header that looks like ::
Georg Brandl116aa622007-08-15 14:28:22 +0000356
Benjamin Petersone41251e2008-04-25 01:59:09 +0000357 Content-Disposition: attachment; filename="bud.gif"
Georg Brandl116aa622007-08-15 14:28:22 +0000358
Ezio Melottie130a522011-10-19 10:58:56 +0300359 An example with non-ASCII characters::
R. David Murray7ec754b2010-12-13 23:51:19 +0000360
361 msg.add_header('Content-Disposition', 'attachment',
362 filename=('iso-8859-1', '', 'Fußballer.ppt'))
363
364 Which produces ::
365
366 Content-Disposition: attachment; filename*="iso-8859-1''Fu%DFballer.ppt"
367
Georg Brandl116aa622007-08-15 14:28:22 +0000368
Benjamin Petersone41251e2008-04-25 01:59:09 +0000369 .. method:: replace_header(_name, _value)
Georg Brandl116aa622007-08-15 14:28:22 +0000370
Benjamin Petersone41251e2008-04-25 01:59:09 +0000371 Replace a header. Replace the first header found in the message that
372 matches *_name*, retaining header order and field name case. If no
373 matching header was found, a :exc:`KeyError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000374
Georg Brandl116aa622007-08-15 14:28:22 +0000375
Benjamin Petersone41251e2008-04-25 01:59:09 +0000376 .. method:: get_content_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000377
Benjamin Petersone41251e2008-04-25 01:59:09 +0000378 Return the message's content type. The returned string is coerced to
379 lower case of the form :mimetype:`maintype/subtype`. If there was no
380 :mailheader:`Content-Type` header in the message the default type as given
381 by :meth:`get_default_type` will be returned. Since according to
382 :rfc:`2045`, messages always have a default type, :meth:`get_content_type`
383 will always return a value.
Georg Brandl116aa622007-08-15 14:28:22 +0000384
Benjamin Petersone41251e2008-04-25 01:59:09 +0000385 :rfc:`2045` defines a message's default type to be :mimetype:`text/plain`
386 unless it appears inside a :mimetype:`multipart/digest` container, in
387 which case it would be :mimetype:`message/rfc822`. If the
388 :mailheader:`Content-Type` header has an invalid type specification,
389 :rfc:`2045` mandates that the default type be :mimetype:`text/plain`.
Georg Brandl116aa622007-08-15 14:28:22 +0000390
Georg Brandl116aa622007-08-15 14:28:22 +0000391
Benjamin Petersone41251e2008-04-25 01:59:09 +0000392 .. method:: get_content_maintype()
Georg Brandl116aa622007-08-15 14:28:22 +0000393
Benjamin Petersone41251e2008-04-25 01:59:09 +0000394 Return the message's main content type. This is the :mimetype:`maintype`
395 part of the string returned by :meth:`get_content_type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000396
Georg Brandl116aa622007-08-15 14:28:22 +0000397
Benjamin Petersone41251e2008-04-25 01:59:09 +0000398 .. method:: get_content_subtype()
Georg Brandl116aa622007-08-15 14:28:22 +0000399
Benjamin Petersone41251e2008-04-25 01:59:09 +0000400 Return the message's sub-content type. This is the :mimetype:`subtype`
401 part of the string returned by :meth:`get_content_type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000402
Georg Brandl116aa622007-08-15 14:28:22 +0000403
Benjamin Petersone41251e2008-04-25 01:59:09 +0000404 .. method:: get_default_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000405
Benjamin Petersone41251e2008-04-25 01:59:09 +0000406 Return the default content type. Most messages have a default content
407 type of :mimetype:`text/plain`, except for messages that are subparts of
408 :mimetype:`multipart/digest` containers. Such subparts have a default
409 content type of :mimetype:`message/rfc822`.
Georg Brandl116aa622007-08-15 14:28:22 +0000410
Georg Brandl116aa622007-08-15 14:28:22 +0000411
Benjamin Petersone41251e2008-04-25 01:59:09 +0000412 .. method:: set_default_type(ctype)
Georg Brandl116aa622007-08-15 14:28:22 +0000413
Benjamin Petersone41251e2008-04-25 01:59:09 +0000414 Set the default content type. *ctype* should either be
415 :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is not
416 enforced. The default content type is not stored in the
417 :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000418
Georg Brandl116aa622007-08-15 14:28:22 +0000419
Georg Brandl3f076d82009-05-17 11:28:33 +0000420 .. method:: get_params(failobj=None, header='content-type', unquote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000421
Benjamin Petersone41251e2008-04-25 01:59:09 +0000422 Return the message's :mailheader:`Content-Type` parameters, as a list.
423 The elements of the returned list are 2-tuples of key/value pairs, as
424 split on the ``'='`` sign. The left hand side of the ``'='`` is the key,
425 while the right hand side is the value. If there is no ``'='`` sign in
426 the parameter the value is the empty string, otherwise the value is as
427 described in :meth:`get_param` and is unquoted if optional *unquote* is
428 ``True`` (the default).
Georg Brandl116aa622007-08-15 14:28:22 +0000429
Benjamin Petersone41251e2008-04-25 01:59:09 +0000430 Optional *failobj* is the object to return if there is no
431 :mailheader:`Content-Type` header. Optional *header* is the header to
432 search instead of :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000433
Georg Brandl116aa622007-08-15 14:28:22 +0000434
Georg Brandl3f076d82009-05-17 11:28:33 +0000435 .. method:: get_param(param, failobj=None, header='content-type', unquote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000436
Benjamin Petersone41251e2008-04-25 01:59:09 +0000437 Return the value of the :mailheader:`Content-Type` header's parameter
438 *param* as a string. If the message has no :mailheader:`Content-Type`
439 header or if there is no such parameter, then *failobj* is returned
440 (defaults to ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000441
Benjamin Petersone41251e2008-04-25 01:59:09 +0000442 Optional *header* if given, specifies the message header to use instead of
443 :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000444
Benjamin Petersone41251e2008-04-25 01:59:09 +0000445 Parameter keys are always compared case insensitively. The return value
446 can either be a string, or a 3-tuple if the parameter was :rfc:`2231`
447 encoded. When it's a 3-tuple, the elements of the value are of the form
448 ``(CHARSET, LANGUAGE, VALUE)``. Note that both ``CHARSET`` and
449 ``LANGUAGE`` can be ``None``, in which case you should consider ``VALUE``
450 to be encoded in the ``us-ascii`` charset. You can usually ignore
451 ``LANGUAGE``.
Georg Brandl116aa622007-08-15 14:28:22 +0000452
Benjamin Petersone41251e2008-04-25 01:59:09 +0000453 If your application doesn't care whether the parameter was encoded as in
454 :rfc:`2231`, you can collapse the parameter value by calling
Georg Brandl540b45c2009-04-27 16:45:26 +0000455 :func:`email.utils.collapse_rfc2231_value`, passing in the return value
Benjamin Petersone41251e2008-04-25 01:59:09 +0000456 from :meth:`get_param`. This will return a suitably decoded Unicode
R. David Murray7ec754b2010-12-13 23:51:19 +0000457 string when the value is a tuple, or the original string unquoted if it
Benjamin Petersone41251e2008-04-25 01:59:09 +0000458 isn't. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000459
Benjamin Petersone41251e2008-04-25 01:59:09 +0000460 rawparam = msg.get_param('foo')
Georg Brandl540b45c2009-04-27 16:45:26 +0000461 param = email.utils.collapse_rfc2231_value(rawparam)
Georg Brandl116aa622007-08-15 14:28:22 +0000462
Benjamin Petersone41251e2008-04-25 01:59:09 +0000463 In any case, the parameter value (either the returned string, or the
464 ``VALUE`` item in the 3-tuple) is always unquoted, unless *unquote* is set
465 to ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000466
Georg Brandl116aa622007-08-15 14:28:22 +0000467
Georg Brandl3f076d82009-05-17 11:28:33 +0000468 .. method:: set_param(param, value, header='Content-Type', requote=True, charset=None, language='')
Georg Brandl116aa622007-08-15 14:28:22 +0000469
Benjamin Petersone41251e2008-04-25 01:59:09 +0000470 Set a parameter in the :mailheader:`Content-Type` header. If the
471 parameter already exists in the header, its value will be replaced with
472 *value*. If the :mailheader:`Content-Type` header as not yet been defined
473 for this message, it will be set to :mimetype:`text/plain` and the new
474 parameter value will be appended as per :rfc:`2045`.
Georg Brandl116aa622007-08-15 14:28:22 +0000475
Benjamin Petersone41251e2008-04-25 01:59:09 +0000476 Optional *header* specifies an alternative header to
477 :mailheader:`Content-Type`, and all parameters will be quoted as necessary
478 unless optional *requote* is ``False`` (the default is ``True``).
Georg Brandl116aa622007-08-15 14:28:22 +0000479
Benjamin Petersone41251e2008-04-25 01:59:09 +0000480 If optional *charset* is specified, the parameter will be encoded
481 according to :rfc:`2231`. Optional *language* specifies the RFC 2231
482 language, defaulting to the empty string. Both *charset* and *language*
483 should be strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000484
Georg Brandl116aa622007-08-15 14:28:22 +0000485
Georg Brandl3f076d82009-05-17 11:28:33 +0000486 .. method:: del_param(param, header='content-type', requote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000487
Benjamin Petersone41251e2008-04-25 01:59:09 +0000488 Remove the given parameter completely from the :mailheader:`Content-Type`
489 header. The header will be re-written in place without the parameter or
490 its value. All values will be quoted as necessary unless *requote* is
491 ``False`` (the default is ``True``). Optional *header* specifies an
492 alternative to :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000493
Georg Brandl116aa622007-08-15 14:28:22 +0000494
Georg Brandl3f076d82009-05-17 11:28:33 +0000495 .. method:: set_type(type, header='Content-Type', requote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000496
Benjamin Petersone41251e2008-04-25 01:59:09 +0000497 Set the main type and subtype for the :mailheader:`Content-Type`
498 header. *type* must be a string in the form :mimetype:`maintype/subtype`,
499 otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000500
Benjamin Petersone41251e2008-04-25 01:59:09 +0000501 This method replaces the :mailheader:`Content-Type` header, keeping all
502 the parameters in place. If *requote* is ``False``, this leaves the
503 existing header's quoting as is, otherwise the parameters will be quoted
504 (the default).
Georg Brandl116aa622007-08-15 14:28:22 +0000505
Benjamin Petersone41251e2008-04-25 01:59:09 +0000506 An alternative header can be specified in the *header* argument. When the
507 :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version`
508 header is also added.
Georg Brandl116aa622007-08-15 14:28:22 +0000509
Georg Brandl116aa622007-08-15 14:28:22 +0000510
Georg Brandl3f076d82009-05-17 11:28:33 +0000511 .. method:: get_filename(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000512
Benjamin Petersone41251e2008-04-25 01:59:09 +0000513 Return the value of the ``filename`` parameter of the
514 :mailheader:`Content-Disposition` header of the message. If the header
515 does not have a ``filename`` parameter, this method falls back to looking
R. David Murray9ed34be2010-03-04 17:38:18 +0000516 for the ``name`` parameter on the :mailheader:`Content-Type` header. If
517 neither is found, or the header is missing, then *failobj* is returned.
518 The returned string will always be unquoted as per
519 :func:`email.utils.unquote`.
Georg Brandl116aa622007-08-15 14:28:22 +0000520
521
Georg Brandl3f076d82009-05-17 11:28:33 +0000522 .. method:: get_boundary(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000523
Benjamin Petersone41251e2008-04-25 01:59:09 +0000524 Return the value of the ``boundary`` parameter of the
525 :mailheader:`Content-Type` header of the message, or *failobj* if either
526 the header is missing, or has no ``boundary`` parameter. The returned
Georg Brandl540b45c2009-04-27 16:45:26 +0000527 string will always be unquoted as per :func:`email.utils.unquote`.
Georg Brandl116aa622007-08-15 14:28:22 +0000528
529
Benjamin Petersone41251e2008-04-25 01:59:09 +0000530 .. method:: set_boundary(boundary)
Georg Brandl116aa622007-08-15 14:28:22 +0000531
Benjamin Petersone41251e2008-04-25 01:59:09 +0000532 Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
533 *boundary*. :meth:`set_boundary` will always quote *boundary* if
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300534 necessary. A :exc:`~email.errors.HeaderParseError` is raised if the
535 message object has no :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000536
Benjamin Petersone41251e2008-04-25 01:59:09 +0000537 Note that using this method is subtly different than deleting the old
538 :mailheader:`Content-Type` header and adding a new one with the new
539 boundary via :meth:`add_header`, because :meth:`set_boundary` preserves
540 the order of the :mailheader:`Content-Type` header in the list of
541 headers. However, it does *not* preserve any continuation lines which may
542 have been present in the original :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000543
544
Georg Brandl3f076d82009-05-17 11:28:33 +0000545 .. method:: get_content_charset(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000546
Benjamin Petersone41251e2008-04-25 01:59:09 +0000547 Return the ``charset`` parameter of the :mailheader:`Content-Type` header,
548 coerced to lower case. If there is no :mailheader:`Content-Type` header, or if
549 that header has no ``charset`` parameter, *failobj* is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000550
Benjamin Petersone41251e2008-04-25 01:59:09 +0000551 Note that this method differs from :meth:`get_charset` which returns the
Georg Brandl3638e482009-04-27 16:46:17 +0000552 :class:`~email.charset.Charset` instance for the default encoding of the message body.
Georg Brandl116aa622007-08-15 14:28:22 +0000553
Georg Brandl116aa622007-08-15 14:28:22 +0000554
Georg Brandl3f076d82009-05-17 11:28:33 +0000555 .. method:: get_charsets(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000556
Benjamin Petersone41251e2008-04-25 01:59:09 +0000557 Return a list containing the character set names in the message. If the
558 message is a :mimetype:`multipart`, then the list will contain one element
559 for each subpart in the payload, otherwise, it will be a list of length 1.
Georg Brandl116aa622007-08-15 14:28:22 +0000560
Benjamin Petersone41251e2008-04-25 01:59:09 +0000561 Each item in the list will be a string which is the value of the
562 ``charset`` parameter in the :mailheader:`Content-Type` header for the
563 represented subpart. However, if the subpart has no
564 :mailheader:`Content-Type` header, no ``charset`` parameter, or is not of
565 the :mimetype:`text` main MIME type, then that item in the returned list
566 will be *failobj*.
Georg Brandl116aa622007-08-15 14:28:22 +0000567
568
Benjamin Petersone41251e2008-04-25 01:59:09 +0000569 .. method:: walk()
Georg Brandl116aa622007-08-15 14:28:22 +0000570
Benjamin Petersone41251e2008-04-25 01:59:09 +0000571 The :meth:`walk` method is an all-purpose generator which can be used to
572 iterate over all the parts and subparts of a message object tree, in
573 depth-first traversal order. You will typically use :meth:`walk` as the
574 iterator in a ``for`` loop; each iteration returns the next subpart.
Georg Brandl116aa622007-08-15 14:28:22 +0000575
Benjamin Petersone41251e2008-04-25 01:59:09 +0000576 Here's an example that prints the MIME type of every part of a multipart
R David Murrayfdfb0052013-07-29 15:49:58 -0400577 message structure:
Georg Brandl116aa622007-08-15 14:28:22 +0000578
R David Murrayfdfb0052013-07-29 15:49:58 -0400579 .. testsetup::
580
581 >>> from email import message_from_binary_file
582 >>> with open('Lib/test/test_email/data/msg_16.txt', 'rb') as f:
583 ... msg = message_from_binary_file(f)
584
585 .. doctest::
586
587 >>> for part in msg.walk():
588 ... print(part.get_content_type())
589 multipart/report
590 text/plain
591 message/delivery-status
592 text/plain
593 text/plain
594 message/rfc822
595 text/plain
Georg Brandl116aa622007-08-15 14:28:22 +0000596
Benjamin Petersone41251e2008-04-25 01:59:09 +0000597 :class:`Message` objects can also optionally contain two instance attributes,
598 which can be used when generating the plain text of a MIME message.
Georg Brandl116aa622007-08-15 14:28:22 +0000599
600
Benjamin Petersone41251e2008-04-25 01:59:09 +0000601 .. attribute:: preamble
Georg Brandl116aa622007-08-15 14:28:22 +0000602
Benjamin Petersone41251e2008-04-25 01:59:09 +0000603 The format of a MIME document allows for some text between the blank line
604 following the headers, and the first multipart boundary string. Normally,
605 this text is never visible in a MIME-aware mail reader because it falls
606 outside the standard MIME armor. However, when viewing the raw text of
607 the message, or when viewing the message in a non-MIME aware reader, this
608 text can become visible.
Georg Brandl116aa622007-08-15 14:28:22 +0000609
Benjamin Petersone41251e2008-04-25 01:59:09 +0000610 The *preamble* attribute contains this leading extra-armor text for MIME
Georg Brandl3638e482009-04-27 16:46:17 +0000611 documents. When the :class:`~email.parser.Parser` discovers some text
612 after the headers but before the first boundary string, it assigns this
613 text to the message's *preamble* attribute. When the
614 :class:`~email.generator.Generator` is writing out the plain text
615 representation of a MIME message, and it finds the
Benjamin Petersone41251e2008-04-25 01:59:09 +0000616 message has a *preamble* attribute, it will write this text in the area
617 between the headers and the first boundary. See :mod:`email.parser` and
618 :mod:`email.generator` for details.
Georg Brandl116aa622007-08-15 14:28:22 +0000619
Benjamin Petersone41251e2008-04-25 01:59:09 +0000620 Note that if the message object has no preamble, the *preamble* attribute
621 will be ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000622
623
Benjamin Petersone41251e2008-04-25 01:59:09 +0000624 .. attribute:: epilogue
Georg Brandl116aa622007-08-15 14:28:22 +0000625
Benjamin Petersone41251e2008-04-25 01:59:09 +0000626 The *epilogue* attribute acts the same way as the *preamble* attribute,
627 except that it contains text that appears between the last boundary and
628 the end of the message.
Georg Brandl116aa622007-08-15 14:28:22 +0000629
Benjamin Petersone41251e2008-04-25 01:59:09 +0000630 You do not need to set the epilogue to the empty string in order for the
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300631 :class:`~email.generator.Generator` to print a newline at the end of the
632 file.
Georg Brandl116aa622007-08-15 14:28:22 +0000633
634
Benjamin Petersone41251e2008-04-25 01:59:09 +0000635 .. attribute:: defects
Georg Brandl116aa622007-08-15 14:28:22 +0000636
Benjamin Petersone41251e2008-04-25 01:59:09 +0000637 The *defects* attribute contains a list of all the problems found when
638 parsing this message. See :mod:`email.errors` for a detailed description
639 of the possible parsing defects.