blob: ebe2d9cb4762e1bbc32a12942a97b064b2fd3a97 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`email`: Representing an email message
2-------------------------------------------
3
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
34.. class:: Message()
35
36 The constructor takes no arguments.
37
38
Georg Brandl3f076d82009-05-17 11:28:33 +000039 .. method:: as_string(unixfrom=False, maxheaderlen=0)
Georg Brandl116aa622007-08-15 14:28:22 +000040
Benjamin Petersone41251e2008-04-25 01:59:09 +000041 Return the entire message flattened as a string. When optional *unixfrom*
42 is ``True``, the envelope header is included in the returned string.
R. David Murray101f2782010-01-10 19:18:27 +000043 *unixfrom* defaults to ``False``. Flattening the message may trigger
44 changes to the :class:`Message` if defaults need to be filled in to
45 complete the transformation to a string (for example, MIME boundaries may
46 be generated or modified).
Georg Brandl116aa622007-08-15 14:28:22 +000047
Benjamin Petersone41251e2008-04-25 01:59:09 +000048 Note that this method is provided as a convenience and may not always
R David Murray7dedcb42011-03-15 14:01:18 -040049 format the message the way you want. For example, by default it does
50 not do the mangling of lines that begin with ``From`` that is
51 required by the unix mbox format. For more flexibility, instantiate a
Georg Brandl3638e482009-04-27 16:46:17 +000052 :class:`~email.generator.Generator` instance and use its :meth:`flatten`
53 method directly. For example::
Georg Brandl116aa622007-08-15 14:28:22 +000054
Georg Brandl03124942008-06-10 15:50:56 +000055 from io import StringIO
Benjamin Petersone41251e2008-04-25 01:59:09 +000056 from email.generator import Generator
57 fp = StringIO()
R David Murray7dedcb42011-03-15 14:01:18 -040058 g = Generator(fp, mangle_from_=True, maxheaderlen=60)
Benjamin Petersone41251e2008-04-25 01:59:09 +000059 g.flatten(msg)
60 text = fp.getvalue()
Georg Brandl116aa622007-08-15 14:28:22 +000061
62
Benjamin Petersone41251e2008-04-25 01:59:09 +000063 .. method:: __str__()
Georg Brandl116aa622007-08-15 14:28:22 +000064
Benjamin Petersone41251e2008-04-25 01:59:09 +000065 Equivalent to ``as_string(unixfrom=True)``.
Georg Brandl116aa622007-08-15 14:28:22 +000066
67
Benjamin Petersone41251e2008-04-25 01:59:09 +000068 .. method:: is_multipart()
Georg Brandl116aa622007-08-15 14:28:22 +000069
Benjamin Petersone41251e2008-04-25 01:59:09 +000070 Return ``True`` if the message's payload is a list of sub-\
71 :class:`Message` objects, otherwise return ``False``. When
72 :meth:`is_multipart` returns False, the payload should be a string object.
Georg Brandl116aa622007-08-15 14:28:22 +000073
74
Benjamin Petersone41251e2008-04-25 01:59:09 +000075 .. method:: set_unixfrom(unixfrom)
Georg Brandl116aa622007-08-15 14:28:22 +000076
Benjamin Petersone41251e2008-04-25 01:59:09 +000077 Set the message's envelope header to *unixfrom*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +000078
79
Benjamin Petersone41251e2008-04-25 01:59:09 +000080 .. method:: get_unixfrom()
Georg Brandl116aa622007-08-15 14:28:22 +000081
Benjamin Petersone41251e2008-04-25 01:59:09 +000082 Return the message's envelope header. Defaults to ``None`` if the
83 envelope header was never set.
Georg Brandl116aa622007-08-15 14:28:22 +000084
85
Benjamin Petersone41251e2008-04-25 01:59:09 +000086 .. method:: attach(payload)
Georg Brandl116aa622007-08-15 14:28:22 +000087
Benjamin Petersone41251e2008-04-25 01:59:09 +000088 Add the given *payload* to the current payload, which must be ``None`` or
89 a list of :class:`Message` objects before the call. After the call, the
90 payload will always be a list of :class:`Message` objects. If you want to
91 set the payload to a scalar object (e.g. a string), use
92 :meth:`set_payload` instead.
Georg Brandl116aa622007-08-15 14:28:22 +000093
94
Georg Brandl3f076d82009-05-17 11:28:33 +000095 .. method:: get_payload(i=None, decode=False)
Georg Brandl116aa622007-08-15 14:28:22 +000096
Benjamin Petersond6313712008-07-31 16:23:04 +000097 Return the current payload, which will be a list of
Benjamin Petersone41251e2008-04-25 01:59:09 +000098 :class:`Message` objects when :meth:`is_multipart` is ``True``, or a
99 string when :meth:`is_multipart` is ``False``. If the payload is a list
100 and you mutate the list object, you modify the message's payload in place.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
Benjamin Petersone41251e2008-04-25 01:59:09 +0000102 With optional argument *i*, :meth:`get_payload` will return the *i*-th
103 element of the payload, counting from zero, if :meth:`is_multipart` is
104 ``True``. An :exc:`IndexError` will be raised if *i* is less than 0 or
105 greater than or equal to the number of items in the payload. If the
106 payload is a string (i.e. :meth:`is_multipart` is ``False``) and *i* is
107 given, a :exc:`TypeError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 Optional *decode* is a flag indicating whether the payload should be
110 decoded or not, according to the :mailheader:`Content-Transfer-Encoding`
111 header. When ``True`` and the message is not a multipart, the payload will
112 be decoded if this header's value is ``quoted-printable`` or ``base64``.
113 If some other encoding is used, or :mailheader:`Content-Transfer-Encoding`
114 header is missing, or if the payload has bogus base64 data, the payload is
R. David Murray96fd54e2010-10-08 15:55:28 +0000115 returned as-is (undecoded). In all cases the returned value is binary
116 data. If the message is a multipart and the *decode* flag is ``True``,
117 then ``None`` is returned.
118
119 When *decode* is ``False`` (the default) the body is returned as a string
120 without decoding the :mailheader:`Content-Transfer-Encoding`. However,
121 for a :mailheader:`Content-Transfer-Encoding` of 8bit, an attempt is made
Senthil Kumaran82270452010-10-15 13:29:33 +0000122 to decode the original bytes using the ``charset`` specified by the
123 :mailheader:`Content-Type` header, using the ``replace`` error handler.
124 If no ``charset`` is specified, or if the ``charset`` given is not
125 recognized by the email package, the body is decoded using the default
126 ASCII charset.
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128
Georg Brandl3f076d82009-05-17 11:28:33 +0000129 .. method:: set_payload(payload, charset=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Benjamin Petersone41251e2008-04-25 01:59:09 +0000131 Set the entire message object's payload to *payload*. It is the client's
132 responsibility to ensure the payload invariants. Optional *charset* sets
133 the message's default character set; see :meth:`set_charset` for details.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Benjamin Petersone41251e2008-04-25 01:59:09 +0000135 .. method:: set_charset(charset)
Georg Brandl116aa622007-08-15 14:28:22 +0000136
Benjamin Petersone41251e2008-04-25 01:59:09 +0000137 Set the character set of the payload to *charset*, which can either be a
Georg Brandl3638e482009-04-27 16:46:17 +0000138 :class:`~email.charset.Charset` instance (see :mod:`email.charset`), a
139 string naming a character set, or ``None``. If it is a string, it will
140 be converted to a :class:`~email.charset.Charset` instance. If *charset*
141 is ``None``, the ``charset`` parameter will be removed from the
142 :mailheader:`Content-Type` header. Anything else will generate a
143 :exc:`TypeError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
Benjamin Petersone41251e2008-04-25 01:59:09 +0000145 The message will be assumed to be of type :mimetype:`text/\*` encoded with
146 *charset.input_charset*. It will be converted to *charset.output_charset*
147 and encoded properly, if needed, when generating the plain text
148 representation of the message. MIME headers (:mailheader:`MIME-Version`,
149 :mailheader:`Content-Type`, :mailheader:`Content-Transfer-Encoding`) will
150 be added as needed.
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Benjamin Petersone41251e2008-04-25 01:59:09 +0000152 .. method:: get_charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000153
Georg Brandl3638e482009-04-27 16:46:17 +0000154 Return the :class:`~email.charset.Charset` instance associated with the
155 message's payload.
Georg Brandl116aa622007-08-15 14:28:22 +0000156
Benjamin Petersone41251e2008-04-25 01:59:09 +0000157 The following methods implement a mapping-like interface for accessing the
158 message's :rfc:`2822` headers. Note that there are some semantic differences
159 between these methods and a normal mapping (i.e. dictionary) interface. For
160 example, in a dictionary there are no duplicate keys, but here there may be
161 duplicate message headers. Also, in dictionaries there is no guaranteed
162 order to the keys returned by :meth:`keys`, but in a :class:`Message` object,
163 headers are always returned in the order they appeared in the original
164 message, or were added to the message later. Any header deleted and then
165 re-added are always appended to the end of the header list.
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Benjamin Petersone41251e2008-04-25 01:59:09 +0000167 These semantic differences are intentional and are biased toward maximal
168 convenience.
Georg Brandl116aa622007-08-15 14:28:22 +0000169
Benjamin Petersone41251e2008-04-25 01:59:09 +0000170 Note that in all cases, any envelope header present in the message is not
171 included in the mapping interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000172
R. David Murray92532142011-01-07 23:25:30 +0000173 In a model generated from bytes, any header values that (in contravention of
174 the RFCs) contain non-ASCII bytes will, when retrieved through this
175 interface, be represented as :class:`~email.header.Header` objects with
176 a charset of `unknown-8bit`.
R. David Murray96fd54e2010-10-08 15:55:28 +0000177
Georg Brandl116aa622007-08-15 14:28:22 +0000178
Benjamin Petersone41251e2008-04-25 01:59:09 +0000179 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000180
Benjamin Petersone41251e2008-04-25 01:59:09 +0000181 Return the total number of headers, including duplicates.
Georg Brandl116aa622007-08-15 14:28:22 +0000182
183
Benjamin Petersone41251e2008-04-25 01:59:09 +0000184 .. method:: __contains__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000185
Benjamin Petersone41251e2008-04-25 01:59:09 +0000186 Return true if the message object has a field named *name*. Matching is
187 done case-insensitively and *name* should not include the trailing colon.
188 Used for the ``in`` operator, e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000189
Benjamin Petersone41251e2008-04-25 01:59:09 +0000190 if 'message-id' in myMessage:
191 print('Message-ID:', myMessage['message-id'])
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193
Benjamin Petersone41251e2008-04-25 01:59:09 +0000194 .. method:: __getitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000195
Benjamin Petersone41251e2008-04-25 01:59:09 +0000196 Return the value of the named header field. *name* should not include the
197 colon field separator. If the header is missing, ``None`` is returned; a
198 :exc:`KeyError` is never raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000199
Benjamin Petersone41251e2008-04-25 01:59:09 +0000200 Note that if the named field appears more than once in the message's
201 headers, exactly which of those field values will be returned is
202 undefined. Use the :meth:`get_all` method to get the values of all the
203 extant named headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000204
205
Benjamin Petersone41251e2008-04-25 01:59:09 +0000206 .. method:: __setitem__(name, val)
Georg Brandl116aa622007-08-15 14:28:22 +0000207
Benjamin Petersone41251e2008-04-25 01:59:09 +0000208 Add a header to the message with field name *name* and value *val*. The
209 field is appended to the end of the message's existing fields.
Georg Brandl116aa622007-08-15 14:28:22 +0000210
Benjamin Petersone41251e2008-04-25 01:59:09 +0000211 Note that this does *not* overwrite or delete any existing header with the same
212 name. If you want to ensure that the new header is the only one present in the
213 message with field name *name*, delete the field first, e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000214
Benjamin Petersone41251e2008-04-25 01:59:09 +0000215 del msg['subject']
216 msg['subject'] = 'Python roolz!'
Georg Brandl116aa622007-08-15 14:28:22 +0000217
218
Benjamin Petersone41251e2008-04-25 01:59:09 +0000219 .. method:: __delitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000220
Benjamin Petersone41251e2008-04-25 01:59:09 +0000221 Delete all occurrences of the field with name *name* from the message's
Georg Brandl3f076d82009-05-17 11:28:33 +0000222 headers. No exception is raised if the named field isn't present in the
223 headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225
Benjamin Petersone41251e2008-04-25 01:59:09 +0000226 .. method:: keys()
Georg Brandl116aa622007-08-15 14:28:22 +0000227
Benjamin Petersone41251e2008-04-25 01:59:09 +0000228 Return a list of all the message's header field names.
Georg Brandl116aa622007-08-15 14:28:22 +0000229
230
Benjamin Petersone41251e2008-04-25 01:59:09 +0000231 .. method:: values()
Georg Brandl116aa622007-08-15 14:28:22 +0000232
Benjamin Petersone41251e2008-04-25 01:59:09 +0000233 Return a list of all the message's field values.
Georg Brandl116aa622007-08-15 14:28:22 +0000234
235
Benjamin Petersone41251e2008-04-25 01:59:09 +0000236 .. method:: items()
Georg Brandl116aa622007-08-15 14:28:22 +0000237
Benjamin Petersone41251e2008-04-25 01:59:09 +0000238 Return a list of 2-tuples containing all the message's field headers and
239 values.
Georg Brandl116aa622007-08-15 14:28:22 +0000240
241
Georg Brandl3f076d82009-05-17 11:28:33 +0000242 .. method:: get(name, failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000243
Benjamin Petersone41251e2008-04-25 01:59:09 +0000244 Return the value of the named header field. This is identical to
245 :meth:`__getitem__` except that optional *failobj* is returned if the
246 named header is missing (defaults to ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000247
Benjamin Petersone41251e2008-04-25 01:59:09 +0000248 Here are some additional useful methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000249
250
Georg Brandl3f076d82009-05-17 11:28:33 +0000251 .. method:: get_all(name, failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000252
Benjamin Petersone41251e2008-04-25 01:59:09 +0000253 Return a list of all the values for the field named *name*. If there are
254 no such named headers in the message, *failobj* is returned (defaults to
255 ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000256
257
Benjamin Petersone41251e2008-04-25 01:59:09 +0000258 .. method:: add_header(_name, _value, **_params)
Georg Brandl116aa622007-08-15 14:28:22 +0000259
Benjamin Petersone41251e2008-04-25 01:59:09 +0000260 Extended header setting. This method is similar to :meth:`__setitem__`
261 except that additional header parameters can be provided as keyword
262 arguments. *_name* is the header field to add and *_value* is the
263 *primary* value for the header.
Georg Brandl116aa622007-08-15 14:28:22 +0000264
Benjamin Petersone41251e2008-04-25 01:59:09 +0000265 For each item in the keyword argument dictionary *_params*, the key is
266 taken as the parameter name, with underscores converted to dashes (since
267 dashes are illegal in Python identifiers). Normally, the parameter will
268 be added as ``key="value"`` unless the value is ``None``, in which case
R. David Murray7ec754b2010-12-13 23:51:19 +0000269 only the key will be added. If the value contains non-ASCII characters,
270 it can be specified as a three tuple in the format
271 ``(CHARSET, LANGUAGE, VALUE)``, where ``CHARSET`` is a string naming the
272 charset to be used to encode the value, ``LANGUAGE`` can usually be set
Georg Brandlc8843502010-12-19 10:28:46 +0000273 to ``None`` or the empty string (see :rfc:`2231` for other possibilities),
R. David Murray7ec754b2010-12-13 23:51:19 +0000274 and ``VALUE`` is the string value containing non-ASCII code points. If
275 a three tuple is not passed and the value contains non-ASCII characters,
Georg Brandlc8843502010-12-19 10:28:46 +0000276 it is automatically encoded in :rfc:`2231` format using a ``CHARSET``
R. David Murray7ec754b2010-12-13 23:51:19 +0000277 of ``utf-8`` and a ``LANGUAGE`` of ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000278
Benjamin Petersone41251e2008-04-25 01:59:09 +0000279 Here's an example::
Georg Brandl116aa622007-08-15 14:28:22 +0000280
Benjamin Petersone41251e2008-04-25 01:59:09 +0000281 msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
Georg Brandl116aa622007-08-15 14:28:22 +0000282
Benjamin Petersone41251e2008-04-25 01:59:09 +0000283 This will add a header that looks like ::
Georg Brandl116aa622007-08-15 14:28:22 +0000284
Benjamin Petersone41251e2008-04-25 01:59:09 +0000285 Content-Disposition: attachment; filename="bud.gif"
Georg Brandl116aa622007-08-15 14:28:22 +0000286
R. David Murray7ec754b2010-12-13 23:51:19 +0000287 An example with with non-ASCII characters::
288
289 msg.add_header('Content-Disposition', 'attachment',
290 filename=('iso-8859-1', '', 'Fußballer.ppt'))
291
292 Which produces ::
293
294 Content-Disposition: attachment; filename*="iso-8859-1''Fu%DFballer.ppt"
295
Georg Brandl116aa622007-08-15 14:28:22 +0000296
Benjamin Petersone41251e2008-04-25 01:59:09 +0000297 .. method:: replace_header(_name, _value)
Georg Brandl116aa622007-08-15 14:28:22 +0000298
Benjamin Petersone41251e2008-04-25 01:59:09 +0000299 Replace a header. Replace the first header found in the message that
300 matches *_name*, retaining header order and field name case. If no
301 matching header was found, a :exc:`KeyError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000302
Georg Brandl116aa622007-08-15 14:28:22 +0000303
Benjamin Petersone41251e2008-04-25 01:59:09 +0000304 .. method:: get_content_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000305
Benjamin Petersone41251e2008-04-25 01:59:09 +0000306 Return the message's content type. The returned string is coerced to
307 lower case of the form :mimetype:`maintype/subtype`. If there was no
308 :mailheader:`Content-Type` header in the message the default type as given
309 by :meth:`get_default_type` will be returned. Since according to
310 :rfc:`2045`, messages always have a default type, :meth:`get_content_type`
311 will always return a value.
Georg Brandl116aa622007-08-15 14:28:22 +0000312
Benjamin Petersone41251e2008-04-25 01:59:09 +0000313 :rfc:`2045` defines a message's default type to be :mimetype:`text/plain`
314 unless it appears inside a :mimetype:`multipart/digest` container, in
315 which case it would be :mimetype:`message/rfc822`. If the
316 :mailheader:`Content-Type` header has an invalid type specification,
317 :rfc:`2045` mandates that the default type be :mimetype:`text/plain`.
Georg Brandl116aa622007-08-15 14:28:22 +0000318
Georg Brandl116aa622007-08-15 14:28:22 +0000319
Benjamin Petersone41251e2008-04-25 01:59:09 +0000320 .. method:: get_content_maintype()
Georg Brandl116aa622007-08-15 14:28:22 +0000321
Benjamin Petersone41251e2008-04-25 01:59:09 +0000322 Return the message's main content type. This is the :mimetype:`maintype`
323 part of the string returned by :meth:`get_content_type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000324
Georg Brandl116aa622007-08-15 14:28:22 +0000325
Benjamin Petersone41251e2008-04-25 01:59:09 +0000326 .. method:: get_content_subtype()
Georg Brandl116aa622007-08-15 14:28:22 +0000327
Benjamin Petersone41251e2008-04-25 01:59:09 +0000328 Return the message's sub-content type. This is the :mimetype:`subtype`
329 part of the string returned by :meth:`get_content_type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000330
Georg Brandl116aa622007-08-15 14:28:22 +0000331
Benjamin Petersone41251e2008-04-25 01:59:09 +0000332 .. method:: get_default_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000333
Benjamin Petersone41251e2008-04-25 01:59:09 +0000334 Return the default content type. Most messages have a default content
335 type of :mimetype:`text/plain`, except for messages that are subparts of
336 :mimetype:`multipart/digest` containers. Such subparts have a default
337 content type of :mimetype:`message/rfc822`.
Georg Brandl116aa622007-08-15 14:28:22 +0000338
Georg Brandl116aa622007-08-15 14:28:22 +0000339
Benjamin Petersone41251e2008-04-25 01:59:09 +0000340 .. method:: set_default_type(ctype)
Georg Brandl116aa622007-08-15 14:28:22 +0000341
Benjamin Petersone41251e2008-04-25 01:59:09 +0000342 Set the default content type. *ctype* should either be
343 :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is not
344 enforced. The default content type is not stored in the
345 :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000346
Georg Brandl116aa622007-08-15 14:28:22 +0000347
Georg Brandl3f076d82009-05-17 11:28:33 +0000348 .. method:: get_params(failobj=None, header='content-type', unquote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000349
Benjamin Petersone41251e2008-04-25 01:59:09 +0000350 Return the message's :mailheader:`Content-Type` parameters, as a list.
351 The elements of the returned list are 2-tuples of key/value pairs, as
352 split on the ``'='`` sign. The left hand side of the ``'='`` is the key,
353 while the right hand side is the value. If there is no ``'='`` sign in
354 the parameter the value is the empty string, otherwise the value is as
355 described in :meth:`get_param` and is unquoted if optional *unquote* is
356 ``True`` (the default).
Georg Brandl116aa622007-08-15 14:28:22 +0000357
Benjamin Petersone41251e2008-04-25 01:59:09 +0000358 Optional *failobj* is the object to return if there is no
359 :mailheader:`Content-Type` header. Optional *header* is the header to
360 search instead of :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000361
Georg Brandl116aa622007-08-15 14:28:22 +0000362
Georg Brandl3f076d82009-05-17 11:28:33 +0000363 .. method:: get_param(param, failobj=None, header='content-type', unquote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000364
Benjamin Petersone41251e2008-04-25 01:59:09 +0000365 Return the value of the :mailheader:`Content-Type` header's parameter
366 *param* as a string. If the message has no :mailheader:`Content-Type`
367 header or if there is no such parameter, then *failobj* is returned
368 (defaults to ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000369
Benjamin Petersone41251e2008-04-25 01:59:09 +0000370 Optional *header* if given, specifies the message header to use instead of
371 :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000372
Benjamin Petersone41251e2008-04-25 01:59:09 +0000373 Parameter keys are always compared case insensitively. The return value
374 can either be a string, or a 3-tuple if the parameter was :rfc:`2231`
375 encoded. When it's a 3-tuple, the elements of the value are of the form
376 ``(CHARSET, LANGUAGE, VALUE)``. Note that both ``CHARSET`` and
377 ``LANGUAGE`` can be ``None``, in which case you should consider ``VALUE``
378 to be encoded in the ``us-ascii`` charset. You can usually ignore
379 ``LANGUAGE``.
Georg Brandl116aa622007-08-15 14:28:22 +0000380
Benjamin Petersone41251e2008-04-25 01:59:09 +0000381 If your application doesn't care whether the parameter was encoded as in
382 :rfc:`2231`, you can collapse the parameter value by calling
Georg Brandl540b45c2009-04-27 16:45:26 +0000383 :func:`email.utils.collapse_rfc2231_value`, passing in the return value
Benjamin Petersone41251e2008-04-25 01:59:09 +0000384 from :meth:`get_param`. This will return a suitably decoded Unicode
R. David Murray7ec754b2010-12-13 23:51:19 +0000385 string when the value is a tuple, or the original string unquoted if it
Benjamin Petersone41251e2008-04-25 01:59:09 +0000386 isn't. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000387
Benjamin Petersone41251e2008-04-25 01:59:09 +0000388 rawparam = msg.get_param('foo')
Georg Brandl540b45c2009-04-27 16:45:26 +0000389 param = email.utils.collapse_rfc2231_value(rawparam)
Georg Brandl116aa622007-08-15 14:28:22 +0000390
Benjamin Petersone41251e2008-04-25 01:59:09 +0000391 In any case, the parameter value (either the returned string, or the
392 ``VALUE`` item in the 3-tuple) is always unquoted, unless *unquote* is set
393 to ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000394
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Georg Brandl3f076d82009-05-17 11:28:33 +0000396 .. method:: set_param(param, value, header='Content-Type', requote=True, charset=None, language='')
Georg Brandl116aa622007-08-15 14:28:22 +0000397
Benjamin Petersone41251e2008-04-25 01:59:09 +0000398 Set a parameter in the :mailheader:`Content-Type` header. If the
399 parameter already exists in the header, its value will be replaced with
400 *value*. If the :mailheader:`Content-Type` header as not yet been defined
401 for this message, it will be set to :mimetype:`text/plain` and the new
402 parameter value will be appended as per :rfc:`2045`.
Georg Brandl116aa622007-08-15 14:28:22 +0000403
Benjamin Petersone41251e2008-04-25 01:59:09 +0000404 Optional *header* specifies an alternative header to
405 :mailheader:`Content-Type`, and all parameters will be quoted as necessary
406 unless optional *requote* is ``False`` (the default is ``True``).
Georg Brandl116aa622007-08-15 14:28:22 +0000407
Benjamin Petersone41251e2008-04-25 01:59:09 +0000408 If optional *charset* is specified, the parameter will be encoded
409 according to :rfc:`2231`. Optional *language* specifies the RFC 2231
410 language, defaulting to the empty string. Both *charset* and *language*
411 should be strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000412
Georg Brandl116aa622007-08-15 14:28:22 +0000413
Georg Brandl3f076d82009-05-17 11:28:33 +0000414 .. method:: del_param(param, header='content-type', requote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000415
Benjamin Petersone41251e2008-04-25 01:59:09 +0000416 Remove the given parameter completely from the :mailheader:`Content-Type`
417 header. The header will be re-written in place without the parameter or
418 its value. All values will be quoted as necessary unless *requote* is
419 ``False`` (the default is ``True``). Optional *header* specifies an
420 alternative to :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000421
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Georg Brandl3f076d82009-05-17 11:28:33 +0000423 .. method:: set_type(type, header='Content-Type', requote=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000424
Benjamin Petersone41251e2008-04-25 01:59:09 +0000425 Set the main type and subtype for the :mailheader:`Content-Type`
426 header. *type* must be a string in the form :mimetype:`maintype/subtype`,
427 otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000428
Benjamin Petersone41251e2008-04-25 01:59:09 +0000429 This method replaces the :mailheader:`Content-Type` header, keeping all
430 the parameters in place. If *requote* is ``False``, this leaves the
431 existing header's quoting as is, otherwise the parameters will be quoted
432 (the default).
Georg Brandl116aa622007-08-15 14:28:22 +0000433
Benjamin Petersone41251e2008-04-25 01:59:09 +0000434 An alternative header can be specified in the *header* argument. When the
435 :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version`
436 header is also added.
Georg Brandl116aa622007-08-15 14:28:22 +0000437
Georg Brandl116aa622007-08-15 14:28:22 +0000438
Georg Brandl3f076d82009-05-17 11:28:33 +0000439 .. method:: get_filename(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000440
Benjamin Petersone41251e2008-04-25 01:59:09 +0000441 Return the value of the ``filename`` parameter of the
442 :mailheader:`Content-Disposition` header of the message. If the header
443 does not have a ``filename`` parameter, this method falls back to looking
R. David Murray9ed34be2010-03-04 17:38:18 +0000444 for the ``name`` parameter on the :mailheader:`Content-Type` header. If
445 neither is found, or the header is missing, then *failobj* is returned.
446 The returned string will always be unquoted as per
447 :func:`email.utils.unquote`.
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449
Georg Brandl3f076d82009-05-17 11:28:33 +0000450 .. method:: get_boundary(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000451
Benjamin Petersone41251e2008-04-25 01:59:09 +0000452 Return the value of the ``boundary`` parameter of the
453 :mailheader:`Content-Type` header of the message, or *failobj* if either
454 the header is missing, or has no ``boundary`` parameter. The returned
Georg Brandl540b45c2009-04-27 16:45:26 +0000455 string will always be unquoted as per :func:`email.utils.unquote`.
Georg Brandl116aa622007-08-15 14:28:22 +0000456
457
Benjamin Petersone41251e2008-04-25 01:59:09 +0000458 .. method:: set_boundary(boundary)
Georg Brandl116aa622007-08-15 14:28:22 +0000459
Benjamin Petersone41251e2008-04-25 01:59:09 +0000460 Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
461 *boundary*. :meth:`set_boundary` will always quote *boundary* if
462 necessary. A :exc:`HeaderParseError` is raised if the message object has
463 no :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000464
Benjamin Petersone41251e2008-04-25 01:59:09 +0000465 Note that using this method is subtly different than deleting the old
466 :mailheader:`Content-Type` header and adding a new one with the new
467 boundary via :meth:`add_header`, because :meth:`set_boundary` preserves
468 the order of the :mailheader:`Content-Type` header in the list of
469 headers. However, it does *not* preserve any continuation lines which may
470 have been present in the original :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000471
472
Georg Brandl3f076d82009-05-17 11:28:33 +0000473 .. method:: get_content_charset(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000474
Benjamin Petersone41251e2008-04-25 01:59:09 +0000475 Return the ``charset`` parameter of the :mailheader:`Content-Type` header,
476 coerced to lower case. If there is no :mailheader:`Content-Type` header, or if
477 that header has no ``charset`` parameter, *failobj* is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000478
Benjamin Petersone41251e2008-04-25 01:59:09 +0000479 Note that this method differs from :meth:`get_charset` which returns the
Georg Brandl3638e482009-04-27 16:46:17 +0000480 :class:`~email.charset.Charset` instance for the default encoding of the message body.
Georg Brandl116aa622007-08-15 14:28:22 +0000481
Georg Brandl116aa622007-08-15 14:28:22 +0000482
Georg Brandl3f076d82009-05-17 11:28:33 +0000483 .. method:: get_charsets(failobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000484
Benjamin Petersone41251e2008-04-25 01:59:09 +0000485 Return a list containing the character set names in the message. If the
486 message is a :mimetype:`multipart`, then the list will contain one element
487 for each subpart in the payload, otherwise, it will be a list of length 1.
Georg Brandl116aa622007-08-15 14:28:22 +0000488
Benjamin Petersone41251e2008-04-25 01:59:09 +0000489 Each item in the list will be a string which is the value of the
490 ``charset`` parameter in the :mailheader:`Content-Type` header for the
491 represented subpart. However, if the subpart has no
492 :mailheader:`Content-Type` header, no ``charset`` parameter, or is not of
493 the :mimetype:`text` main MIME type, then that item in the returned list
494 will be *failobj*.
Georg Brandl116aa622007-08-15 14:28:22 +0000495
496
Benjamin Petersone41251e2008-04-25 01:59:09 +0000497 .. method:: walk()
Georg Brandl116aa622007-08-15 14:28:22 +0000498
Benjamin Petersone41251e2008-04-25 01:59:09 +0000499 The :meth:`walk` method is an all-purpose generator which can be used to
500 iterate over all the parts and subparts of a message object tree, in
501 depth-first traversal order. You will typically use :meth:`walk` as the
502 iterator in a ``for`` loop; each iteration returns the next subpart.
Georg Brandl116aa622007-08-15 14:28:22 +0000503
Benjamin Petersone41251e2008-04-25 01:59:09 +0000504 Here's an example that prints the MIME type of every part of a multipart
505 message structure::
Georg Brandl116aa622007-08-15 14:28:22 +0000506
Benjamin Petersone41251e2008-04-25 01:59:09 +0000507 >>> for part in msg.walk():
508 ... print(part.get_content_type())
509 multipart/report
510 text/plain
511 message/delivery-status
512 text/plain
513 text/plain
514 message/rfc822
Georg Brandl116aa622007-08-15 14:28:22 +0000515
Benjamin Petersone41251e2008-04-25 01:59:09 +0000516 :class:`Message` objects can also optionally contain two instance attributes,
517 which can be used when generating the plain text of a MIME message.
Georg Brandl116aa622007-08-15 14:28:22 +0000518
519
Benjamin Petersone41251e2008-04-25 01:59:09 +0000520 .. attribute:: preamble
Georg Brandl116aa622007-08-15 14:28:22 +0000521
Benjamin Petersone41251e2008-04-25 01:59:09 +0000522 The format of a MIME document allows for some text between the blank line
523 following the headers, and the first multipart boundary string. Normally,
524 this text is never visible in a MIME-aware mail reader because it falls
525 outside the standard MIME armor. However, when viewing the raw text of
526 the message, or when viewing the message in a non-MIME aware reader, this
527 text can become visible.
Georg Brandl116aa622007-08-15 14:28:22 +0000528
Benjamin Petersone41251e2008-04-25 01:59:09 +0000529 The *preamble* attribute contains this leading extra-armor text for MIME
Georg Brandl3638e482009-04-27 16:46:17 +0000530 documents. When the :class:`~email.parser.Parser` discovers some text
531 after the headers but before the first boundary string, it assigns this
532 text to the message's *preamble* attribute. When the
533 :class:`~email.generator.Generator` is writing out the plain text
534 representation of a MIME message, and it finds the
Benjamin Petersone41251e2008-04-25 01:59:09 +0000535 message has a *preamble* attribute, it will write this text in the area
536 between the headers and the first boundary. See :mod:`email.parser` and
537 :mod:`email.generator` for details.
Georg Brandl116aa622007-08-15 14:28:22 +0000538
Benjamin Petersone41251e2008-04-25 01:59:09 +0000539 Note that if the message object has no preamble, the *preamble* attribute
540 will be ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000541
542
Benjamin Petersone41251e2008-04-25 01:59:09 +0000543 .. attribute:: epilogue
Georg Brandl116aa622007-08-15 14:28:22 +0000544
Benjamin Petersone41251e2008-04-25 01:59:09 +0000545 The *epilogue* attribute acts the same way as the *preamble* attribute,
546 except that it contains text that appears between the last boundary and
547 the end of the message.
Georg Brandl116aa622007-08-15 14:28:22 +0000548
Benjamin Petersone41251e2008-04-25 01:59:09 +0000549 You do not need to set the epilogue to the empty string in order for the
550 :class:`Generator` to print a newline at the end of the file.
Georg Brandl116aa622007-08-15 14:28:22 +0000551
552
Benjamin Petersone41251e2008-04-25 01:59:09 +0000553 .. attribute:: defects
Georg Brandl116aa622007-08-15 14:28:22 +0000554
Benjamin Petersone41251e2008-04-25 01:59:09 +0000555 The *defects* attribute contains a list of all the problems found when
556 parsing this message. See :mod:`email.errors` for a detailed description
557 of the possible parsing defects.