blob: b91f26d12045eb4a25a79f8139290052695de5cc [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`
R David Murraya83ade12014-05-08 10:05:47 -040037 class) use the rules it specifies to update 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
R David Murray9cc5fd72014-09-27 15:37:40 -0400134 :meth:`is_multipart` returns ``False``, the payload should be a string
135 object. (Note that :meth:`is_multipart` returning ``True`` does not
136 necessarily mean that "msg.get_content_maintype() == 'multipart'" will
137 return the ``True``. For example, ``is_multipart`` will return ``True``
138 when the :class:`Message` is of type ``message/rfc822``.)
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140
Benjamin Petersone41251e2008-04-25 01:59:09 +0000141 .. method:: set_unixfrom(unixfrom)
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Benjamin Petersone41251e2008-04-25 01:59:09 +0000143 Set the message's envelope header to *unixfrom*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145
Benjamin Petersone41251e2008-04-25 01:59:09 +0000146 .. method:: get_unixfrom()
Georg Brandl116aa622007-08-15 14:28:22 +0000147
Benjamin Petersone41251e2008-04-25 01:59:09 +0000148 Return the message's envelope header. Defaults to ``None`` if the
149 envelope header was never set.
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151
Benjamin Petersone41251e2008-04-25 01:59:09 +0000152 .. method:: attach(payload)
Georg Brandl116aa622007-08-15 14:28:22 +0000153
Benjamin Petersone41251e2008-04-25 01:59:09 +0000154 Add the given *payload* to the current payload, which must be ``None`` or
155 a list of :class:`Message` objects before the call. After the call, the
156 payload will always be a list of :class:`Message` objects. If you want to
157 set the payload to a scalar object (e.g. a string), use
158 :meth:`set_payload` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000159
160
Georg Brandl3f076d82009-05-17 11:28:33 +0000161 .. method:: get_payload(i=None, decode=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Benjamin Petersond6313712008-07-31 16:23:04 +0000163 Return the current payload, which will be a list of
Benjamin Petersone41251e2008-04-25 01:59:09 +0000164 :class:`Message` objects when :meth:`is_multipart` is ``True``, or a
165 string when :meth:`is_multipart` is ``False``. If the payload is a list
166 and you mutate the list object, you modify the message's payload in place.
Georg Brandl116aa622007-08-15 14:28:22 +0000167
Benjamin Petersone41251e2008-04-25 01:59:09 +0000168 With optional argument *i*, :meth:`get_payload` will return the *i*-th
169 element of the payload, counting from zero, if :meth:`is_multipart` is
170 ``True``. An :exc:`IndexError` will be raised if *i* is less than 0 or
171 greater than or equal to the number of items in the payload. If the
172 payload is a string (i.e. :meth:`is_multipart` is ``False``) and *i* is
173 given, a :exc:`TypeError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Benjamin Petersone41251e2008-04-25 01:59:09 +0000175 Optional *decode* is a flag indicating whether the payload should be
176 decoded or not, according to the :mailheader:`Content-Transfer-Encoding`
177 header. When ``True`` and the message is not a multipart, the payload will
178 be decoded if this header's value is ``quoted-printable`` or ``base64``.
179 If some other encoding is used, or :mailheader:`Content-Transfer-Encoding`
R David Murray80e0aee2012-05-27 21:23:34 -0400180 header is missing, the payload is
R. David Murray96fd54e2010-10-08 15:55:28 +0000181 returned as-is (undecoded). In all cases the returned value is binary
182 data. If the message is a multipart and the *decode* flag is ``True``,
R David Murray80e0aee2012-05-27 21:23:34 -0400183 then ``None`` is returned. If the payload is base64 and it was not
184 perfectly formed (missing padding, characters outside the base64
185 alphabet), then an appropriate defect will be added to the message's
186 defect property (:class:`~email.errors.InvalidBase64PaddingDefect` or
187 :class:`~email.errors.InvalidBase64CharactersDefect`, respectively).
R. David Murray96fd54e2010-10-08 15:55:28 +0000188
189 When *decode* is ``False`` (the default) the body is returned as a string
190 without decoding the :mailheader:`Content-Transfer-Encoding`. However,
191 for a :mailheader:`Content-Transfer-Encoding` of 8bit, an attempt is made
Senthil Kumaran82270452010-10-15 13:29:33 +0000192 to decode the original bytes using the ``charset`` specified by the
193 :mailheader:`Content-Type` header, using the ``replace`` error handler.
194 If no ``charset`` is specified, or if the ``charset`` given is not
195 recognized by the email package, the body is decoded using the default
196 ASCII charset.
Georg Brandl116aa622007-08-15 14:28:22 +0000197
198
Georg Brandl3f076d82009-05-17 11:28:33 +0000199 .. method:: set_payload(payload, charset=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000200
Benjamin Petersone41251e2008-04-25 01:59:09 +0000201 Set the entire message object's payload to *payload*. It is the client's
202 responsibility to ensure the payload invariants. Optional *charset* sets
R David Murray27e9de62014-02-07 12:40:37 -0500203 the message's default character set; see :meth:`set_charset` for details.
Georg Brandl116aa622007-08-15 14:28:22 +0000204
Benjamin Petersone41251e2008-04-25 01:59:09 +0000205 .. method:: set_charset(charset)
Georg Brandl116aa622007-08-15 14:28:22 +0000206
Benjamin Petersone41251e2008-04-25 01:59:09 +0000207 Set the character set of the payload to *charset*, which can either be a
Georg Brandl3638e482009-04-27 16:46:17 +0000208 :class:`~email.charset.Charset` instance (see :mod:`email.charset`), a
209 string naming a character set, or ``None``. If it is a string, it will
210 be converted to a :class:`~email.charset.Charset` instance. If *charset*
211 is ``None``, the ``charset`` parameter will be removed from the
R David Murraye3d09ff2011-03-15 17:41:13 -0400212 :mailheader:`Content-Type` header (the message will not be otherwise
213 modified). Anything else will generate a :exc:`TypeError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000214
R David Murraye3d09ff2011-03-15 17:41:13 -0400215 If there is no existing :mailheader:`MIME-Version` header one will be
216 added. If there is no existing :mailheader:`Content-Type` header, one
217 will be added with a value of :mimetype:`text/plain`. Whether the
218 :mailheader:`Content-Type` header already exists or not, its ``charset``
219 parameter will be set to *charset.output_charset*. If
220 *charset.input_charset* and *charset.output_charset* differ, the payload
221 will be re-encoded to the *output_charset*. If there is no existing
222 :mailheader:`Content-Transfer-Encoding` header, then the payload will be
223 transfer-encoded, if needed, using the specified
224 :class:`~email.charset.Charset`, and a header with the appropriate value
225 will be added. If a :mailheader:`Content-Transfer-Encoding` header
226 already exists, the payload is assumed to already be correctly encoded
227 using that :mailheader:`Content-Transfer-Encoding` and is not modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000228
Benjamin Petersone41251e2008-04-25 01:59:09 +0000229 .. method:: get_charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000230
Georg Brandl3638e482009-04-27 16:46:17 +0000231 Return the :class:`~email.charset.Charset` instance associated with the
232 message's payload.
Georg Brandl116aa622007-08-15 14:28:22 +0000233
Benjamin Petersone41251e2008-04-25 01:59:09 +0000234 The following methods implement a mapping-like interface for accessing the
235 message's :rfc:`2822` headers. Note that there are some semantic differences
236 between these methods and a normal mapping (i.e. dictionary) interface. For
237 example, in a dictionary there are no duplicate keys, but here there may be
238 duplicate message headers. Also, in dictionaries there is no guaranteed
239 order to the keys returned by :meth:`keys`, but in a :class:`Message` object,
240 headers are always returned in the order they appeared in the original
241 message, or were added to the message later. Any header deleted and then
242 re-added are always appended to the end of the header list.
Georg Brandl116aa622007-08-15 14:28:22 +0000243
Benjamin Petersone41251e2008-04-25 01:59:09 +0000244 These semantic differences are intentional and are biased toward maximal
245 convenience.
Georg Brandl116aa622007-08-15 14:28:22 +0000246
Benjamin Petersone41251e2008-04-25 01:59:09 +0000247 Note that in all cases, any envelope header present in the message is not
248 included in the mapping interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
R. David Murray92532142011-01-07 23:25:30 +0000250 In a model generated from bytes, any header values that (in contravention of
251 the RFCs) contain non-ASCII bytes will, when retrieved through this
252 interface, be represented as :class:`~email.header.Header` objects with
253 a charset of `unknown-8bit`.
R. David Murray96fd54e2010-10-08 15:55:28 +0000254
Georg Brandl116aa622007-08-15 14:28:22 +0000255
Benjamin Petersone41251e2008-04-25 01:59:09 +0000256 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000257
Benjamin Petersone41251e2008-04-25 01:59:09 +0000258 Return the total number of headers, including duplicates.
Georg Brandl116aa622007-08-15 14:28:22 +0000259
260
Benjamin Petersone41251e2008-04-25 01:59:09 +0000261 .. method:: __contains__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000262
Benjamin Petersone41251e2008-04-25 01:59:09 +0000263 Return true if the message object has a field named *name*. Matching is
264 done case-insensitively and *name* should not include the trailing colon.
265 Used for the ``in`` operator, e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000266
Benjamin Petersone41251e2008-04-25 01:59:09 +0000267 if 'message-id' in myMessage:
268 print('Message-ID:', myMessage['message-id'])
Georg Brandl116aa622007-08-15 14:28:22 +0000269
270
Benjamin Petersone41251e2008-04-25 01:59:09 +0000271 .. method:: __getitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000272
Benjamin Petersone41251e2008-04-25 01:59:09 +0000273 Return the value of the named header field. *name* should not include the
274 colon field separator. If the header is missing, ``None`` is returned; a
275 :exc:`KeyError` is never raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000276
Benjamin Petersone41251e2008-04-25 01:59:09 +0000277 Note that if the named field appears more than once in the message's
278 headers, exactly which of those field values will be returned is
279 undefined. Use the :meth:`get_all` method to get the values of all the
280 extant named headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000281
282
Benjamin Petersone41251e2008-04-25 01:59:09 +0000283 .. method:: __setitem__(name, val)
Georg Brandl116aa622007-08-15 14:28:22 +0000284
Benjamin Petersone41251e2008-04-25 01:59:09 +0000285 Add a header to the message with field name *name* and value *val*. The
286 field is appended to the end of the message's existing fields.
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Benjamin Petersone41251e2008-04-25 01:59:09 +0000288 Note that this does *not* overwrite or delete any existing header with the same
289 name. If you want to ensure that the new header is the only one present in the
290 message with field name *name*, delete the field first, e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000291
Benjamin Petersone41251e2008-04-25 01:59:09 +0000292 del msg['subject']
293 msg['subject'] = 'Python roolz!'
Georg Brandl116aa622007-08-15 14:28:22 +0000294
295
Benjamin Petersone41251e2008-04-25 01:59:09 +0000296 .. method:: __delitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000297
Benjamin Petersone41251e2008-04-25 01:59:09 +0000298 Delete all occurrences of the field with name *name* from the message's
Georg Brandl3f076d82009-05-17 11:28:33 +0000299 headers. No exception is raised if the named field isn't present in the
300 headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000301
302
Benjamin Petersone41251e2008-04-25 01:59:09 +0000303 .. method:: keys()
Georg Brandl116aa622007-08-15 14:28:22 +0000304
Benjamin Petersone41251e2008-04-25 01:59:09 +0000305 Return a list of all the message's header field names.
Georg Brandl116aa622007-08-15 14:28:22 +0000306
307
Benjamin Petersone41251e2008-04-25 01:59:09 +0000308 .. method:: values()
Georg Brandl116aa622007-08-15 14:28:22 +0000309
Benjamin Petersone41251e2008-04-25 01:59:09 +0000310 Return a list of all the message's field values.
Georg Brandl116aa622007-08-15 14:28:22 +0000311
312
Benjamin Petersone41251e2008-04-25 01:59:09 +0000313 .. method:: items()
Georg Brandl116aa622007-08-15 14:28:22 +0000314
Benjamin Petersone41251e2008-04-25 01:59:09 +0000315 Return a list of 2-tuples containing all the message's field headers and
316 values.
Georg Brandl116aa622007-08-15 14:28:22 +0000317
318
Georg Brandl3f076d82009-05-17 11:28:33 +0000319 .. method:: get(name, failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000320
Benjamin Petersone41251e2008-04-25 01:59:09 +0000321 Return the value of the named header field. This is identical to
322 :meth:`__getitem__` except that optional *failobj* is returned if the
323 named header is missing (defaults to ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000324
Benjamin Petersone41251e2008-04-25 01:59:09 +0000325 Here are some additional useful methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000326
327
Georg Brandl3f076d82009-05-17 11:28:33 +0000328 .. method:: get_all(name, failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000329
Benjamin Petersone41251e2008-04-25 01:59:09 +0000330 Return a list of all the values for the field named *name*. If there are
331 no such named headers in the message, *failobj* is returned (defaults to
332 ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000333
334
Benjamin Petersone41251e2008-04-25 01:59:09 +0000335 .. method:: add_header(_name, _value, **_params)
Georg Brandl116aa622007-08-15 14:28:22 +0000336
Benjamin Petersone41251e2008-04-25 01:59:09 +0000337 Extended header setting. This method is similar to :meth:`__setitem__`
338 except that additional header parameters can be provided as keyword
339 arguments. *_name* is the header field to add and *_value* is the
340 *primary* value for the header.
Georg Brandl116aa622007-08-15 14:28:22 +0000341
Benjamin Petersone41251e2008-04-25 01:59:09 +0000342 For each item in the keyword argument dictionary *_params*, the key is
343 taken as the parameter name, with underscores converted to dashes (since
344 dashes are illegal in Python identifiers). Normally, the parameter will
345 be added as ``key="value"`` unless the value is ``None``, in which case
R. David Murray7ec754b2010-12-13 23:51:19 +0000346 only the key will be added. If the value contains non-ASCII characters,
347 it can be specified as a three tuple in the format
348 ``(CHARSET, LANGUAGE, VALUE)``, where ``CHARSET`` is a string naming the
349 charset to be used to encode the value, ``LANGUAGE`` can usually be set
Georg Brandlc8843502010-12-19 10:28:46 +0000350 to ``None`` or the empty string (see :rfc:`2231` for other possibilities),
R. David Murray7ec754b2010-12-13 23:51:19 +0000351 and ``VALUE`` is the string value containing non-ASCII code points. If
352 a three tuple is not passed and the value contains non-ASCII characters,
Georg Brandlc8843502010-12-19 10:28:46 +0000353 it is automatically encoded in :rfc:`2231` format using a ``CHARSET``
R. David Murray7ec754b2010-12-13 23:51:19 +0000354 of ``utf-8`` and a ``LANGUAGE`` of ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000355
Benjamin Petersone41251e2008-04-25 01:59:09 +0000356 Here's an example::
Georg Brandl116aa622007-08-15 14:28:22 +0000357
Benjamin Petersone41251e2008-04-25 01:59:09 +0000358 msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
Georg Brandl116aa622007-08-15 14:28:22 +0000359
Benjamin Petersone41251e2008-04-25 01:59:09 +0000360 This will add a header that looks like ::
Georg Brandl116aa622007-08-15 14:28:22 +0000361
Benjamin Petersone41251e2008-04-25 01:59:09 +0000362 Content-Disposition: attachment; filename="bud.gif"
Georg Brandl116aa622007-08-15 14:28:22 +0000363
Ezio Melottie130a522011-10-19 10:58:56 +0300364 An example with non-ASCII characters::
R. David Murray7ec754b2010-12-13 23:51:19 +0000365
366 msg.add_header('Content-Disposition', 'attachment',
367 filename=('iso-8859-1', '', 'Fußballer.ppt'))
368
369 Which produces ::
370
371 Content-Disposition: attachment; filename*="iso-8859-1''Fu%DFballer.ppt"
372
Georg Brandl116aa622007-08-15 14:28:22 +0000373
Benjamin Petersone41251e2008-04-25 01:59:09 +0000374 .. method:: replace_header(_name, _value)
Georg Brandl116aa622007-08-15 14:28:22 +0000375
Benjamin Petersone41251e2008-04-25 01:59:09 +0000376 Replace a header. Replace the first header found in the message that
377 matches *_name*, retaining header order and field name case. If no
378 matching header was found, a :exc:`KeyError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000379
Georg Brandl116aa622007-08-15 14:28:22 +0000380
Benjamin Petersone41251e2008-04-25 01:59:09 +0000381 .. method:: get_content_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000382
Benjamin Petersone41251e2008-04-25 01:59:09 +0000383 Return the message's content type. The returned string is coerced to
384 lower case of the form :mimetype:`maintype/subtype`. If there was no
385 :mailheader:`Content-Type` header in the message the default type as given
386 by :meth:`get_default_type` will be returned. Since according to
387 :rfc:`2045`, messages always have a default type, :meth:`get_content_type`
388 will always return a value.
Georg Brandl116aa622007-08-15 14:28:22 +0000389
Benjamin Petersone41251e2008-04-25 01:59:09 +0000390 :rfc:`2045` defines a message's default type to be :mimetype:`text/plain`
391 unless it appears inside a :mimetype:`multipart/digest` container, in
392 which case it would be :mimetype:`message/rfc822`. If the
393 :mailheader:`Content-Type` header has an invalid type specification,
394 :rfc:`2045` mandates that the default type be :mimetype:`text/plain`.
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Georg Brandl116aa622007-08-15 14:28:22 +0000396
Benjamin Petersone41251e2008-04-25 01:59:09 +0000397 .. method:: get_content_maintype()
Georg Brandl116aa622007-08-15 14:28:22 +0000398
Benjamin Petersone41251e2008-04-25 01:59:09 +0000399 Return the message's main content type. This is the :mimetype:`maintype`
400 part of the string returned by :meth:`get_content_type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000401
Georg Brandl116aa622007-08-15 14:28:22 +0000402
Benjamin Petersone41251e2008-04-25 01:59:09 +0000403 .. method:: get_content_subtype()
Georg Brandl116aa622007-08-15 14:28:22 +0000404
Benjamin Petersone41251e2008-04-25 01:59:09 +0000405 Return the message's sub-content type. This is the :mimetype:`subtype`
406 part of the string returned by :meth:`get_content_type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000407
Georg Brandl116aa622007-08-15 14:28:22 +0000408
Benjamin Petersone41251e2008-04-25 01:59:09 +0000409 .. method:: get_default_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000410
Benjamin Petersone41251e2008-04-25 01:59:09 +0000411 Return the default content type. Most messages have a default content
412 type of :mimetype:`text/plain`, except for messages that are subparts of
413 :mimetype:`multipart/digest` containers. Such subparts have a default
414 content type of :mimetype:`message/rfc822`.
Georg Brandl116aa622007-08-15 14:28:22 +0000415
Georg Brandl116aa622007-08-15 14:28:22 +0000416
Benjamin Petersone41251e2008-04-25 01:59:09 +0000417 .. method:: set_default_type(ctype)
Georg Brandl116aa622007-08-15 14:28:22 +0000418
Benjamin Petersone41251e2008-04-25 01:59:09 +0000419 Set the default content type. *ctype* should either be
420 :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is not
421 enforced. The default content type is not stored in the
422 :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000423
Georg Brandl116aa622007-08-15 14:28:22 +0000424
Georg Brandl3f076d82009-05-17 11:28:33 +0000425 .. method:: get_params(failobj=None, header='content-type', unquote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000426
Benjamin Petersone41251e2008-04-25 01:59:09 +0000427 Return the message's :mailheader:`Content-Type` parameters, as a list.
428 The elements of the returned list are 2-tuples of key/value pairs, as
429 split on the ``'='`` sign. The left hand side of the ``'='`` is the key,
430 while the right hand side is the value. If there is no ``'='`` sign in
431 the parameter the value is the empty string, otherwise the value is as
432 described in :meth:`get_param` and is unquoted if optional *unquote* is
433 ``True`` (the default).
Georg Brandl116aa622007-08-15 14:28:22 +0000434
Benjamin Petersone41251e2008-04-25 01:59:09 +0000435 Optional *failobj* is the object to return if there is no
436 :mailheader:`Content-Type` header. Optional *header* is the header to
437 search instead of :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000438
Georg Brandl116aa622007-08-15 14:28:22 +0000439
Georg Brandl3f076d82009-05-17 11:28:33 +0000440 .. method:: get_param(param, failobj=None, header='content-type', unquote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000441
Benjamin Petersone41251e2008-04-25 01:59:09 +0000442 Return the value of the :mailheader:`Content-Type` header's parameter
443 *param* as a string. If the message has no :mailheader:`Content-Type`
444 header or if there is no such parameter, then *failobj* is returned
445 (defaults to ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000446
Benjamin Petersone41251e2008-04-25 01:59:09 +0000447 Optional *header* if given, specifies the message header to use instead of
448 :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000449
Benjamin Petersone41251e2008-04-25 01:59:09 +0000450 Parameter keys are always compared case insensitively. The return value
451 can either be a string, or a 3-tuple if the parameter was :rfc:`2231`
452 encoded. When it's a 3-tuple, the elements of the value are of the form
453 ``(CHARSET, LANGUAGE, VALUE)``. Note that both ``CHARSET`` and
454 ``LANGUAGE`` can be ``None``, in which case you should consider ``VALUE``
455 to be encoded in the ``us-ascii`` charset. You can usually ignore
456 ``LANGUAGE``.
Georg Brandl116aa622007-08-15 14:28:22 +0000457
Benjamin Petersone41251e2008-04-25 01:59:09 +0000458 If your application doesn't care whether the parameter was encoded as in
459 :rfc:`2231`, you can collapse the parameter value by calling
Georg Brandl540b45c2009-04-27 16:45:26 +0000460 :func:`email.utils.collapse_rfc2231_value`, passing in the return value
Benjamin Petersone41251e2008-04-25 01:59:09 +0000461 from :meth:`get_param`. This will return a suitably decoded Unicode
R. David Murray7ec754b2010-12-13 23:51:19 +0000462 string when the value is a tuple, or the original string unquoted if it
Benjamin Petersone41251e2008-04-25 01:59:09 +0000463 isn't. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000464
Benjamin Petersone41251e2008-04-25 01:59:09 +0000465 rawparam = msg.get_param('foo')
Georg Brandl540b45c2009-04-27 16:45:26 +0000466 param = email.utils.collapse_rfc2231_value(rawparam)
Georg Brandl116aa622007-08-15 14:28:22 +0000467
Benjamin Petersone41251e2008-04-25 01:59:09 +0000468 In any case, the parameter value (either the returned string, or the
469 ``VALUE`` item in the 3-tuple) is always unquoted, unless *unquote* is set
470 to ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000471
Georg Brandl116aa622007-08-15 14:28:22 +0000472
Larry Hastings3732ed22014-03-15 21:13:56 -0700473 .. method:: set_param(param, value, header='Content-Type', requote=True, \
R David Murray3da240f2013-10-16 22:48:40 -0400474 charset=None, language='', replace=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000475
Benjamin Petersone41251e2008-04-25 01:59:09 +0000476 Set a parameter in the :mailheader:`Content-Type` header. If the
477 parameter already exists in the header, its value will be replaced with
478 *value*. If the :mailheader:`Content-Type` header as not yet been defined
479 for this message, it will be set to :mimetype:`text/plain` and the new
480 parameter value will be appended as per :rfc:`2045`.
Georg Brandl116aa622007-08-15 14:28:22 +0000481
Benjamin Petersone41251e2008-04-25 01:59:09 +0000482 Optional *header* specifies an alternative header to
483 :mailheader:`Content-Type`, and all parameters will be quoted as necessary
484 unless optional *requote* is ``False`` (the default is ``True``).
Georg Brandl116aa622007-08-15 14:28:22 +0000485
Benjamin Petersone41251e2008-04-25 01:59:09 +0000486 If optional *charset* is specified, the parameter will be encoded
487 according to :rfc:`2231`. Optional *language* specifies the RFC 2231
488 language, defaulting to the empty string. Both *charset* and *language*
489 should be strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000490
R David Murray3da240f2013-10-16 22:48:40 -0400491 If *replace* is ``False`` (the default) the header is moved to the
492 end of the list of headers. If *replace* is ``True``, the header
493 will be updated in place.
494
Larry Hastings3732ed22014-03-15 21:13:56 -0700495 .. versionchanged:: 3.4 ``replace`` keyword was added.
R David Murray3da240f2013-10-16 22:48:40 -0400496
Georg Brandl116aa622007-08-15 14:28:22 +0000497
Georg Brandl3f076d82009-05-17 11:28:33 +0000498 .. method:: del_param(param, header='content-type', requote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000499
Benjamin Petersone41251e2008-04-25 01:59:09 +0000500 Remove the given parameter completely from the :mailheader:`Content-Type`
501 header. The header will be re-written in place without the parameter or
502 its value. All values will be quoted as necessary unless *requote* is
503 ``False`` (the default is ``True``). Optional *header* specifies an
504 alternative to :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000505
Georg Brandl116aa622007-08-15 14:28:22 +0000506
Georg Brandl3f076d82009-05-17 11:28:33 +0000507 .. method:: set_type(type, header='Content-Type', requote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000508
Benjamin Petersone41251e2008-04-25 01:59:09 +0000509 Set the main type and subtype for the :mailheader:`Content-Type`
510 header. *type* must be a string in the form :mimetype:`maintype/subtype`,
511 otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000512
Benjamin Petersone41251e2008-04-25 01:59:09 +0000513 This method replaces the :mailheader:`Content-Type` header, keeping all
514 the parameters in place. If *requote* is ``False``, this leaves the
515 existing header's quoting as is, otherwise the parameters will be quoted
516 (the default).
Georg Brandl116aa622007-08-15 14:28:22 +0000517
Benjamin Petersone41251e2008-04-25 01:59:09 +0000518 An alternative header can be specified in the *header* argument. When the
519 :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version`
520 header is also added.
Georg Brandl116aa622007-08-15 14:28:22 +0000521
Georg Brandl116aa622007-08-15 14:28:22 +0000522
Georg Brandl3f076d82009-05-17 11:28:33 +0000523 .. method:: get_filename(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000524
Benjamin Petersone41251e2008-04-25 01:59:09 +0000525 Return the value of the ``filename`` parameter of the
526 :mailheader:`Content-Disposition` header of the message. If the header
527 does not have a ``filename`` parameter, this method falls back to looking
R. David Murray9ed34be2010-03-04 17:38:18 +0000528 for the ``name`` parameter on the :mailheader:`Content-Type` header. If
529 neither is found, or the header is missing, then *failobj* is returned.
530 The returned string will always be unquoted as per
531 :func:`email.utils.unquote`.
Georg Brandl116aa622007-08-15 14:28:22 +0000532
533
Georg Brandl3f076d82009-05-17 11:28:33 +0000534 .. method:: get_boundary(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000535
Benjamin Petersone41251e2008-04-25 01:59:09 +0000536 Return the value of the ``boundary`` parameter of the
537 :mailheader:`Content-Type` header of the message, or *failobj* if either
538 the header is missing, or has no ``boundary`` parameter. The returned
Georg Brandl540b45c2009-04-27 16:45:26 +0000539 string will always be unquoted as per :func:`email.utils.unquote`.
Georg Brandl116aa622007-08-15 14:28:22 +0000540
541
Benjamin Petersone41251e2008-04-25 01:59:09 +0000542 .. method:: set_boundary(boundary)
Georg Brandl116aa622007-08-15 14:28:22 +0000543
Benjamin Petersone41251e2008-04-25 01:59:09 +0000544 Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
545 *boundary*. :meth:`set_boundary` will always quote *boundary* if
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300546 necessary. A :exc:`~email.errors.HeaderParseError` is raised if the
547 message object has no :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000548
Benjamin Petersone41251e2008-04-25 01:59:09 +0000549 Note that using this method is subtly different than deleting the old
550 :mailheader:`Content-Type` header and adding a new one with the new
551 boundary via :meth:`add_header`, because :meth:`set_boundary` preserves
552 the order of the :mailheader:`Content-Type` header in the list of
553 headers. However, it does *not* preserve any continuation lines which may
554 have been present in the original :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000555
556
Georg Brandl3f076d82009-05-17 11:28:33 +0000557 .. method:: get_content_charset(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000558
Benjamin Petersone41251e2008-04-25 01:59:09 +0000559 Return the ``charset`` parameter of the :mailheader:`Content-Type` header,
560 coerced to lower case. If there is no :mailheader:`Content-Type` header, or if
561 that header has no ``charset`` parameter, *failobj* is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000562
Benjamin Petersone41251e2008-04-25 01:59:09 +0000563 Note that this method differs from :meth:`get_charset` which returns the
Georg Brandl3638e482009-04-27 16:46:17 +0000564 :class:`~email.charset.Charset` instance for the default encoding of the message body.
Georg Brandl116aa622007-08-15 14:28:22 +0000565
Georg Brandl116aa622007-08-15 14:28:22 +0000566
Georg Brandl3f076d82009-05-17 11:28:33 +0000567 .. method:: get_charsets(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000568
Benjamin Petersone41251e2008-04-25 01:59:09 +0000569 Return a list containing the character set names in the message. If the
570 message is a :mimetype:`multipart`, then the list will contain one element
571 for each subpart in the payload, otherwise, it will be a list of length 1.
Georg Brandl116aa622007-08-15 14:28:22 +0000572
Benjamin Petersone41251e2008-04-25 01:59:09 +0000573 Each item in the list will be a string which is the value of the
574 ``charset`` parameter in the :mailheader:`Content-Type` header for the
575 represented subpart. However, if the subpart has no
576 :mailheader:`Content-Type` header, no ``charset`` parameter, or is not of
577 the :mimetype:`text` main MIME type, then that item in the returned list
578 will be *failobj*.
Georg Brandl116aa622007-08-15 14:28:22 +0000579
580
R David Murrayb744f3a2015-05-16 15:41:07 -0400581 .. method:: get_content_disposition()
582
583 Return the lowercased value (without parameters) of the message's
584 :mailheader:`Content-Disposition` header if it has one, or ``None``. The
585 possible values for this method are *inline*, *attachment* or ``None``
586 if the message follows :rfc:`2183`.
587
588 .. versionadded:: 3.5
589
Benjamin Petersone41251e2008-04-25 01:59:09 +0000590 .. method:: walk()
Georg Brandl116aa622007-08-15 14:28:22 +0000591
Benjamin Petersone41251e2008-04-25 01:59:09 +0000592 The :meth:`walk` method is an all-purpose generator which can be used to
593 iterate over all the parts and subparts of a message object tree, in
594 depth-first traversal order. You will typically use :meth:`walk` as the
595 iterator in a ``for`` loop; each iteration returns the next subpart.
Georg Brandl116aa622007-08-15 14:28:22 +0000596
Benjamin Petersone41251e2008-04-25 01:59:09 +0000597 Here's an example that prints the MIME type of every part of a multipart
R David Murrayfdfb0052013-07-29 15:49:58 -0400598 message structure:
Georg Brandl116aa622007-08-15 14:28:22 +0000599
R David Murray9cc5fd72014-09-27 15:37:40 -0400600 .. testsetup::
R David Murrayfdfb0052013-07-29 15:49:58 -0400601
R David Murray9cc5fd72014-09-27 15:37:40 -0400602 >>> from email import message_from_binary_file
603 >>> with open('Lib/test/test_email/data/msg_16.txt', 'rb') as f:
604 ... msg = message_from_binary_file(f)
605 >>> from email.iterators import _structure
R David Murrayfdfb0052013-07-29 15:49:58 -0400606
R David Murray9cc5fd72014-09-27 15:37:40 -0400607 .. doctest::
R David Murrayfdfb0052013-07-29 15:49:58 -0400608
R David Murray9cc5fd72014-09-27 15:37:40 -0400609 >>> for part in msg.walk():
610 ... print(part.get_content_type())
611 multipart/report
612 text/plain
613 message/delivery-status
614 text/plain
615 text/plain
616 message/rfc822
617 text/plain
618
619 ``walk`` iterates over the subparts of any part where
620 :meth:`is_multipart` returns ``True``, even though
621 ``msg.get_content_maintype() == 'multipart'`` may return ``False``. We
622 can see this in our example by making use of the ``_structure`` debug
623 helper function:
624
625 .. doctest::
626
627 >>> for part in msg.walk():
628 ... print(part.get_content_maintype() == 'multipart'),
629 ... part.is_multipart())
630 True True
631 False False
632 False True
633 False False
634 False False
635 False True
636 False False
637 >>> _structure(msg)
638 multipart/report
639 text/plain
640 message/delivery-status
641 text/plain
642 text/plain
643 message/rfc822
644 text/plain
645
646 Here the ``message`` parts are not ``multiparts``, but they do contain
647 subparts. ``is_multipart()`` returns ``True`` and ``walk`` descends
648 into the subparts.
649
Georg Brandl116aa622007-08-15 14:28:22 +0000650
Benjamin Petersone41251e2008-04-25 01:59:09 +0000651 :class:`Message` objects can also optionally contain two instance attributes,
652 which can be used when generating the plain text of a MIME message.
Georg Brandl116aa622007-08-15 14:28:22 +0000653
654
Benjamin Petersone41251e2008-04-25 01:59:09 +0000655 .. attribute:: preamble
Georg Brandl116aa622007-08-15 14:28:22 +0000656
Benjamin Petersone41251e2008-04-25 01:59:09 +0000657 The format of a MIME document allows for some text between the blank line
658 following the headers, and the first multipart boundary string. Normally,
659 this text is never visible in a MIME-aware mail reader because it falls
660 outside the standard MIME armor. However, when viewing the raw text of
661 the message, or when viewing the message in a non-MIME aware reader, this
662 text can become visible.
Georg Brandl116aa622007-08-15 14:28:22 +0000663
Benjamin Petersone41251e2008-04-25 01:59:09 +0000664 The *preamble* attribute contains this leading extra-armor text for MIME
Georg Brandl3638e482009-04-27 16:46:17 +0000665 documents. When the :class:`~email.parser.Parser` discovers some text
666 after the headers but before the first boundary string, it assigns this
667 text to the message's *preamble* attribute. When the
668 :class:`~email.generator.Generator` is writing out the plain text
669 representation of a MIME message, and it finds the
Benjamin Petersone41251e2008-04-25 01:59:09 +0000670 message has a *preamble* attribute, it will write this text in the area
671 between the headers and the first boundary. See :mod:`email.parser` and
672 :mod:`email.generator` for details.
Georg Brandl116aa622007-08-15 14:28:22 +0000673
Benjamin Petersone41251e2008-04-25 01:59:09 +0000674 Note that if the message object has no preamble, the *preamble* attribute
675 will be ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000676
677
Benjamin Petersone41251e2008-04-25 01:59:09 +0000678 .. attribute:: epilogue
Georg Brandl116aa622007-08-15 14:28:22 +0000679
Benjamin Petersone41251e2008-04-25 01:59:09 +0000680 The *epilogue* attribute acts the same way as the *preamble* attribute,
681 except that it contains text that appears between the last boundary and
682 the end of the message.
Georg Brandl116aa622007-08-15 14:28:22 +0000683
Benjamin Petersone41251e2008-04-25 01:59:09 +0000684 You do not need to set the epilogue to the empty string in order for the
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300685 :class:`~email.generator.Generator` to print a newline at the end of the
686 file.
Georg Brandl116aa622007-08-15 14:28:22 +0000687
688
Benjamin Petersone41251e2008-04-25 01:59:09 +0000689 .. attribute:: defects
Georg Brandl116aa622007-08-15 14:28:22 +0000690
Benjamin Petersone41251e2008-04-25 01:59:09 +0000691 The *defects* attribute contains a list of all the problems found when
692 parsing this message. See :mod:`email.errors` for a detailed description
693 of the possible parsing defects.