blob: 91a694f3ecf7aafd952121e4440823f12b600c01 [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/email/message.py`
8
9--------------
Georg Brandl116aa622007-08-15 14:28:22 +000010
11The central class in the :mod:`email` package is the :class:`Message` class,
12imported 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
14setting and querying header fields, and for accessing message bodies.
15
16Conceptually, a :class:`Message` object consists of *headers* and *payloads*.
17Headers are :rfc:`2822` style field names and values where the field name and
18value are separated by a colon. The colon is not part of either the field name
19or the field value.
20
21Headers are stored and returned in case-preserving form but are matched
22case-insensitively. There may also be a single envelope header, also known as
23the *Unix-From* header or the ``From_`` header. The payload is either a string
24in the case of simple message objects or a list of :class:`Message` objects for
25MIME container documents (e.g. :mimetype:`multipart/\*` and
26:mimetype:`message/rfc822`).
27
28:class:`Message` objects provide a mapping style interface for accessing the
29message headers, and an explicit interface for accessing both the headers and
30the payload. It provides convenience methods for generating a flat text
31representation of the message object tree, for accessing commonly used header
32parameters, and for recursively walking over the object tree.
33
34Here are the methods of the :class:`Message` class:
35
36
R David Murraybb17d2b2013-08-09 16:15:28 -040037.. class:: Message(policy=compat32)
Georg Brandl116aa622007-08-15 14:28:22 +000038
R David Murray3da240f2013-10-16 22:48:40 -040039 If *policy* is specified (it must be an instance of a :mod:`~email.policy`
R David Murraya83ade12014-05-08 10:05:47 -040040 class) use the rules it specifies to update and serialize the representation
Georg Brandled007d52013-11-24 16:09:26 +010041 of the message. If *policy* is not set, use the :class:`compat32
R David Murray3da240f2013-10-16 22:48:40 -040042 <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 Murraybb17d2b2013-08-09 16:15:28 -040044 :mod:`~email.policy` documentation.
45
46 .. versionchanged:: 3.3 The *policy* keyword argument was added.
Georg Brandl116aa622007-08-15 14:28:22 +000047
48
R David Murraybb17d2b2013-08-09 16:15:28 -040049 .. method:: as_string(unixfrom=False, maxheaderlen=0, policy=None)
Georg Brandl116aa622007-08-15 14:28:22 +000050
Benjamin Petersone41251e2008-04-25 01:59:09 +000051 Return the entire message flattened as a string. When optional *unixfrom*
R David Murraybb17d2b2013-08-09 16:15:28 -040052 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 Brandl116aa622007-08-15 14:28:22 +000064
Benjamin Petersone41251e2008-04-25 01:59:09 +000065 Note that this method is provided as a convenience and may not always
R David Murray7dedcb42011-03-15 14:01:18 -040066 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 Storchakae0f0cf42013-08-19 09:59:18 +030069 :class:`~email.generator.Generator` instance and use its
70 :meth:`~email.generator.Generator.flatten` method directly. For example::
Georg Brandl116aa622007-08-15 14:28:22 +000071
Georg Brandl03124942008-06-10 15:50:56 +000072 from io import StringIO
Benjamin Petersone41251e2008-04-25 01:59:09 +000073 from email.generator import Generator
74 fp = StringIO()
R David Murray7dedcb42011-03-15 14:01:18 -040075 g = Generator(fp, mangle_from_=True, maxheaderlen=60)
Benjamin Petersone41251e2008-04-25 01:59:09 +000076 g.flatten(msg)
77 text = fp.getvalue()
Georg Brandl116aa622007-08-15 14:28:22 +000078
R David Murraybb17d2b2013-08-09 16:15:28 -040079 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 Brandl116aa622007-08-15 14:28:22 +000086
Benjamin Petersone41251e2008-04-25 01:59:09 +000087 .. method:: __str__()
Georg Brandl116aa622007-08-15 14:28:22 +000088
R David Murraybb17d2b2013-08-09 16:15:28 -040089 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 Storchaka319f3a12013-08-19 10:03:11 +0300112 :meth:`~email.generator.BytesGenerator.flatten` method directly.
113 For example::
R David Murraybb17d2b2013-08-09 16:15:28 -0400114
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 Brandl116aa622007-08-15 14:28:22 +0000131
132
Benjamin Petersone41251e2008-04-25 01:59:09 +0000133 .. method:: is_multipart()
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Benjamin Petersone41251e2008-04-25 01:59:09 +0000135 Return ``True`` if the message's payload is a list of sub-\
136 :class:`Message` objects, otherwise return ``False``. When
R David Murray9cc5fd72014-09-27 15:37:40 -0400137 :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 Brandl116aa622007-08-15 14:28:22 +0000142
143
Benjamin Petersone41251e2008-04-25 01:59:09 +0000144 .. method:: set_unixfrom(unixfrom)
Georg Brandl116aa622007-08-15 14:28:22 +0000145
Benjamin Petersone41251e2008-04-25 01:59:09 +0000146 Set the message's envelope header to *unixfrom*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148
Benjamin Petersone41251e2008-04-25 01:59:09 +0000149 .. method:: get_unixfrom()
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Benjamin Petersone41251e2008-04-25 01:59:09 +0000151 Return the message's envelope header. Defaults to ``None`` if the
152 envelope header was never set.
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154
Benjamin Petersone41251e2008-04-25 01:59:09 +0000155 .. method:: attach(payload)
Georg Brandl116aa622007-08-15 14:28:22 +0000156
Benjamin Petersone41251e2008-04-25 01:59:09 +0000157 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 Brandl116aa622007-08-15 14:28:22 +0000162
163
Georg Brandl3f076d82009-05-17 11:28:33 +0000164 .. method:: get_payload(i=None, decode=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000165
Benjamin Petersond6313712008-07-31 16:23:04 +0000166 Return the current payload, which will be a list of
Benjamin Petersone41251e2008-04-25 01:59:09 +0000167 :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 Brandl116aa622007-08-15 14:28:22 +0000170
Benjamin Petersone41251e2008-04-25 01:59:09 +0000171 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 Brandl116aa622007-08-15 14:28:22 +0000177
Benjamin Petersone41251e2008-04-25 01:59:09 +0000178 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 Murray80e0aee2012-05-27 21:23:34 -0400183 header is missing, the payload is
R. David Murray96fd54e2010-10-08 15:55:28 +0000184 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 Murray80e0aee2012-05-27 21:23:34 -0400186 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 Murray96fd54e2010-10-08 15:55:28 +0000191
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 Kumaran82270452010-10-15 13:29:33 +0000195 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 Brandl116aa622007-08-15 14:28:22 +0000200
201
Georg Brandl3f076d82009-05-17 11:28:33 +0000202 .. method:: set_payload(payload, charset=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000203
Benjamin Petersone41251e2008-04-25 01:59:09 +0000204 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 Murray27e9de62014-02-07 12:40:37 -0500206 the message's default character set; see :meth:`set_charset` for details.
Georg Brandl116aa622007-08-15 14:28:22 +0000207
Benjamin Petersone41251e2008-04-25 01:59:09 +0000208 .. method:: set_charset(charset)
Georg Brandl116aa622007-08-15 14:28:22 +0000209
Benjamin Petersone41251e2008-04-25 01:59:09 +0000210 Set the character set of the payload to *charset*, which can either be a
Georg Brandl3638e482009-04-27 16:46:17 +0000211 :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 Murraye3d09ff2011-03-15 17:41:13 -0400215 :mailheader:`Content-Type` header (the message will not be otherwise
216 modified). Anything else will generate a :exc:`TypeError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000217
R David Murraye3d09ff2011-03-15 17:41:13 -0400218 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 Brandl116aa622007-08-15 14:28:22 +0000231
Benjamin Petersone41251e2008-04-25 01:59:09 +0000232 .. method:: get_charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000233
Georg Brandl3638e482009-04-27 16:46:17 +0000234 Return the :class:`~email.charset.Charset` instance associated with the
235 message's payload.
Georg Brandl116aa622007-08-15 14:28:22 +0000236
Benjamin Petersone41251e2008-04-25 01:59:09 +0000237 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 Brandl116aa622007-08-15 14:28:22 +0000246
Benjamin Petersone41251e2008-04-25 01:59:09 +0000247 These semantic differences are intentional and are biased toward maximal
248 convenience.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
Benjamin Petersone41251e2008-04-25 01:59:09 +0000250 Note that in all cases, any envelope header present in the message is not
251 included in the mapping interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000252
R. David Murray92532142011-01-07 23:25:30 +0000253 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 Murray96fd54e2010-10-08 15:55:28 +0000257
Georg Brandl116aa622007-08-15 14:28:22 +0000258
Benjamin Petersone41251e2008-04-25 01:59:09 +0000259 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000260
Benjamin Petersone41251e2008-04-25 01:59:09 +0000261 Return the total number of headers, including duplicates.
Georg Brandl116aa622007-08-15 14:28:22 +0000262
263
Benjamin Petersone41251e2008-04-25 01:59:09 +0000264 .. method:: __contains__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000265
Benjamin Petersone41251e2008-04-25 01:59:09 +0000266 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 Brandl116aa622007-08-15 14:28:22 +0000269
Benjamin Petersone41251e2008-04-25 01:59:09 +0000270 if 'message-id' in myMessage:
271 print('Message-ID:', myMessage['message-id'])
Georg Brandl116aa622007-08-15 14:28:22 +0000272
273
Benjamin Petersone41251e2008-04-25 01:59:09 +0000274 .. method:: __getitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000275
Benjamin Petersone41251e2008-04-25 01:59:09 +0000276 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 Brandl116aa622007-08-15 14:28:22 +0000279
Benjamin Petersone41251e2008-04-25 01:59:09 +0000280 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 Brandl116aa622007-08-15 14:28:22 +0000284
285
Benjamin Petersone41251e2008-04-25 01:59:09 +0000286 .. method:: __setitem__(name, val)
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Benjamin Petersone41251e2008-04-25 01:59:09 +0000288 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 Brandl116aa622007-08-15 14:28:22 +0000290
Benjamin Petersone41251e2008-04-25 01:59:09 +0000291 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 Brandl116aa622007-08-15 14:28:22 +0000294
Benjamin Petersone41251e2008-04-25 01:59:09 +0000295 del msg['subject']
296 msg['subject'] = 'Python roolz!'
Georg Brandl116aa622007-08-15 14:28:22 +0000297
298
Benjamin Petersone41251e2008-04-25 01:59:09 +0000299 .. method:: __delitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000300
Benjamin Petersone41251e2008-04-25 01:59:09 +0000301 Delete all occurrences of the field with name *name* from the message's
Georg Brandl3f076d82009-05-17 11:28:33 +0000302 headers. No exception is raised if the named field isn't present in the
303 headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000304
305
Benjamin Petersone41251e2008-04-25 01:59:09 +0000306 .. method:: keys()
Georg Brandl116aa622007-08-15 14:28:22 +0000307
Benjamin Petersone41251e2008-04-25 01:59:09 +0000308 Return a list of all the message's header field names.
Georg Brandl116aa622007-08-15 14:28:22 +0000309
310
Benjamin Petersone41251e2008-04-25 01:59:09 +0000311 .. method:: values()
Georg Brandl116aa622007-08-15 14:28:22 +0000312
Benjamin Petersone41251e2008-04-25 01:59:09 +0000313 Return a list of all the message's field values.
Georg Brandl116aa622007-08-15 14:28:22 +0000314
315
Benjamin Petersone41251e2008-04-25 01:59:09 +0000316 .. method:: items()
Georg Brandl116aa622007-08-15 14:28:22 +0000317
Benjamin Petersone41251e2008-04-25 01:59:09 +0000318 Return a list of 2-tuples containing all the message's field headers and
319 values.
Georg Brandl116aa622007-08-15 14:28:22 +0000320
321
Georg Brandl3f076d82009-05-17 11:28:33 +0000322 .. method:: get(name, failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000323
Benjamin Petersone41251e2008-04-25 01:59:09 +0000324 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 Brandl116aa622007-08-15 14:28:22 +0000327
Benjamin Petersone41251e2008-04-25 01:59:09 +0000328 Here are some additional useful methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000329
330
Georg Brandl3f076d82009-05-17 11:28:33 +0000331 .. method:: get_all(name, failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000332
Benjamin Petersone41251e2008-04-25 01:59:09 +0000333 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 Brandl116aa622007-08-15 14:28:22 +0000336
337
Benjamin Petersone41251e2008-04-25 01:59:09 +0000338 .. method:: add_header(_name, _value, **_params)
Georg Brandl116aa622007-08-15 14:28:22 +0000339
Benjamin Petersone41251e2008-04-25 01:59:09 +0000340 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 Brandl116aa622007-08-15 14:28:22 +0000344
Benjamin Petersone41251e2008-04-25 01:59:09 +0000345 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 Murray7ec754b2010-12-13 23:51:19 +0000349 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 Brandlc8843502010-12-19 10:28:46 +0000353 to ``None`` or the empty string (see :rfc:`2231` for other possibilities),
R. David Murray7ec754b2010-12-13 23:51:19 +0000354 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 Brandlc8843502010-12-19 10:28:46 +0000356 it is automatically encoded in :rfc:`2231` format using a ``CHARSET``
R. David Murray7ec754b2010-12-13 23:51:19 +0000357 of ``utf-8`` and a ``LANGUAGE`` of ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000358
Benjamin Petersone41251e2008-04-25 01:59:09 +0000359 Here's an example::
Georg Brandl116aa622007-08-15 14:28:22 +0000360
Benjamin Petersone41251e2008-04-25 01:59:09 +0000361 msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
Georg Brandl116aa622007-08-15 14:28:22 +0000362
Benjamin Petersone41251e2008-04-25 01:59:09 +0000363 This will add a header that looks like ::
Georg Brandl116aa622007-08-15 14:28:22 +0000364
Benjamin Petersone41251e2008-04-25 01:59:09 +0000365 Content-Disposition: attachment; filename="bud.gif"
Georg Brandl116aa622007-08-15 14:28:22 +0000366
Ezio Melottie130a522011-10-19 10:58:56 +0300367 An example with non-ASCII characters::
R. David Murray7ec754b2010-12-13 23:51:19 +0000368
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 Brandl116aa622007-08-15 14:28:22 +0000376
Benjamin Petersone41251e2008-04-25 01:59:09 +0000377 .. method:: replace_header(_name, _value)
Georg Brandl116aa622007-08-15 14:28:22 +0000378
Benjamin Petersone41251e2008-04-25 01:59:09 +0000379 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 Brandl116aa622007-08-15 14:28:22 +0000382
Georg Brandl116aa622007-08-15 14:28:22 +0000383
Benjamin Petersone41251e2008-04-25 01:59:09 +0000384 .. method:: get_content_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000385
Benjamin Petersone41251e2008-04-25 01:59:09 +0000386 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 Brandl116aa622007-08-15 14:28:22 +0000392
Benjamin Petersone41251e2008-04-25 01:59:09 +0000393 :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 Brandl116aa622007-08-15 14:28:22 +0000398
Georg Brandl116aa622007-08-15 14:28:22 +0000399
Benjamin Petersone41251e2008-04-25 01:59:09 +0000400 .. method:: get_content_maintype()
Georg Brandl116aa622007-08-15 14:28:22 +0000401
Benjamin Petersone41251e2008-04-25 01:59:09 +0000402 Return the message's main content type. This is the :mimetype:`maintype`
403 part of the string returned by :meth:`get_content_type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000404
Georg Brandl116aa622007-08-15 14:28:22 +0000405
Benjamin Petersone41251e2008-04-25 01:59:09 +0000406 .. method:: get_content_subtype()
Georg Brandl116aa622007-08-15 14:28:22 +0000407
Benjamin Petersone41251e2008-04-25 01:59:09 +0000408 Return the message's sub-content type. This is the :mimetype:`subtype`
409 part of the string returned by :meth:`get_content_type`.
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:: get_default_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000413
Benjamin Petersone41251e2008-04-25 01:59:09 +0000414 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 Brandl116aa622007-08-15 14:28:22 +0000418
Georg Brandl116aa622007-08-15 14:28:22 +0000419
Benjamin Petersone41251e2008-04-25 01:59:09 +0000420 .. method:: set_default_type(ctype)
Georg Brandl116aa622007-08-15 14:28:22 +0000421
Benjamin Petersone41251e2008-04-25 01:59:09 +0000422 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 Brandl116aa622007-08-15 14:28:22 +0000426
Georg Brandl116aa622007-08-15 14:28:22 +0000427
Georg Brandl3f076d82009-05-17 11:28:33 +0000428 .. method:: get_params(failobj=None, header='content-type', unquote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000429
Benjamin Petersone41251e2008-04-25 01:59:09 +0000430 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 Brandl116aa622007-08-15 14:28:22 +0000437
Benjamin Petersone41251e2008-04-25 01:59:09 +0000438 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 Brandl116aa622007-08-15 14:28:22 +0000441
Georg Brandl116aa622007-08-15 14:28:22 +0000442
Georg Brandl3f076d82009-05-17 11:28:33 +0000443 .. method:: get_param(param, failobj=None, header='content-type', unquote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000444
Benjamin Petersone41251e2008-04-25 01:59:09 +0000445 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 Brandl116aa622007-08-15 14:28:22 +0000449
Benjamin Petersone41251e2008-04-25 01:59:09 +0000450 Optional *header* if given, specifies the message header to use instead of
451 :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000452
Benjamin Petersone41251e2008-04-25 01:59:09 +0000453 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 Brandl116aa622007-08-15 14:28:22 +0000460
Benjamin Petersone41251e2008-04-25 01:59:09 +0000461 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 Brandl540b45c2009-04-27 16:45:26 +0000463 :func:`email.utils.collapse_rfc2231_value`, passing in the return value
Benjamin Petersone41251e2008-04-25 01:59:09 +0000464 from :meth:`get_param`. This will return a suitably decoded Unicode
R. David Murray7ec754b2010-12-13 23:51:19 +0000465 string when the value is a tuple, or the original string unquoted if it
Benjamin Petersone41251e2008-04-25 01:59:09 +0000466 isn't. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000467
Benjamin Petersone41251e2008-04-25 01:59:09 +0000468 rawparam = msg.get_param('foo')
Georg Brandl540b45c2009-04-27 16:45:26 +0000469 param = email.utils.collapse_rfc2231_value(rawparam)
Georg Brandl116aa622007-08-15 14:28:22 +0000470
Benjamin Petersone41251e2008-04-25 01:59:09 +0000471 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 Brandl116aa622007-08-15 14:28:22 +0000474
Georg Brandl116aa622007-08-15 14:28:22 +0000475
Larry Hastings3732ed22014-03-15 21:13:56 -0700476 .. method:: set_param(param, value, header='Content-Type', requote=True, \
R David Murray3da240f2013-10-16 22:48:40 -0400477 charset=None, language='', replace=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000478
Benjamin Petersone41251e2008-04-25 01:59:09 +0000479 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 Brandl116aa622007-08-15 14:28:22 +0000484
Benjamin Petersone41251e2008-04-25 01:59:09 +0000485 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 Brandl116aa622007-08-15 14:28:22 +0000488
Benjamin Petersone41251e2008-04-25 01:59:09 +0000489 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 Brandl116aa622007-08-15 14:28:22 +0000493
R David Murray3da240f2013-10-16 22:48:40 -0400494 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 Hastings3732ed22014-03-15 21:13:56 -0700498 .. versionchanged:: 3.4 ``replace`` keyword was added.
R David Murray3da240f2013-10-16 22:48:40 -0400499
Georg Brandl116aa622007-08-15 14:28:22 +0000500
Georg Brandl3f076d82009-05-17 11:28:33 +0000501 .. method:: del_param(param, header='content-type', requote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000502
Benjamin Petersone41251e2008-04-25 01:59:09 +0000503 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 Brandl116aa622007-08-15 14:28:22 +0000508
Georg Brandl116aa622007-08-15 14:28:22 +0000509
Georg Brandl3f076d82009-05-17 11:28:33 +0000510 .. method:: set_type(type, header='Content-Type', requote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000511
Benjamin Petersone41251e2008-04-25 01:59:09 +0000512 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 Brandl116aa622007-08-15 14:28:22 +0000515
Benjamin Petersone41251e2008-04-25 01:59:09 +0000516 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 Brandl116aa622007-08-15 14:28:22 +0000520
Benjamin Petersone41251e2008-04-25 01:59:09 +0000521 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 Brandl116aa622007-08-15 14:28:22 +0000524
Georg Brandl116aa622007-08-15 14:28:22 +0000525
Georg Brandl3f076d82009-05-17 11:28:33 +0000526 .. method:: get_filename(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000527
Benjamin Petersone41251e2008-04-25 01:59:09 +0000528 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 Murray9ed34be2010-03-04 17:38:18 +0000531 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 Brandl116aa622007-08-15 14:28:22 +0000535
536
Georg Brandl3f076d82009-05-17 11:28:33 +0000537 .. method:: get_boundary(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000538
Benjamin Petersone41251e2008-04-25 01:59:09 +0000539 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 Brandl540b45c2009-04-27 16:45:26 +0000542 string will always be unquoted as per :func:`email.utils.unquote`.
Georg Brandl116aa622007-08-15 14:28:22 +0000543
544
Benjamin Petersone41251e2008-04-25 01:59:09 +0000545 .. method:: set_boundary(boundary)
Georg Brandl116aa622007-08-15 14:28:22 +0000546
Benjamin Petersone41251e2008-04-25 01:59:09 +0000547 Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
548 *boundary*. :meth:`set_boundary` will always quote *boundary* if
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300549 necessary. A :exc:`~email.errors.HeaderParseError` is raised if the
550 message object has no :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000551
Benjamin Petersone41251e2008-04-25 01:59:09 +0000552 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 Brandl116aa622007-08-15 14:28:22 +0000558
559
Georg Brandl3f076d82009-05-17 11:28:33 +0000560 .. method:: get_content_charset(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000561
Benjamin Petersone41251e2008-04-25 01:59:09 +0000562 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 Brandl116aa622007-08-15 14:28:22 +0000565
Benjamin Petersone41251e2008-04-25 01:59:09 +0000566 Note that this method differs from :meth:`get_charset` which returns the
Georg Brandl3638e482009-04-27 16:46:17 +0000567 :class:`~email.charset.Charset` instance for the default encoding of the message body.
Georg Brandl116aa622007-08-15 14:28:22 +0000568
Georg Brandl116aa622007-08-15 14:28:22 +0000569
Georg Brandl3f076d82009-05-17 11:28:33 +0000570 .. method:: get_charsets(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000571
Benjamin Petersone41251e2008-04-25 01:59:09 +0000572 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 Brandl116aa622007-08-15 14:28:22 +0000575
Benjamin Petersone41251e2008-04-25 01:59:09 +0000576 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 Brandl116aa622007-08-15 14:28:22 +0000582
583
R David Murrayb744f3a2015-05-16 15:41:07 -0400584 .. 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 Petersone41251e2008-04-25 01:59:09 +0000593 .. method:: walk()
Georg Brandl116aa622007-08-15 14:28:22 +0000594
Benjamin Petersone41251e2008-04-25 01:59:09 +0000595 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 Brandl116aa622007-08-15 14:28:22 +0000599
Benjamin Petersone41251e2008-04-25 01:59:09 +0000600 Here's an example that prints the MIME type of every part of a multipart
R David Murrayfdfb0052013-07-29 15:49:58 -0400601 message structure:
Georg Brandl116aa622007-08-15 14:28:22 +0000602
R David Murray9cc5fd72014-09-27 15:37:40 -0400603 .. testsetup::
R David Murrayfdfb0052013-07-29 15:49:58 -0400604
R David Murray9cc5fd72014-09-27 15:37:40 -0400605 >>> 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 Murrayfdfb0052013-07-29 15:49:58 -0400609
R David Murray9cc5fd72014-09-27 15:37:40 -0400610 .. doctest::
R David Murrayfdfb0052013-07-29 15:49:58 -0400611
R David Murray9cc5fd72014-09-27 15:37:40 -0400612 >>> 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 Brandl116aa622007-08-15 14:28:22 +0000653
Benjamin Petersone41251e2008-04-25 01:59:09 +0000654 :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 Brandl116aa622007-08-15 14:28:22 +0000656
657
Benjamin Petersone41251e2008-04-25 01:59:09 +0000658 .. attribute:: preamble
Georg Brandl116aa622007-08-15 14:28:22 +0000659
Benjamin Petersone41251e2008-04-25 01:59:09 +0000660 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 Brandl116aa622007-08-15 14:28:22 +0000666
Benjamin Petersone41251e2008-04-25 01:59:09 +0000667 The *preamble* attribute contains this leading extra-armor text for MIME
Georg Brandl3638e482009-04-27 16:46:17 +0000668 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 Petersone41251e2008-04-25 01:59:09 +0000673 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 Brandl116aa622007-08-15 14:28:22 +0000676
Benjamin Petersone41251e2008-04-25 01:59:09 +0000677 Note that if the message object has no preamble, the *preamble* attribute
678 will be ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000679
680
Benjamin Petersone41251e2008-04-25 01:59:09 +0000681 .. attribute:: epilogue
Georg Brandl116aa622007-08-15 14:28:22 +0000682
Benjamin Petersone41251e2008-04-25 01:59:09 +0000683 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 Brandl116aa622007-08-15 14:28:22 +0000686
Benjamin Petersone41251e2008-04-25 01:59:09 +0000687 You do not need to set the epilogue to the empty string in order for the
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300688 :class:`~email.generator.Generator` to print a newline at the end of the
689 file.
Georg Brandl116aa622007-08-15 14:28:22 +0000690
691
Benjamin Petersone41251e2008-04-25 01:59:09 +0000692 .. attribute:: defects
Georg Brandl116aa622007-08-15 14:28:22 +0000693
Benjamin Petersone41251e2008-04-25 01:59:09 +0000694 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.