blob: 6f40c05ef4c2191f791fcc8b692be0aad7ee810c [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Benjamin Petersonc7b05922008-04-25 01:29:10 +000039 .. method:: as_string([unixfrom])
Georg Brandl8ec7f652007-08-15 14:28:01 +000040
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Murray0e207df2010-01-10 17:41:28 +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 Brandl8ec7f652007-08-15 14:28:01 +000047
Benjamin Petersonc7b05922008-04-25 01:29:10 +000048 Note that this method is provided as a convenience and may not always
49 format the message the way you want. For example, by default it mangles
50 lines that begin with ``From``. For more flexibility, instantiate a
Georg Brandlb48327a2009-04-13 13:13:25 +000051 :class:`~email.generator.Generator` instance and use its :meth:`flatten`
52 method directly. For example::
Georg Brandl8ec7f652007-08-15 14:28:01 +000053
Benjamin Petersonc7b05922008-04-25 01:29:10 +000054 from cStringIO import StringIO
55 from email.generator import Generator
56 fp = StringIO()
57 g = Generator(fp, mangle_from_=False, maxheaderlen=60)
58 g.flatten(msg)
59 text = fp.getvalue()
Georg Brandl8ec7f652007-08-15 14:28:01 +000060
61
Benjamin Petersonc7b05922008-04-25 01:29:10 +000062 .. method:: __str__()
Georg Brandl8ec7f652007-08-15 14:28:01 +000063
Benjamin Petersonc7b05922008-04-25 01:29:10 +000064 Equivalent to ``as_string(unixfrom=True)``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
66
Benjamin Petersonc7b05922008-04-25 01:29:10 +000067 .. method:: is_multipart()
Georg Brandl8ec7f652007-08-15 14:28:01 +000068
Benjamin Petersonc7b05922008-04-25 01:29:10 +000069 Return ``True`` if the message's payload is a list of sub-\
70 :class:`Message` objects, otherwise return ``False``. When
71 :meth:`is_multipart` returns False, the payload should be a string object.
Georg Brandl8ec7f652007-08-15 14:28:01 +000072
73
Benjamin Petersonc7b05922008-04-25 01:29:10 +000074 .. method:: set_unixfrom(unixfrom)
Georg Brandl8ec7f652007-08-15 14:28:01 +000075
Benjamin Petersonc7b05922008-04-25 01:29:10 +000076 Set the message's envelope header to *unixfrom*, which should be a string.
Georg Brandl8ec7f652007-08-15 14:28:01 +000077
78
Benjamin Petersonc7b05922008-04-25 01:29:10 +000079 .. method:: get_unixfrom()
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
Benjamin Petersonc7b05922008-04-25 01:29:10 +000081 Return the message's envelope header. Defaults to ``None`` if the
82 envelope header was never set.
Georg Brandl8ec7f652007-08-15 14:28:01 +000083
84
Benjamin Petersonc7b05922008-04-25 01:29:10 +000085 .. method:: attach(payload)
Georg Brandl8ec7f652007-08-15 14:28:01 +000086
Benjamin Petersonc7b05922008-04-25 01:29:10 +000087 Add the given *payload* to the current payload, which must be ``None`` or
88 a list of :class:`Message` objects before the call. After the call, the
89 payload will always be a list of :class:`Message` objects. If you want to
90 set the payload to a scalar object (e.g. a string), use
91 :meth:`set_payload` instead.
Georg Brandl8ec7f652007-08-15 14:28:01 +000092
93
Benjamin Petersonc7b05922008-04-25 01:29:10 +000094 .. method:: get_payload([i[, decode]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000095
Andrew M. Kuchlingcd48d8a2008-07-26 13:09:06 +000096 Return the current payload, which will be a list of
Benjamin Petersonc7b05922008-04-25 01:29:10 +000097 :class:`Message` objects when :meth:`is_multipart` is ``True``, or a
98 string when :meth:`is_multipart` is ``False``. If the payload is a list
99 and you mutate the list object, you modify the message's payload in place.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000101 With optional argument *i*, :meth:`get_payload` will return the *i*-th
102 element of the payload, counting from zero, if :meth:`is_multipart` is
103 ``True``. An :exc:`IndexError` will be raised if *i* is less than 0 or
104 greater than or equal to the number of items in the payload. If the
105 payload is a string (i.e. :meth:`is_multipart` is ``False``) and *i* is
106 given, a :exc:`TypeError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000108 Optional *decode* is a flag indicating whether the payload should be
109 decoded or not, according to the :mailheader:`Content-Transfer-Encoding`
110 header. When ``True`` and the message is not a multipart, the payload will
111 be decoded if this header's value is ``quoted-printable`` or ``base64``.
112 If some other encoding is used, or :mailheader:`Content-Transfer-Encoding`
113 header is missing, or if the payload has bogus base64 data, the payload is
114 returned as-is (undecoded). If the message is a multipart and the
115 *decode* flag is ``True``, then ``None`` is returned. The default for
116 *decode* is ``False``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117
118
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000119 .. method:: set_payload(payload[, charset])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000121 Set the entire message object's payload to *payload*. It is the client's
122 responsibility to ensure the payload invariants. Optional *charset* sets
123 the message's default character set; see :meth:`set_charset` for details.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000125 .. versionchanged:: 2.2.2
126 *charset* argument added.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000127
128
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000129 .. method:: set_charset(charset)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000130
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000131 Set the character set of the payload to *charset*, which can either be a
Georg Brandlb48327a2009-04-13 13:13:25 +0000132 :class:`~email.charset.Charset` instance (see :mod:`email.charset`), a
133 string naming a character set, or ``None``. If it is a string, it will
134 be converted to a :class:`~email.charset.Charset` instance. If *charset*
135 is ``None``, the ``charset`` parameter will be removed from the
136 :mailheader:`Content-Type` header. Anything else will generate a
137 :exc:`TypeError`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000138
R. David Murray52dcd452010-06-02 22:03:15 +0000139 The message will be assumed to be of type :mimetype:`text/\*`, with the
140 payload either in unicode or encoded with *charset.input_charset*.
141 It will be encoded or converted to *charset.output_charset*
142 and transfer encoded properly, if needed, when generating the plain text
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000143 representation of the message. MIME headers (:mailheader:`MIME-Version`,
144 :mailheader:`Content-Type`, :mailheader:`Content-Transfer-Encoding`) will
145 be added as needed.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000147 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000148
149
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000150 .. method:: get_charset()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000151
Georg Brandlb48327a2009-04-13 13:13:25 +0000152 Return the :class:`~email.charset.Charset` instance associated with the
153 message's payload.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000154
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000155 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000156
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Brandl8ec7f652007-08-15 14:28:01 +0000166
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000167 These semantic differences are intentional and are biased toward maximal
168 convenience.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000169
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000170 Note that in all cases, any envelope header present in the message is not
171 included in the mapping interface.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000172
173
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000174 .. method:: __len__()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000175
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000176 Return the total number of headers, including duplicates.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000177
178
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000179 .. method:: __contains__(name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000180
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000181 Return true if the message object has a field named *name*. Matching is
182 done case-insensitively and *name* should not include the trailing colon.
183 Used for the ``in`` operator, e.g.::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000184
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000185 if 'message-id' in myMessage:
186 print 'Message-ID:', myMessage['message-id']
Georg Brandl8ec7f652007-08-15 14:28:01 +0000187
188
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000189 .. method:: __getitem__(name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000190
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000191 Return the value of the named header field. *name* should not include the
192 colon field separator. If the header is missing, ``None`` is returned; a
193 :exc:`KeyError` is never raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000194
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000195 Note that if the named field appears more than once in the message's
196 headers, exactly which of those field values will be returned is
197 undefined. Use the :meth:`get_all` method to get the values of all the
198 extant named headers.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000199
200
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000201 .. method:: __setitem__(name, val)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000202
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000203 Add a header to the message with field name *name* and value *val*. The
204 field is appended to the end of the message's existing fields.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000205
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000206 Note that this does *not* overwrite or delete any existing header with the same
207 name. If you want to ensure that the new header is the only one present in the
208 message with field name *name*, delete the field first, e.g.::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000209
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000210 del msg['subject']
211 msg['subject'] = 'Python roolz!'
Georg Brandl8ec7f652007-08-15 14:28:01 +0000212
213
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000214 .. method:: __delitem__(name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000215
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000216 Delete all occurrences of the field with name *name* from the message's
217 headers. No exception is raised if the named field isn't present in the headers.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000218
219
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000220 .. method:: has_key(name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000221
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000222 Return true if the message contains a header field named *name*, otherwise
223 return false.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000224
225
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000226 .. method:: keys()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000227
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000228 Return a list of all the message's header field names.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000229
230
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000231 .. method:: values()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000232
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000233 Return a list of all the message's field values.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000234
235
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000236 .. method:: items()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000237
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000238 Return a list of 2-tuples containing all the message's field headers and
239 values.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000240
241
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000242 .. method:: get(name[, failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000243
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Brandl8ec7f652007-08-15 14:28:01 +0000247
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000248 Here are some additional useful methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000249
250
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000251 .. method:: get_all(name[, failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000252
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Brandl8ec7f652007-08-15 14:28:01 +0000256
257
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000258 .. method:: add_header(_name, _value, **_params)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000259
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Brandl8ec7f652007-08-15 14:28:01 +0000264
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Murraya9aa34c2010-12-14 00:29:27 +0000269 only the key will be added. If the value contains non-ASCII characters,
270 it must 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
273 to ``None`` or the empty string (see :RFC:`2231` for other possibilities),
274 and ``VALUE`` is the string value containing non-ASCII code points.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000275
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000276 Here's an example::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000277
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000278 msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000279
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000280 This will add a header that looks like ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000281
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000282 Content-Disposition: attachment; filename="bud.gif"
Georg Brandl8ec7f652007-08-15 14:28:01 +0000283
R. David Murraya9aa34c2010-12-14 00:29:27 +0000284 An example with with non-ASCII characters::
285
286 msg.add_header('Content-Disposition', 'attachment',
287 filename=('iso-8859-1', '', 'Fußballer.ppt'))
288
289 Which produces ::
290
291 Content-Disposition: attachment; filename*="iso-8859-1''Fu%DFballer.ppt"
292
Georg Brandl8ec7f652007-08-15 14:28:01 +0000293
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000294 .. method:: replace_header(_name, _value)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000295
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000296 Replace a header. Replace the first header found in the message that
297 matches *_name*, retaining header order and field name case. If no
298 matching header was found, a :exc:`KeyError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000299
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000300 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000301
302
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000303 .. method:: get_content_type()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000304
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000305 Return the message's content type. The returned string is coerced to
306 lower case of the form :mimetype:`maintype/subtype`. If there was no
307 :mailheader:`Content-Type` header in the message the default type as given
308 by :meth:`get_default_type` will be returned. Since according to
309 :rfc:`2045`, messages always have a default type, :meth:`get_content_type`
310 will always return a value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000311
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000312 :rfc:`2045` defines a message's default type to be :mimetype:`text/plain`
313 unless it appears inside a :mimetype:`multipart/digest` container, in
314 which case it would be :mimetype:`message/rfc822`. If the
315 :mailheader:`Content-Type` header has an invalid type specification,
316 :rfc:`2045` mandates that the default type be :mimetype:`text/plain`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000317
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000318 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000319
Georg Brandl8ec7f652007-08-15 14:28:01 +0000320
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000321 .. method:: get_content_maintype()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000322
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000323 Return the message's main content type. This is the :mimetype:`maintype`
324 part of the string returned by :meth:`get_content_type`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000325
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000326 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000327
Georg Brandl8ec7f652007-08-15 14:28:01 +0000328
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000329 .. method:: get_content_subtype()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000330
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000331 Return the message's sub-content type. This is the :mimetype:`subtype`
332 part of the string returned by :meth:`get_content_type`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000333
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000334 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000335
Georg Brandl8ec7f652007-08-15 14:28:01 +0000336
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000337 .. method:: get_default_type()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000338
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000339 Return the default content type. Most messages have a default content
340 type of :mimetype:`text/plain`, except for messages that are subparts of
341 :mimetype:`multipart/digest` containers. Such subparts have a default
342 content type of :mimetype:`message/rfc822`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000343
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000344 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000345
Georg Brandl8ec7f652007-08-15 14:28:01 +0000346
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000347 .. method:: set_default_type(ctype)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000348
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000349 Set the default content type. *ctype* should either be
350 :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is not
351 enforced. The default content type is not stored in the
352 :mailheader:`Content-Type` header.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000353
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000354 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000355
Georg Brandl8ec7f652007-08-15 14:28:01 +0000356
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000357 .. method:: get_params([failobj[, header[, unquote]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000358
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000359 Return the message's :mailheader:`Content-Type` parameters, as a list.
360 The elements of the returned list are 2-tuples of key/value pairs, as
361 split on the ``'='`` sign. The left hand side of the ``'='`` is the key,
362 while the right hand side is the value. If there is no ``'='`` sign in
363 the parameter the value is the empty string, otherwise the value is as
364 described in :meth:`get_param` and is unquoted if optional *unquote* is
365 ``True`` (the default).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000366
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000367 Optional *failobj* is the object to return if there is no
368 :mailheader:`Content-Type` header. Optional *header* is the header to
369 search instead of :mailheader:`Content-Type`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000370
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000371 .. versionchanged:: 2.2.2
372 *unquote* argument added.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000373
Georg Brandl8ec7f652007-08-15 14:28:01 +0000374
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000375 .. method:: get_param(param[, failobj[, header[, unquote]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000376
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000377 Return the value of the :mailheader:`Content-Type` header's parameter
378 *param* as a string. If the message has no :mailheader:`Content-Type`
379 header or if there is no such parameter, then *failobj* is returned
380 (defaults to ``None``).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000381
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000382 Optional *header* if given, specifies the message header to use instead of
383 :mailheader:`Content-Type`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000384
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000385 Parameter keys are always compared case insensitively. The return value
386 can either be a string, or a 3-tuple if the parameter was :rfc:`2231`
387 encoded. When it's a 3-tuple, the elements of the value are of the form
388 ``(CHARSET, LANGUAGE, VALUE)``. Note that both ``CHARSET`` and
389 ``LANGUAGE`` can be ``None``, in which case you should consider ``VALUE``
390 to be encoded in the ``us-ascii`` charset. You can usually ignore
391 ``LANGUAGE``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000392
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000393 If your application doesn't care whether the parameter was encoded as in
394 :rfc:`2231`, you can collapse the parameter value by calling
Hirokazu Yamamoto3bd40582009-04-13 01:07:06 +0000395 :func:`email.utils.collapse_rfc2231_value`, passing in the return value
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000396 from :meth:`get_param`. This will return a suitably decoded Unicode
R. David Murraya9aa34c2010-12-14 00:29:27 +0000397 string when the value is a tuple, or the original string unquoted if it
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000398 isn't. For example::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000399
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000400 rawparam = msg.get_param('foo')
Hirokazu Yamamoto3bd40582009-04-13 01:07:06 +0000401 param = email.utils.collapse_rfc2231_value(rawparam)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000402
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000403 In any case, the parameter value (either the returned string, or the
404 ``VALUE`` item in the 3-tuple) is always unquoted, unless *unquote* is set
405 to ``False``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000406
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000407 .. versionchanged:: 2.2.2
408 *unquote* argument added, and 3-tuple return value possible.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000409
410
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000411 .. method:: set_param(param, value[, header[, requote[, charset[, language]]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000412
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000413 Set a parameter in the :mailheader:`Content-Type` header. If the
414 parameter already exists in the header, its value will be replaced with
415 *value*. If the :mailheader:`Content-Type` header as not yet been defined
416 for this message, it will be set to :mimetype:`text/plain` and the new
417 parameter value will be appended as per :rfc:`2045`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000418
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000419 Optional *header* specifies an alternative header to
420 :mailheader:`Content-Type`, and all parameters will be quoted as necessary
421 unless optional *requote* is ``False`` (the default is ``True``).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000422
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000423 If optional *charset* is specified, the parameter will be encoded
424 according to :rfc:`2231`. Optional *language* specifies the RFC 2231
425 language, defaulting to the empty string. Both *charset* and *language*
426 should be strings.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000427
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000428 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000429
430
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000431 .. method:: del_param(param[, header[, requote]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000432
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000433 Remove the given parameter completely from the :mailheader:`Content-Type`
434 header. The header will be re-written in place without the parameter or
435 its value. All values will be quoted as necessary unless *requote* is
436 ``False`` (the default is ``True``). Optional *header* specifies an
437 alternative to :mailheader:`Content-Type`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000438
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000439 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000440
Georg Brandl8ec7f652007-08-15 14:28:01 +0000441
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000442 .. method:: set_type(type[, header][, requote])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000443
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000444 Set the main type and subtype for the :mailheader:`Content-Type`
445 header. *type* must be a string in the form :mimetype:`maintype/subtype`,
446 otherwise a :exc:`ValueError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000447
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000448 This method replaces the :mailheader:`Content-Type` header, keeping all
449 the parameters in place. If *requote* is ``False``, this leaves the
450 existing header's quoting as is, otherwise the parameters will be quoted
451 (the default).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000452
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000453 An alternative header can be specified in the *header* argument. When the
454 :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version`
455 header is also added.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000456
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000457 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000458
459
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000460 .. method:: get_filename([failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000461
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000462 Return the value of the ``filename`` parameter of the
463 :mailheader:`Content-Disposition` header of the message. If the header
464 does not have a ``filename`` parameter, this method falls back to looking
R. David Murray3f3330f2010-03-04 17:34:05 +0000465 for the ``name`` parameter on the :mailheader:`Content-Type` header. If
466 neither is found, or the header is missing, then *failobj* is returned.
467 The returned string will always be unquoted as per
468 :func:`email.utils.unquote`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000469
Georg Brandl8ec7f652007-08-15 14:28:01 +0000470
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000471 .. method:: get_boundary([failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000472
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000473 Return the value of the ``boundary`` parameter of the
474 :mailheader:`Content-Type` header of the message, or *failobj* if either
475 the header is missing, or has no ``boundary`` parameter. The returned
Hirokazu Yamamoto45857462009-04-13 01:21:56 +0000476 string will always be unquoted as per :func:`email.utils.unquote`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000477
Georg Brandl8ec7f652007-08-15 14:28:01 +0000478
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000479 .. method:: set_boundary(boundary)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000480
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000481 Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
482 *boundary*. :meth:`set_boundary` will always quote *boundary* if
483 necessary. A :exc:`HeaderParseError` is raised if the message object has
484 no :mailheader:`Content-Type` header.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000485
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000486 Note that using this method is subtly different than deleting the old
487 :mailheader:`Content-Type` header and adding a new one with the new
488 boundary via :meth:`add_header`, because :meth:`set_boundary` preserves
489 the order of the :mailheader:`Content-Type` header in the list of
490 headers. However, it does *not* preserve any continuation lines which may
491 have been present in the original :mailheader:`Content-Type` header.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000492
Georg Brandl8ec7f652007-08-15 14:28:01 +0000493
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000494 .. method:: get_content_charset([failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000495
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000496 Return the ``charset`` parameter of the :mailheader:`Content-Type` header,
497 coerced to lower case. If there is no :mailheader:`Content-Type` header, or if
498 that header has no ``charset`` parameter, *failobj* is returned.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000499
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000500 Note that this method differs from :meth:`get_charset` which returns the
Georg Brandlb48327a2009-04-13 13:13:25 +0000501 :class:`~email.charset.Charset` instance for the default encoding of the message body.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000502
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000503 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000504
Georg Brandl8ec7f652007-08-15 14:28:01 +0000505
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000506 .. method:: get_charsets([failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000507
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000508 Return a list containing the character set names in the message. If the
509 message is a :mimetype:`multipart`, then the list will contain one element
510 for each subpart in the payload, otherwise, it will be a list of length 1.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000511
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000512 Each item in the list will be a string which is the value of the
513 ``charset`` parameter in the :mailheader:`Content-Type` header for the
514 represented subpart. However, if the subpart has no
515 :mailheader:`Content-Type` header, no ``charset`` parameter, or is not of
516 the :mimetype:`text` main MIME type, then that item in the returned list
517 will be *failobj*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000518
Georg Brandl8ec7f652007-08-15 14:28:01 +0000519
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000520 .. method:: walk()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000521
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000522 The :meth:`walk` method is an all-purpose generator which can be used to
523 iterate over all the parts and subparts of a message object tree, in
524 depth-first traversal order. You will typically use :meth:`walk` as the
525 iterator in a ``for`` loop; each iteration returns the next subpart.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000526
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000527 Here's an example that prints the MIME type of every part of a multipart
528 message structure::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000529
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000530 >>> for part in msg.walk():
531 ... print part.get_content_type()
532 multipart/report
533 text/plain
534 message/delivery-status
535 text/plain
536 text/plain
537 message/rfc822
Georg Brandl8ec7f652007-08-15 14:28:01 +0000538
539 .. versionchanged:: 2.5
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000540 The previously deprecated methods :meth:`get_type`, :meth:`get_main_type`, and
541 :meth:`get_subtype` were removed.
542
543 :class:`Message` objects can also optionally contain two instance attributes,
544 which can be used when generating the plain text of a MIME message.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000545
546
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000547 .. attribute:: preamble
Georg Brandl8ec7f652007-08-15 14:28:01 +0000548
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000549 The format of a MIME document allows for some text between the blank line
550 following the headers, and the first multipart boundary string. Normally,
551 this text is never visible in a MIME-aware mail reader because it falls
552 outside the standard MIME armor. However, when viewing the raw text of
553 the message, or when viewing the message in a non-MIME aware reader, this
554 text can become visible.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000555
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000556 The *preamble* attribute contains this leading extra-armor text for MIME
Georg Brandlb48327a2009-04-13 13:13:25 +0000557 documents. When the :class:`~email.parser.Parser` discovers some text
558 after the headers but before the first boundary string, it assigns this
559 text to the message's *preamble* attribute. When the
560 :class:`~email.generator.Generator` is writing out the plain text
561 representation of a MIME message, and it finds the
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000562 message has a *preamble* attribute, it will write this text in the area
563 between the headers and the first boundary. See :mod:`email.parser` and
564 :mod:`email.generator` for details.
565
566 Note that if the message object has no preamble, the *preamble* attribute
567 will be ``None``.
568
569
570 .. attribute:: epilogue
571
572 The *epilogue* attribute acts the same way as the *preamble* attribute,
573 except that it contains text that appears between the last boundary and
574 the end of the message.
575
576 .. versionchanged:: 2.5
577 You do not need to set the epilogue to the empty string in order for the
578 :class:`Generator` to print a newline at the end of the file.
579
580
581 .. attribute:: defects
582
583 The *defects* attribute contains a list of all the problems found when
584 parsing this message. See :mod:`email.errors` for a detailed description
585 of the possible parsing defects.
586
587 .. versionadded:: 2.4
Georg Brandl8ec7f652007-08-15 14:28:01 +0000588