blob: 84a5f5138ccb7cd32835432c14cb191c1af28b3a [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 Murray3da240f2013-10-16 22:48:40 -040036 If *policy* is specified (it must be an instance of a :mod:`~email.policy`
37 class) use the rules it specifies to udpate and serialize the representation
Georg Brandled007d52013-11-24 16:09:26 +010038 of the message. If *policy* is not set, use the :class:`compat32
R David Murray3da240f2013-10-16 22:48:40 -040039 <email.policy.Compat32>` policy, which maintains backward compatibility with
40 the Python 3.2 version of the email package. For more information see the
R David Murraybb17d2b2013-08-09 16:15:28 -040041 :mod:`~email.policy` documentation.
42
43 .. versionchanged:: 3.3 The *policy* keyword argument was added.
Georg Brandl116aa622007-08-15 14:28:22 +000044
45
R David Murraybb17d2b2013-08-09 16:15:28 -040046 .. method:: as_string(unixfrom=False, maxheaderlen=0, policy=None)
Georg Brandl116aa622007-08-15 14:28:22 +000047
Benjamin Petersone41251e2008-04-25 01:59:09 +000048 Return the entire message flattened as a string. When optional *unixfrom*
R David Murraybb17d2b2013-08-09 16:15:28 -040049 is true, the envelope header is included in the returned string.
50 *unixfrom* defaults to ``False``. For backward compabitility reasons,
51 *maxheaderlen* defaults to ``0``, so if you want a different value you
52 must override it explicitly (the value specified for *max_line_length* in
53 the policy will be ignored by this method). The *policy* argument may be
54 used to override the default policy obtained from the message instance.
55 This can be used to control some of the formatting produced by the
56 method, since the specified *policy* will be passed to the ``Generator``.
57
58 Flattening the message may trigger changes to the :class:`Message` if
59 defaults need to be filled in to complete the transformation to a string
60 (for example, MIME boundaries may be generated or modified).
Georg Brandl116aa622007-08-15 14:28:22 +000061
Benjamin Petersone41251e2008-04-25 01:59:09 +000062 Note that this method is provided as a convenience and may not always
R David Murray7dedcb42011-03-15 14:01:18 -040063 format the message the way you want. For example, by default it does
64 not do the mangling of lines that begin with ``From`` that is
65 required by the unix mbox format. For more flexibility, instantiate a
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030066 :class:`~email.generator.Generator` instance and use its
67 :meth:`~email.generator.Generator.flatten` method directly. For example::
Georg Brandl116aa622007-08-15 14:28:22 +000068
Georg Brandl03124942008-06-10 15:50:56 +000069 from io import StringIO
Benjamin Petersone41251e2008-04-25 01:59:09 +000070 from email.generator import Generator
71 fp = StringIO()
R David Murray7dedcb42011-03-15 14:01:18 -040072 g = Generator(fp, mangle_from_=True, maxheaderlen=60)
Benjamin Petersone41251e2008-04-25 01:59:09 +000073 g.flatten(msg)
74 text = fp.getvalue()
Georg Brandl116aa622007-08-15 14:28:22 +000075
R David Murraybb17d2b2013-08-09 16:15:28 -040076 If the message object contains binary data that is not encoded according
77 to RFC standards, the non-compliant data will be replaced by unicode
78 "unknown character" code points. (See also :meth:`.as_bytes` and
79 :class:`~email.generator.BytesGenerator`.)
80
81 .. versionchanged:: 3.4 the *policy* keyword argument was added.
82
Georg Brandl116aa622007-08-15 14:28:22 +000083
Benjamin Petersone41251e2008-04-25 01:59:09 +000084 .. method:: __str__()
Georg Brandl116aa622007-08-15 14:28:22 +000085
R David Murraybb17d2b2013-08-09 16:15:28 -040086 Equivalent to :meth:`.as_string()`. Allows ``str(msg)`` to produce a
87 string containing the formatted message.
88
89
90 .. method:: as_bytes(unixfrom=False, policy=None)
91
92 Return the entire message flattened as a bytes object. When optional
93 *unixfrom* is true, the envelope header is included in the returned
94 string. *unixfrom* defaults to ``False``. The *policy* argument may be
95 used to override the default policy obtained from the message instance.
96 This can be used to control some of the formatting produced by the
97 method, since the specified *policy* will be passed to the
98 ``BytesGenerator``.
99
100 Flattening the message may trigger changes to the :class:`Message` if
101 defaults need to be filled in to complete the transformation to a string
102 (for example, MIME boundaries may be generated or modified).
103
104 Note that this method is provided as a convenience and may not always
105 format the message the way you want. For example, by default it does
106 not do the mangling of lines that begin with ``From`` that is
107 required by the unix mbox format. For more flexibility, instantiate a
108 :class:`~email.generator.BytesGenerator` instance and use its
Serhiy Storchaka319f3a12013-08-19 10:03:11 +0300109 :meth:`~email.generator.BytesGenerator.flatten` method directly.
110 For example::
R David Murraybb17d2b2013-08-09 16:15:28 -0400111
112 from io import BytesIO
113 from email.generator import BytesGenerator
114 fp = BytesIO()
115 g = BytesGenerator(fp, mangle_from_=True, maxheaderlen=60)
116 g.flatten(msg)
117 text = fp.getvalue()
118
119 .. versionadded:: 3.4
120
121
122 .. method:: __bytes__()
123
124 Equivalent to :meth:`.as_bytes()`. Allows ``bytes(msg)`` to produce a
125 bytes object containing the formatted message.
126
127 .. versionadded:: 3.4
Georg Brandl116aa622007-08-15 14:28:22 +0000128
129
Benjamin Petersone41251e2008-04-25 01:59:09 +0000130 .. method:: is_multipart()
Georg Brandl116aa622007-08-15 14:28:22 +0000131
Benjamin Petersone41251e2008-04-25 01:59:09 +0000132 Return ``True`` if the message's payload is a list of sub-\
133 :class:`Message` objects, otherwise return ``False``. When
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200134 :meth:`is_multipart` returns ``False``, the payload should be a string object.
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136
Benjamin Petersone41251e2008-04-25 01:59:09 +0000137 .. method:: set_unixfrom(unixfrom)
Georg Brandl116aa622007-08-15 14:28:22 +0000138
Benjamin Petersone41251e2008-04-25 01:59:09 +0000139 Set the message's envelope header to *unixfrom*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141
Benjamin Petersone41251e2008-04-25 01:59:09 +0000142 .. method:: get_unixfrom()
Georg Brandl116aa622007-08-15 14:28:22 +0000143
Benjamin Petersone41251e2008-04-25 01:59:09 +0000144 Return the message's envelope header. Defaults to ``None`` if the
145 envelope header was never set.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
147
Benjamin Petersone41251e2008-04-25 01:59:09 +0000148 .. method:: attach(payload)
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Benjamin Petersone41251e2008-04-25 01:59:09 +0000150 Add the given *payload* to the current payload, which must be ``None`` or
151 a list of :class:`Message` objects before the call. After the call, the
152 payload will always be a list of :class:`Message` objects. If you want to
153 set the payload to a scalar object (e.g. a string), use
154 :meth:`set_payload` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156
Georg Brandl3f076d82009-05-17 11:28:33 +0000157 .. method:: get_payload(i=None, decode=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000158
Benjamin Petersond6313712008-07-31 16:23:04 +0000159 Return the current payload, which will be a list of
Benjamin Petersone41251e2008-04-25 01:59:09 +0000160 :class:`Message` objects when :meth:`is_multipart` is ``True``, or a
161 string when :meth:`is_multipart` is ``False``. If the payload is a list
162 and you mutate the list object, you modify the message's payload in place.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Benjamin Petersone41251e2008-04-25 01:59:09 +0000164 With optional argument *i*, :meth:`get_payload` will return the *i*-th
165 element of the payload, counting from zero, if :meth:`is_multipart` is
166 ``True``. An :exc:`IndexError` will be raised if *i* is less than 0 or
167 greater than or equal to the number of items in the payload. If the
168 payload is a string (i.e. :meth:`is_multipart` is ``False``) and *i* is
169 given, a :exc:`TypeError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000170
Benjamin Petersone41251e2008-04-25 01:59:09 +0000171 Optional *decode* is a flag indicating whether the payload should be
172 decoded or not, according to the :mailheader:`Content-Transfer-Encoding`
173 header. When ``True`` and the message is not a multipart, the payload will
174 be decoded if this header's value is ``quoted-printable`` or ``base64``.
175 If some other encoding is used, or :mailheader:`Content-Transfer-Encoding`
R David Murray80e0aee2012-05-27 21:23:34 -0400176 header is missing, the payload is
R. David Murray96fd54e2010-10-08 15:55:28 +0000177 returned as-is (undecoded). In all cases the returned value is binary
178 data. If the message is a multipart and the *decode* flag is ``True``,
R David Murray80e0aee2012-05-27 21:23:34 -0400179 then ``None`` is returned. If the payload is base64 and it was not
180 perfectly formed (missing padding, characters outside the base64
181 alphabet), then an appropriate defect will be added to the message's
182 defect property (:class:`~email.errors.InvalidBase64PaddingDefect` or
183 :class:`~email.errors.InvalidBase64CharactersDefect`, respectively).
R. David Murray96fd54e2010-10-08 15:55:28 +0000184
185 When *decode* is ``False`` (the default) the body is returned as a string
186 without decoding the :mailheader:`Content-Transfer-Encoding`. However,
187 for a :mailheader:`Content-Transfer-Encoding` of 8bit, an attempt is made
Senthil Kumaran82270452010-10-15 13:29:33 +0000188 to decode the original bytes using the ``charset`` specified by the
189 :mailheader:`Content-Type` header, using the ``replace`` error handler.
190 If no ``charset`` is specified, or if the ``charset`` given is not
191 recognized by the email package, the body is decoded using the default
192 ASCII charset.
Georg Brandl116aa622007-08-15 14:28:22 +0000193
194
Georg Brandl3f076d82009-05-17 11:28:33 +0000195 .. method:: set_payload(payload, charset=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000196
Benjamin Petersone41251e2008-04-25 01:59:09 +0000197 Set the entire message object's payload to *payload*. It is the client's
198 responsibility to ensure the payload invariants. Optional *charset* sets
199 the message's default character set; see :meth:`set_charset` for details.
Georg Brandl116aa622007-08-15 14:28:22 +0000200
Benjamin Petersone41251e2008-04-25 01:59:09 +0000201 .. method:: set_charset(charset)
Georg Brandl116aa622007-08-15 14:28:22 +0000202
Benjamin Petersone41251e2008-04-25 01:59:09 +0000203 Set the character set of the payload to *charset*, which can either be a
Georg Brandl3638e482009-04-27 16:46:17 +0000204 :class:`~email.charset.Charset` instance (see :mod:`email.charset`), a
205 string naming a character set, or ``None``. If it is a string, it will
206 be converted to a :class:`~email.charset.Charset` instance. If *charset*
207 is ``None``, the ``charset`` parameter will be removed from the
R David Murraye3d09ff2011-03-15 17:41:13 -0400208 :mailheader:`Content-Type` header (the message will not be otherwise
209 modified). Anything else will generate a :exc:`TypeError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000210
R David Murraye3d09ff2011-03-15 17:41:13 -0400211 If there is no existing :mailheader:`MIME-Version` header one will be
212 added. If there is no existing :mailheader:`Content-Type` header, one
213 will be added with a value of :mimetype:`text/plain`. Whether the
214 :mailheader:`Content-Type` header already exists or not, its ``charset``
215 parameter will be set to *charset.output_charset*. If
216 *charset.input_charset* and *charset.output_charset* differ, the payload
217 will be re-encoded to the *output_charset*. If there is no existing
218 :mailheader:`Content-Transfer-Encoding` header, then the payload will be
219 transfer-encoded, if needed, using the specified
220 :class:`~email.charset.Charset`, and a header with the appropriate value
221 will be added. If a :mailheader:`Content-Transfer-Encoding` header
222 already exists, the payload is assumed to already be correctly encoded
223 using that :mailheader:`Content-Transfer-Encoding` and is not modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000224
Benjamin Petersone41251e2008-04-25 01:59:09 +0000225 .. method:: get_charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000226
Georg Brandl3638e482009-04-27 16:46:17 +0000227 Return the :class:`~email.charset.Charset` instance associated with the
228 message's payload.
Georg Brandl116aa622007-08-15 14:28:22 +0000229
Benjamin Petersone41251e2008-04-25 01:59:09 +0000230 The following methods implement a mapping-like interface for accessing the
231 message's :rfc:`2822` headers. Note that there are some semantic differences
232 between these methods and a normal mapping (i.e. dictionary) interface. For
233 example, in a dictionary there are no duplicate keys, but here there may be
234 duplicate message headers. Also, in dictionaries there is no guaranteed
235 order to the keys returned by :meth:`keys`, but in a :class:`Message` object,
236 headers are always returned in the order they appeared in the original
237 message, or were added to the message later. Any header deleted and then
238 re-added are always appended to the end of the header list.
Georg Brandl116aa622007-08-15 14:28:22 +0000239
Benjamin Petersone41251e2008-04-25 01:59:09 +0000240 These semantic differences are intentional and are biased toward maximal
241 convenience.
Georg Brandl116aa622007-08-15 14:28:22 +0000242
Benjamin Petersone41251e2008-04-25 01:59:09 +0000243 Note that in all cases, any envelope header present in the message is not
244 included in the mapping interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000245
R. David Murray92532142011-01-07 23:25:30 +0000246 In a model generated from bytes, any header values that (in contravention of
247 the RFCs) contain non-ASCII bytes will, when retrieved through this
248 interface, be represented as :class:`~email.header.Header` objects with
249 a charset of `unknown-8bit`.
R. David Murray96fd54e2010-10-08 15:55:28 +0000250
Georg Brandl116aa622007-08-15 14:28:22 +0000251
Benjamin Petersone41251e2008-04-25 01:59:09 +0000252 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000253
Benjamin Petersone41251e2008-04-25 01:59:09 +0000254 Return the total number of headers, including duplicates.
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256
Benjamin Petersone41251e2008-04-25 01:59:09 +0000257 .. method:: __contains__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000258
Benjamin Petersone41251e2008-04-25 01:59:09 +0000259 Return true if the message object has a field named *name*. Matching is
260 done case-insensitively and *name* should not include the trailing colon.
261 Used for the ``in`` operator, e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000262
Benjamin Petersone41251e2008-04-25 01:59:09 +0000263 if 'message-id' in myMessage:
264 print('Message-ID:', myMessage['message-id'])
Georg Brandl116aa622007-08-15 14:28:22 +0000265
266
Benjamin Petersone41251e2008-04-25 01:59:09 +0000267 .. method:: __getitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000268
Benjamin Petersone41251e2008-04-25 01:59:09 +0000269 Return the value of the named header field. *name* should not include the
270 colon field separator. If the header is missing, ``None`` is returned; a
271 :exc:`KeyError` is never raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000272
Benjamin Petersone41251e2008-04-25 01:59:09 +0000273 Note that if the named field appears more than once in the message's
274 headers, exactly which of those field values will be returned is
275 undefined. Use the :meth:`get_all` method to get the values of all the
276 extant named headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000277
278
Benjamin Petersone41251e2008-04-25 01:59:09 +0000279 .. method:: __setitem__(name, val)
Georg Brandl116aa622007-08-15 14:28:22 +0000280
Benjamin Petersone41251e2008-04-25 01:59:09 +0000281 Add a header to the message with field name *name* and value *val*. The
282 field is appended to the end of the message's existing fields.
Georg Brandl116aa622007-08-15 14:28:22 +0000283
Benjamin Petersone41251e2008-04-25 01:59:09 +0000284 Note that this does *not* overwrite or delete any existing header with the same
285 name. If you want to ensure that the new header is the only one present in the
286 message with field name *name*, delete the field first, e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Benjamin Petersone41251e2008-04-25 01:59:09 +0000288 del msg['subject']
289 msg['subject'] = 'Python roolz!'
Georg Brandl116aa622007-08-15 14:28:22 +0000290
291
Benjamin Petersone41251e2008-04-25 01:59:09 +0000292 .. method:: __delitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000293
Benjamin Petersone41251e2008-04-25 01:59:09 +0000294 Delete all occurrences of the field with name *name* from the message's
Georg Brandl3f076d82009-05-17 11:28:33 +0000295 headers. No exception is raised if the named field isn't present in the
296 headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000297
298
Benjamin Petersone41251e2008-04-25 01:59:09 +0000299 .. method:: keys()
Georg Brandl116aa622007-08-15 14:28:22 +0000300
Benjamin Petersone41251e2008-04-25 01:59:09 +0000301 Return a list of all the message's header field names.
Georg Brandl116aa622007-08-15 14:28:22 +0000302
303
Benjamin Petersone41251e2008-04-25 01:59:09 +0000304 .. method:: values()
Georg Brandl116aa622007-08-15 14:28:22 +0000305
Benjamin Petersone41251e2008-04-25 01:59:09 +0000306 Return a list of all the message's field values.
Georg Brandl116aa622007-08-15 14:28:22 +0000307
308
Benjamin Petersone41251e2008-04-25 01:59:09 +0000309 .. method:: items()
Georg Brandl116aa622007-08-15 14:28:22 +0000310
Benjamin Petersone41251e2008-04-25 01:59:09 +0000311 Return a list of 2-tuples containing all the message's field headers and
312 values.
Georg Brandl116aa622007-08-15 14:28:22 +0000313
314
Georg Brandl3f076d82009-05-17 11:28:33 +0000315 .. method:: get(name, failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000316
Benjamin Petersone41251e2008-04-25 01:59:09 +0000317 Return the value of the named header field. This is identical to
318 :meth:`__getitem__` except that optional *failobj* is returned if the
319 named header is missing (defaults to ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000320
Benjamin Petersone41251e2008-04-25 01:59:09 +0000321 Here are some additional useful methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000322
323
Georg Brandl3f076d82009-05-17 11:28:33 +0000324 .. method:: get_all(name, failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000325
Benjamin Petersone41251e2008-04-25 01:59:09 +0000326 Return a list of all the values for the field named *name*. If there are
327 no such named headers in the message, *failobj* is returned (defaults to
328 ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000329
330
Benjamin Petersone41251e2008-04-25 01:59:09 +0000331 .. method:: add_header(_name, _value, **_params)
Georg Brandl116aa622007-08-15 14:28:22 +0000332
Benjamin Petersone41251e2008-04-25 01:59:09 +0000333 Extended header setting. This method is similar to :meth:`__setitem__`
334 except that additional header parameters can be provided as keyword
335 arguments. *_name* is the header field to add and *_value* is the
336 *primary* value for the header.
Georg Brandl116aa622007-08-15 14:28:22 +0000337
Benjamin Petersone41251e2008-04-25 01:59:09 +0000338 For each item in the keyword argument dictionary *_params*, the key is
339 taken as the parameter name, with underscores converted to dashes (since
340 dashes are illegal in Python identifiers). Normally, the parameter will
341 be added as ``key="value"`` unless the value is ``None``, in which case
R. David Murray7ec754b2010-12-13 23:51:19 +0000342 only the key will be added. If the value contains non-ASCII characters,
343 it can be specified as a three tuple in the format
344 ``(CHARSET, LANGUAGE, VALUE)``, where ``CHARSET`` is a string naming the
345 charset to be used to encode the value, ``LANGUAGE`` can usually be set
Georg Brandlc8843502010-12-19 10:28:46 +0000346 to ``None`` or the empty string (see :rfc:`2231` for other possibilities),
R. David Murray7ec754b2010-12-13 23:51:19 +0000347 and ``VALUE`` is the string value containing non-ASCII code points. If
348 a three tuple is not passed and the value contains non-ASCII characters,
Georg Brandlc8843502010-12-19 10:28:46 +0000349 it is automatically encoded in :rfc:`2231` format using a ``CHARSET``
R. David Murray7ec754b2010-12-13 23:51:19 +0000350 of ``utf-8`` and a ``LANGUAGE`` of ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000351
Benjamin Petersone41251e2008-04-25 01:59:09 +0000352 Here's an example::
Georg Brandl116aa622007-08-15 14:28:22 +0000353
Benjamin Petersone41251e2008-04-25 01:59:09 +0000354 msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
Georg Brandl116aa622007-08-15 14:28:22 +0000355
Benjamin Petersone41251e2008-04-25 01:59:09 +0000356 This will add a header that looks like ::
Georg Brandl116aa622007-08-15 14:28:22 +0000357
Benjamin Petersone41251e2008-04-25 01:59:09 +0000358 Content-Disposition: attachment; filename="bud.gif"
Georg Brandl116aa622007-08-15 14:28:22 +0000359
Ezio Melottie130a522011-10-19 10:58:56 +0300360 An example with non-ASCII characters::
R. David Murray7ec754b2010-12-13 23:51:19 +0000361
362 msg.add_header('Content-Disposition', 'attachment',
363 filename=('iso-8859-1', '', 'Fußballer.ppt'))
364
365 Which produces ::
366
367 Content-Disposition: attachment; filename*="iso-8859-1''Fu%DFballer.ppt"
368
Georg Brandl116aa622007-08-15 14:28:22 +0000369
Benjamin Petersone41251e2008-04-25 01:59:09 +0000370 .. method:: replace_header(_name, _value)
Georg Brandl116aa622007-08-15 14:28:22 +0000371
Benjamin Petersone41251e2008-04-25 01:59:09 +0000372 Replace a header. Replace the first header found in the message that
373 matches *_name*, retaining header order and field name case. If no
374 matching header was found, a :exc:`KeyError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000375
Georg Brandl116aa622007-08-15 14:28:22 +0000376
Benjamin Petersone41251e2008-04-25 01:59:09 +0000377 .. method:: get_content_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000378
Benjamin Petersone41251e2008-04-25 01:59:09 +0000379 Return the message's content type. The returned string is coerced to
380 lower case of the form :mimetype:`maintype/subtype`. If there was no
381 :mailheader:`Content-Type` header in the message the default type as given
382 by :meth:`get_default_type` will be returned. Since according to
383 :rfc:`2045`, messages always have a default type, :meth:`get_content_type`
384 will always return a value.
Georg Brandl116aa622007-08-15 14:28:22 +0000385
Benjamin Petersone41251e2008-04-25 01:59:09 +0000386 :rfc:`2045` defines a message's default type to be :mimetype:`text/plain`
387 unless it appears inside a :mimetype:`multipart/digest` container, in
388 which case it would be :mimetype:`message/rfc822`. If the
389 :mailheader:`Content-Type` header has an invalid type specification,
390 :rfc:`2045` mandates that the default type be :mimetype:`text/plain`.
Georg Brandl116aa622007-08-15 14:28:22 +0000391
Georg Brandl116aa622007-08-15 14:28:22 +0000392
Benjamin Petersone41251e2008-04-25 01:59:09 +0000393 .. method:: get_content_maintype()
Georg Brandl116aa622007-08-15 14:28:22 +0000394
Benjamin Petersone41251e2008-04-25 01:59:09 +0000395 Return the message's main content type. This is the :mimetype:`maintype`
396 part of the string returned by :meth:`get_content_type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000397
Georg Brandl116aa622007-08-15 14:28:22 +0000398
Benjamin Petersone41251e2008-04-25 01:59:09 +0000399 .. method:: get_content_subtype()
Georg Brandl116aa622007-08-15 14:28:22 +0000400
Benjamin Petersone41251e2008-04-25 01:59:09 +0000401 Return the message's sub-content type. This is the :mimetype:`subtype`
402 part of the string returned by :meth:`get_content_type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000403
Georg Brandl116aa622007-08-15 14:28:22 +0000404
Benjamin Petersone41251e2008-04-25 01:59:09 +0000405 .. method:: get_default_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000406
Benjamin Petersone41251e2008-04-25 01:59:09 +0000407 Return the default content type. Most messages have a default content
408 type of :mimetype:`text/plain`, except for messages that are subparts of
409 :mimetype:`multipart/digest` containers. Such subparts have a default
410 content type of :mimetype:`message/rfc822`.
Georg Brandl116aa622007-08-15 14:28:22 +0000411
Georg Brandl116aa622007-08-15 14:28:22 +0000412
Benjamin Petersone41251e2008-04-25 01:59:09 +0000413 .. method:: set_default_type(ctype)
Georg Brandl116aa622007-08-15 14:28:22 +0000414
Benjamin Petersone41251e2008-04-25 01:59:09 +0000415 Set the default content type. *ctype* should either be
416 :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is not
417 enforced. The default content type is not stored in the
418 :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000419
Georg Brandl116aa622007-08-15 14:28:22 +0000420
Georg Brandl3f076d82009-05-17 11:28:33 +0000421 .. method:: get_params(failobj=None, header='content-type', unquote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Benjamin Petersone41251e2008-04-25 01:59:09 +0000423 Return the message's :mailheader:`Content-Type` parameters, as a list.
424 The elements of the returned list are 2-tuples of key/value pairs, as
425 split on the ``'='`` sign. The left hand side of the ``'='`` is the key,
426 while the right hand side is the value. If there is no ``'='`` sign in
427 the parameter the value is the empty string, otherwise the value is as
428 described in :meth:`get_param` and is unquoted if optional *unquote* is
429 ``True`` (the default).
Georg Brandl116aa622007-08-15 14:28:22 +0000430
Benjamin Petersone41251e2008-04-25 01:59:09 +0000431 Optional *failobj* is the object to return if there is no
432 :mailheader:`Content-Type` header. Optional *header* is the header to
433 search instead of :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000434
Georg Brandl116aa622007-08-15 14:28:22 +0000435
Georg Brandl3f076d82009-05-17 11:28:33 +0000436 .. method:: get_param(param, failobj=None, header='content-type', unquote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000437
Benjamin Petersone41251e2008-04-25 01:59:09 +0000438 Return the value of the :mailheader:`Content-Type` header's parameter
439 *param* as a string. If the message has no :mailheader:`Content-Type`
440 header or if there is no such parameter, then *failobj* is returned
441 (defaults to ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000442
Benjamin Petersone41251e2008-04-25 01:59:09 +0000443 Optional *header* if given, specifies the message header to use instead of
444 :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000445
Benjamin Petersone41251e2008-04-25 01:59:09 +0000446 Parameter keys are always compared case insensitively. The return value
447 can either be a string, or a 3-tuple if the parameter was :rfc:`2231`
448 encoded. When it's a 3-tuple, the elements of the value are of the form
449 ``(CHARSET, LANGUAGE, VALUE)``. Note that both ``CHARSET`` and
450 ``LANGUAGE`` can be ``None``, in which case you should consider ``VALUE``
451 to be encoded in the ``us-ascii`` charset. You can usually ignore
452 ``LANGUAGE``.
Georg Brandl116aa622007-08-15 14:28:22 +0000453
Benjamin Petersone41251e2008-04-25 01:59:09 +0000454 If your application doesn't care whether the parameter was encoded as in
455 :rfc:`2231`, you can collapse the parameter value by calling
Georg Brandl540b45c2009-04-27 16:45:26 +0000456 :func:`email.utils.collapse_rfc2231_value`, passing in the return value
Benjamin Petersone41251e2008-04-25 01:59:09 +0000457 from :meth:`get_param`. This will return a suitably decoded Unicode
R. David Murray7ec754b2010-12-13 23:51:19 +0000458 string when the value is a tuple, or the original string unquoted if it
Benjamin Petersone41251e2008-04-25 01:59:09 +0000459 isn't. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000460
Benjamin Petersone41251e2008-04-25 01:59:09 +0000461 rawparam = msg.get_param('foo')
Georg Brandl540b45c2009-04-27 16:45:26 +0000462 param = email.utils.collapse_rfc2231_value(rawparam)
Georg Brandl116aa622007-08-15 14:28:22 +0000463
Benjamin Petersone41251e2008-04-25 01:59:09 +0000464 In any case, the parameter value (either the returned string, or the
465 ``VALUE`` item in the 3-tuple) is always unquoted, unless *unquote* is set
466 to ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000467
Georg Brandl116aa622007-08-15 14:28:22 +0000468
R David Murray3da240f2013-10-16 22:48:40 -0400469 .. method:: set_param(param, value, header='Content-Type', requote=True,
470 charset=None, language='', replace=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000471
Benjamin Petersone41251e2008-04-25 01:59:09 +0000472 Set a parameter in the :mailheader:`Content-Type` header. If the
473 parameter already exists in the header, its value will be replaced with
474 *value*. If the :mailheader:`Content-Type` header as not yet been defined
475 for this message, it will be set to :mimetype:`text/plain` and the new
476 parameter value will be appended as per :rfc:`2045`.
Georg Brandl116aa622007-08-15 14:28:22 +0000477
Benjamin Petersone41251e2008-04-25 01:59:09 +0000478 Optional *header* specifies an alternative header to
479 :mailheader:`Content-Type`, and all parameters will be quoted as necessary
480 unless optional *requote* is ``False`` (the default is ``True``).
Georg Brandl116aa622007-08-15 14:28:22 +0000481
Benjamin Petersone41251e2008-04-25 01:59:09 +0000482 If optional *charset* is specified, the parameter will be encoded
483 according to :rfc:`2231`. Optional *language* specifies the RFC 2231
484 language, defaulting to the empty string. Both *charset* and *language*
485 should be strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000486
R David Murray3da240f2013-10-16 22:48:40 -0400487 If *replace* is ``False`` (the default) the header is moved to the
488 end of the list of headers. If *replace* is ``True``, the header
489 will be updated in place.
490
491 .. versionchanged: 3.4 ``replace`` keyword was added.
492
Georg Brandl116aa622007-08-15 14:28:22 +0000493
Georg Brandl3f076d82009-05-17 11:28:33 +0000494 .. method:: del_param(param, header='content-type', requote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000495
Benjamin Petersone41251e2008-04-25 01:59:09 +0000496 Remove the given parameter completely from the :mailheader:`Content-Type`
497 header. The header will be re-written in place without the parameter or
498 its value. All values will be quoted as necessary unless *requote* is
499 ``False`` (the default is ``True``). Optional *header* specifies an
500 alternative to :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000501
Georg Brandl116aa622007-08-15 14:28:22 +0000502
Georg Brandl3f076d82009-05-17 11:28:33 +0000503 .. method:: set_type(type, header='Content-Type', requote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000504
Benjamin Petersone41251e2008-04-25 01:59:09 +0000505 Set the main type and subtype for the :mailheader:`Content-Type`
506 header. *type* must be a string in the form :mimetype:`maintype/subtype`,
507 otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000508
Benjamin Petersone41251e2008-04-25 01:59:09 +0000509 This method replaces the :mailheader:`Content-Type` header, keeping all
510 the parameters in place. If *requote* is ``False``, this leaves the
511 existing header's quoting as is, otherwise the parameters will be quoted
512 (the default).
Georg Brandl116aa622007-08-15 14:28:22 +0000513
Benjamin Petersone41251e2008-04-25 01:59:09 +0000514 An alternative header can be specified in the *header* argument. When the
515 :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version`
516 header is also added.
Georg Brandl116aa622007-08-15 14:28:22 +0000517
Georg Brandl116aa622007-08-15 14:28:22 +0000518
Georg Brandl3f076d82009-05-17 11:28:33 +0000519 .. method:: get_filename(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000520
Benjamin Petersone41251e2008-04-25 01:59:09 +0000521 Return the value of the ``filename`` parameter of the
522 :mailheader:`Content-Disposition` header of the message. If the header
523 does not have a ``filename`` parameter, this method falls back to looking
R. David Murray9ed34be2010-03-04 17:38:18 +0000524 for the ``name`` parameter on the :mailheader:`Content-Type` header. If
525 neither is found, or the header is missing, then *failobj* is returned.
526 The returned string will always be unquoted as per
527 :func:`email.utils.unquote`.
Georg Brandl116aa622007-08-15 14:28:22 +0000528
529
Georg Brandl3f076d82009-05-17 11:28:33 +0000530 .. method:: get_boundary(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000531
Benjamin Petersone41251e2008-04-25 01:59:09 +0000532 Return the value of the ``boundary`` parameter of the
533 :mailheader:`Content-Type` header of the message, or *failobj* if either
534 the header is missing, or has no ``boundary`` parameter. The returned
Georg Brandl540b45c2009-04-27 16:45:26 +0000535 string will always be unquoted as per :func:`email.utils.unquote`.
Georg Brandl116aa622007-08-15 14:28:22 +0000536
537
Benjamin Petersone41251e2008-04-25 01:59:09 +0000538 .. method:: set_boundary(boundary)
Georg Brandl116aa622007-08-15 14:28:22 +0000539
Benjamin Petersone41251e2008-04-25 01:59:09 +0000540 Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
541 *boundary*. :meth:`set_boundary` will always quote *boundary* if
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300542 necessary. A :exc:`~email.errors.HeaderParseError` is raised if the
543 message object has no :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000544
Benjamin Petersone41251e2008-04-25 01:59:09 +0000545 Note that using this method is subtly different than deleting the old
546 :mailheader:`Content-Type` header and adding a new one with the new
547 boundary via :meth:`add_header`, because :meth:`set_boundary` preserves
548 the order of the :mailheader:`Content-Type` header in the list of
549 headers. However, it does *not* preserve any continuation lines which may
550 have been present in the original :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000551
552
Georg Brandl3f076d82009-05-17 11:28:33 +0000553 .. method:: get_content_charset(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000554
Benjamin Petersone41251e2008-04-25 01:59:09 +0000555 Return the ``charset`` parameter of the :mailheader:`Content-Type` header,
556 coerced to lower case. If there is no :mailheader:`Content-Type` header, or if
557 that header has no ``charset`` parameter, *failobj* is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000558
Benjamin Petersone41251e2008-04-25 01:59:09 +0000559 Note that this method differs from :meth:`get_charset` which returns the
Georg Brandl3638e482009-04-27 16:46:17 +0000560 :class:`~email.charset.Charset` instance for the default encoding of the message body.
Georg Brandl116aa622007-08-15 14:28:22 +0000561
Georg Brandl116aa622007-08-15 14:28:22 +0000562
Georg Brandl3f076d82009-05-17 11:28:33 +0000563 .. method:: get_charsets(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000564
Benjamin Petersone41251e2008-04-25 01:59:09 +0000565 Return a list containing the character set names in the message. If the
566 message is a :mimetype:`multipart`, then the list will contain one element
567 for each subpart in the payload, otherwise, it will be a list of length 1.
Georg Brandl116aa622007-08-15 14:28:22 +0000568
Benjamin Petersone41251e2008-04-25 01:59:09 +0000569 Each item in the list will be a string which is the value of the
570 ``charset`` parameter in the :mailheader:`Content-Type` header for the
571 represented subpart. However, if the subpart has no
572 :mailheader:`Content-Type` header, no ``charset`` parameter, or is not of
573 the :mimetype:`text` main MIME type, then that item in the returned list
574 will be *failobj*.
Georg Brandl116aa622007-08-15 14:28:22 +0000575
576
Benjamin Petersone41251e2008-04-25 01:59:09 +0000577 .. method:: walk()
Georg Brandl116aa622007-08-15 14:28:22 +0000578
Benjamin Petersone41251e2008-04-25 01:59:09 +0000579 The :meth:`walk` method is an all-purpose generator which can be used to
580 iterate over all the parts and subparts of a message object tree, in
581 depth-first traversal order. You will typically use :meth:`walk` as the
582 iterator in a ``for`` loop; each iteration returns the next subpart.
Georg Brandl116aa622007-08-15 14:28:22 +0000583
Benjamin Petersone41251e2008-04-25 01:59:09 +0000584 Here's an example that prints the MIME type of every part of a multipart
R David Murrayfdfb0052013-07-29 15:49:58 -0400585 message structure:
Georg Brandl116aa622007-08-15 14:28:22 +0000586
R David Murrayfdfb0052013-07-29 15:49:58 -0400587 .. testsetup::
588
589 >>> from email import message_from_binary_file
590 >>> with open('Lib/test/test_email/data/msg_16.txt', 'rb') as f:
591 ... msg = message_from_binary_file(f)
592
593 .. doctest::
594
595 >>> for part in msg.walk():
596 ... print(part.get_content_type())
597 multipart/report
598 text/plain
599 message/delivery-status
600 text/plain
601 text/plain
602 message/rfc822
603 text/plain
Georg Brandl116aa622007-08-15 14:28:22 +0000604
Benjamin Petersone41251e2008-04-25 01:59:09 +0000605 :class:`Message` objects can also optionally contain two instance attributes,
606 which can be used when generating the plain text of a MIME message.
Georg Brandl116aa622007-08-15 14:28:22 +0000607
608
Benjamin Petersone41251e2008-04-25 01:59:09 +0000609 .. attribute:: preamble
Georg Brandl116aa622007-08-15 14:28:22 +0000610
Benjamin Petersone41251e2008-04-25 01:59:09 +0000611 The format of a MIME document allows for some text between the blank line
612 following the headers, and the first multipart boundary string. Normally,
613 this text is never visible in a MIME-aware mail reader because it falls
614 outside the standard MIME armor. However, when viewing the raw text of
615 the message, or when viewing the message in a non-MIME aware reader, this
616 text can become visible.
Georg Brandl116aa622007-08-15 14:28:22 +0000617
Benjamin Petersone41251e2008-04-25 01:59:09 +0000618 The *preamble* attribute contains this leading extra-armor text for MIME
Georg Brandl3638e482009-04-27 16:46:17 +0000619 documents. When the :class:`~email.parser.Parser` discovers some text
620 after the headers but before the first boundary string, it assigns this
621 text to the message's *preamble* attribute. When the
622 :class:`~email.generator.Generator` is writing out the plain text
623 representation of a MIME message, and it finds the
Benjamin Petersone41251e2008-04-25 01:59:09 +0000624 message has a *preamble* attribute, it will write this text in the area
625 between the headers and the first boundary. See :mod:`email.parser` and
626 :mod:`email.generator` for details.
Georg Brandl116aa622007-08-15 14:28:22 +0000627
Benjamin Petersone41251e2008-04-25 01:59:09 +0000628 Note that if the message object has no preamble, the *preamble* attribute
629 will be ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000630
631
Benjamin Petersone41251e2008-04-25 01:59:09 +0000632 .. attribute:: epilogue
Georg Brandl116aa622007-08-15 14:28:22 +0000633
Benjamin Petersone41251e2008-04-25 01:59:09 +0000634 The *epilogue* attribute acts the same way as the *preamble* attribute,
635 except that it contains text that appears between the last boundary and
636 the end of the message.
Georg Brandl116aa622007-08-15 14:28:22 +0000637
Benjamin Petersone41251e2008-04-25 01:59:09 +0000638 You do not need to set the epilogue to the empty string in order for the
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300639 :class:`~email.generator.Generator` to print a newline at the end of the
640 file.
Georg Brandl116aa622007-08-15 14:28:22 +0000641
642
Benjamin Petersone41251e2008-04-25 01:59:09 +0000643 .. attribute:: defects
Georg Brandl116aa622007-08-15 14:28:22 +0000644
Benjamin Petersone41251e2008-04-25 01:59:09 +0000645 The *defects* attribute contains a list of all the problems found when
646 parsing this message. See :mod:`email.errors` for a detailed description
647 of the possible parsing defects.