blob: 0e79b6c8a44c4c763a2a12dc7e46a0cebaed7c23 [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.
43 *unixfrom* defaults to ``False``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000044
Benjamin Petersonc7b05922008-04-25 01:29:10 +000045 Note that this method is provided as a convenience and may not always
46 format the message the way you want. For example, by default it mangles
47 lines that begin with ``From``. For more flexibility, instantiate a
Georg Brandlb48327a2009-04-13 13:13:25 +000048 :class:`~email.generator.Generator` instance and use its :meth:`flatten`
49 method directly. For example::
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
Benjamin Petersonc7b05922008-04-25 01:29:10 +000051 from cStringIO import StringIO
52 from email.generator import Generator
53 fp = StringIO()
54 g = Generator(fp, mangle_from_=False, maxheaderlen=60)
55 g.flatten(msg)
56 text = fp.getvalue()
Georg Brandl8ec7f652007-08-15 14:28:01 +000057
58
Benjamin Petersonc7b05922008-04-25 01:29:10 +000059 .. method:: __str__()
Georg Brandl8ec7f652007-08-15 14:28:01 +000060
Benjamin Petersonc7b05922008-04-25 01:29:10 +000061 Equivalent to ``as_string(unixfrom=True)``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
63
Benjamin Petersonc7b05922008-04-25 01:29:10 +000064 .. method:: is_multipart()
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
Benjamin Petersonc7b05922008-04-25 01:29:10 +000066 Return ``True`` if the message's payload is a list of sub-\
67 :class:`Message` objects, otherwise return ``False``. When
68 :meth:`is_multipart` returns False, the payload should be a string object.
Georg Brandl8ec7f652007-08-15 14:28:01 +000069
70
Benjamin Petersonc7b05922008-04-25 01:29:10 +000071 .. method:: set_unixfrom(unixfrom)
Georg Brandl8ec7f652007-08-15 14:28:01 +000072
Benjamin Petersonc7b05922008-04-25 01:29:10 +000073 Set the message's envelope header to *unixfrom*, which should be a string.
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
75
Benjamin Petersonc7b05922008-04-25 01:29:10 +000076 .. method:: get_unixfrom()
Georg Brandl8ec7f652007-08-15 14:28:01 +000077
Benjamin Petersonc7b05922008-04-25 01:29:10 +000078 Return the message's envelope header. Defaults to ``None`` if the
79 envelope header was never set.
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
81
Benjamin Petersonc7b05922008-04-25 01:29:10 +000082 .. method:: attach(payload)
Georg Brandl8ec7f652007-08-15 14:28:01 +000083
Benjamin Petersonc7b05922008-04-25 01:29:10 +000084 Add the given *payload* to the current payload, which must be ``None`` or
85 a list of :class:`Message` objects before the call. After the call, the
86 payload will always be a list of :class:`Message` objects. If you want to
87 set the payload to a scalar object (e.g. a string), use
88 :meth:`set_payload` instead.
Georg Brandl8ec7f652007-08-15 14:28:01 +000089
90
Benjamin Petersonc7b05922008-04-25 01:29:10 +000091 .. method:: get_payload([i[, decode]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000092
Andrew M. Kuchlingcd48d8a2008-07-26 13:09:06 +000093 Return the current payload, which will be a list of
Benjamin Petersonc7b05922008-04-25 01:29:10 +000094 :class:`Message` objects when :meth:`is_multipart` is ``True``, or a
95 string when :meth:`is_multipart` is ``False``. If the payload is a list
96 and you mutate the list object, you modify the message's payload in place.
Georg Brandl8ec7f652007-08-15 14:28:01 +000097
Benjamin Petersonc7b05922008-04-25 01:29:10 +000098 With optional argument *i*, :meth:`get_payload` will return the *i*-th
99 element of the payload, counting from zero, if :meth:`is_multipart` is
100 ``True``. An :exc:`IndexError` will be raised if *i* is less than 0 or
101 greater than or equal to the number of items in the payload. If the
102 payload is a string (i.e. :meth:`is_multipart` is ``False``) and *i* is
103 given, a :exc:`TypeError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000105 Optional *decode* is a flag indicating whether the payload should be
106 decoded or not, according to the :mailheader:`Content-Transfer-Encoding`
107 header. When ``True`` and the message is not a multipart, the payload will
108 be decoded if this header's value is ``quoted-printable`` or ``base64``.
109 If some other encoding is used, or :mailheader:`Content-Transfer-Encoding`
110 header is missing, or if the payload has bogus base64 data, the payload is
111 returned as-is (undecoded). If the message is a multipart and the
112 *decode* flag is ``True``, then ``None`` is returned. The default for
113 *decode* is ``False``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000114
115
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000116 .. method:: set_payload(payload[, charset])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000118 Set the entire message object's payload to *payload*. It is the client's
119 responsibility to ensure the payload invariants. Optional *charset* sets
120 the message's default character set; see :meth:`set_charset` for details.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000121
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000122 .. versionchanged:: 2.2.2
123 *charset* argument added.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124
125
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000126 .. method:: set_charset(charset)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000127
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000128 Set the character set of the payload to *charset*, which can either be a
Georg Brandlb48327a2009-04-13 13:13:25 +0000129 :class:`~email.charset.Charset` instance (see :mod:`email.charset`), a
130 string naming a character set, or ``None``. If it is a string, it will
131 be converted to a :class:`~email.charset.Charset` instance. If *charset*
132 is ``None``, the ``charset`` parameter will be removed from the
133 :mailheader:`Content-Type` header. Anything else will generate a
134 :exc:`TypeError`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000135
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000136 The message will be assumed to be of type :mimetype:`text/\*` encoded with
137 *charset.input_charset*. It will be converted to *charset.output_charset*
138 and encoded properly, if needed, when generating the plain text
139 representation of the message. MIME headers (:mailheader:`MIME-Version`,
140 :mailheader:`Content-Type`, :mailheader:`Content-Transfer-Encoding`) will
141 be added as needed.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000142
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000143 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000144
145
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000146 .. method:: get_charset()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000147
Georg Brandlb48327a2009-04-13 13:13:25 +0000148 Return the :class:`~email.charset.Charset` instance associated with the
149 message's payload.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000150
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000151 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000152
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000153 The following methods implement a mapping-like interface for accessing the
154 message's :rfc:`2822` headers. Note that there are some semantic differences
155 between these methods and a normal mapping (i.e. dictionary) interface. For
156 example, in a dictionary there are no duplicate keys, but here there may be
157 duplicate message headers. Also, in dictionaries there is no guaranteed
158 order to the keys returned by :meth:`keys`, but in a :class:`Message` object,
159 headers are always returned in the order they appeared in the original
160 message, or were added to the message later. Any header deleted and then
161 re-added are always appended to the end of the header list.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000162
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000163 These semantic differences are intentional and are biased toward maximal
164 convenience.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000165
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000166 Note that in all cases, any envelope header present in the message is not
167 included in the mapping interface.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000168
169
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000170 .. method:: __len__()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000171
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000172 Return the total number of headers, including duplicates.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000173
174
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000175 .. method:: __contains__(name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000176
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000177 Return true if the message object has a field named *name*. Matching is
178 done case-insensitively and *name* should not include the trailing colon.
179 Used for the ``in`` operator, e.g.::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000180
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000181 if 'message-id' in myMessage:
182 print 'Message-ID:', myMessage['message-id']
Georg Brandl8ec7f652007-08-15 14:28:01 +0000183
184
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000185 .. method:: __getitem__(name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000186
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000187 Return the value of the named header field. *name* should not include the
188 colon field separator. If the header is missing, ``None`` is returned; a
189 :exc:`KeyError` is never raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000190
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000191 Note that if the named field appears more than once in the message's
192 headers, exactly which of those field values will be returned is
193 undefined. Use the :meth:`get_all` method to get the values of all the
194 extant named headers.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000195
196
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000197 .. method:: __setitem__(name, val)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000198
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000199 Add a header to the message with field name *name* and value *val*. The
200 field is appended to the end of the message's existing fields.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000201
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000202 Note that this does *not* overwrite or delete any existing header with the same
203 name. If you want to ensure that the new header is the only one present in the
204 message with field name *name*, delete the field first, e.g.::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000205
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000206 del msg['subject']
207 msg['subject'] = 'Python roolz!'
Georg Brandl8ec7f652007-08-15 14:28:01 +0000208
209
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000210 .. method:: __delitem__(name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000211
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000212 Delete all occurrences of the field with name *name* from the message's
213 headers. No exception is raised if the named field isn't present in the headers.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000214
215
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000216 .. method:: has_key(name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000217
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000218 Return true if the message contains a header field named *name*, otherwise
219 return false.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000220
221
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000222 .. method:: keys()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000223
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000224 Return a list of all the message's header field names.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000225
226
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000227 .. method:: values()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000228
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000229 Return a list of all the message's field values.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000230
231
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000232 .. method:: items()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000233
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000234 Return a list of 2-tuples containing all the message's field headers and
235 values.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000236
237
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000238 .. method:: get(name[, failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000239
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000240 Return the value of the named header field. This is identical to
241 :meth:`__getitem__` except that optional *failobj* is returned if the
242 named header is missing (defaults to ``None``).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000243
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000244 Here are some additional useful methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000245
246
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000247 .. method:: get_all(name[, failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000248
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000249 Return a list of all the values for the field named *name*. If there are
250 no such named headers in the message, *failobj* is returned (defaults to
251 ``None``).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000252
253
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000254 .. method:: add_header(_name, _value, **_params)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000255
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000256 Extended header setting. This method is similar to :meth:`__setitem__`
257 except that additional header parameters can be provided as keyword
258 arguments. *_name* is the header field to add and *_value* is the
259 *primary* value for the header.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000260
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000261 For each item in the keyword argument dictionary *_params*, the key is
262 taken as the parameter name, with underscores converted to dashes (since
263 dashes are illegal in Python identifiers). Normally, the parameter will
264 be added as ``key="value"`` unless the value is ``None``, in which case
265 only the key will be added.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000266
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000267 Here's an example::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000268
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000269 msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000270
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000271 This will add a header that looks like ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000272
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000273 Content-Disposition: attachment; filename="bud.gif"
Georg Brandl8ec7f652007-08-15 14:28:01 +0000274
275
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000276 .. method:: replace_header(_name, _value)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000277
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000278 Replace a header. Replace the first header found in the message that
279 matches *_name*, retaining header order and field name case. If no
280 matching header was found, a :exc:`KeyError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000281
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000282 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000283
284
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000285 .. method:: get_content_type()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000286
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000287 Return the message's content type. The returned string is coerced to
288 lower case of the form :mimetype:`maintype/subtype`. If there was no
289 :mailheader:`Content-Type` header in the message the default type as given
290 by :meth:`get_default_type` will be returned. Since according to
291 :rfc:`2045`, messages always have a default type, :meth:`get_content_type`
292 will always return a value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000293
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000294 :rfc:`2045` defines a message's default type to be :mimetype:`text/plain`
295 unless it appears inside a :mimetype:`multipart/digest` container, in
296 which case it would be :mimetype:`message/rfc822`. If the
297 :mailheader:`Content-Type` header has an invalid type specification,
298 :rfc:`2045` mandates that the default type be :mimetype:`text/plain`.
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
Georg Brandl8ec7f652007-08-15 14:28:01 +0000302
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000303 .. method:: get_content_maintype()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000304
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000305 Return the message's main content type. This is the :mimetype:`maintype`
306 part of the string returned by :meth:`get_content_type`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000307
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000308 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000309
Georg Brandl8ec7f652007-08-15 14:28:01 +0000310
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000311 .. method:: get_content_subtype()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000312
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000313 Return the message's sub-content type. This is the :mimetype:`subtype`
314 part of the string returned by :meth:`get_content_type`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000315
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000316 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000317
Georg Brandl8ec7f652007-08-15 14:28:01 +0000318
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000319 .. method:: get_default_type()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000320
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000321 Return the default content type. Most messages have a default content
322 type of :mimetype:`text/plain`, except for messages that are subparts of
323 :mimetype:`multipart/digest` containers. Such subparts have a default
324 content type of :mimetype:`message/rfc822`.
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:: set_default_type(ctype)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000330
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000331 Set the default content type. *ctype* should either be
332 :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is not
333 enforced. The default content type is not stored in the
334 :mailheader:`Content-Type` header.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000335
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000336 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000337
Georg Brandl8ec7f652007-08-15 14:28:01 +0000338
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000339 .. method:: get_params([failobj[, header[, unquote]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000340
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000341 Return the message's :mailheader:`Content-Type` parameters, as a list.
342 The elements of the returned list are 2-tuples of key/value pairs, as
343 split on the ``'='`` sign. The left hand side of the ``'='`` is the key,
344 while the right hand side is the value. If there is no ``'='`` sign in
345 the parameter the value is the empty string, otherwise the value is as
346 described in :meth:`get_param` and is unquoted if optional *unquote* is
347 ``True`` (the default).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000348
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000349 Optional *failobj* is the object to return if there is no
350 :mailheader:`Content-Type` header. Optional *header* is the header to
351 search instead of :mailheader:`Content-Type`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000352
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000353 .. versionchanged:: 2.2.2
354 *unquote* argument added.
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_param(param[, failobj[, header[, unquote]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000358
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000359 Return the value of the :mailheader:`Content-Type` header's parameter
360 *param* as a string. If the message has no :mailheader:`Content-Type`
361 header or if there is no such parameter, then *failobj* is returned
362 (defaults to ``None``).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000363
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000364 Optional *header* if given, specifies the message header to use instead of
365 :mailheader:`Content-Type`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000366
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000367 Parameter keys are always compared case insensitively. The return value
368 can either be a string, or a 3-tuple if the parameter was :rfc:`2231`
369 encoded. When it's a 3-tuple, the elements of the value are of the form
370 ``(CHARSET, LANGUAGE, VALUE)``. Note that both ``CHARSET`` and
371 ``LANGUAGE`` can be ``None``, in which case you should consider ``VALUE``
372 to be encoded in the ``us-ascii`` charset. You can usually ignore
373 ``LANGUAGE``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000374
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000375 If your application doesn't care whether the parameter was encoded as in
376 :rfc:`2231`, you can collapse the parameter value by calling
Hirokazu Yamamoto3bd40582009-04-13 01:07:06 +0000377 :func:`email.utils.collapse_rfc2231_value`, passing in the return value
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000378 from :meth:`get_param`. This will return a suitably decoded Unicode
379 string whn the value is a tuple, or the original string unquoted if it
380 isn't. For example::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000381
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000382 rawparam = msg.get_param('foo')
Hirokazu Yamamoto3bd40582009-04-13 01:07:06 +0000383 param = email.utils.collapse_rfc2231_value(rawparam)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000384
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000385 In any case, the parameter value (either the returned string, or the
386 ``VALUE`` item in the 3-tuple) is always unquoted, unless *unquote* is set
387 to ``False``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000388
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000389 .. versionchanged:: 2.2.2
390 *unquote* argument added, and 3-tuple return value possible.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000391
392
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000393 .. method:: set_param(param, value[, header[, requote[, charset[, language]]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000394
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000395 Set a parameter in the :mailheader:`Content-Type` header. If the
396 parameter already exists in the header, its value will be replaced with
397 *value*. If the :mailheader:`Content-Type` header as not yet been defined
398 for this message, it will be set to :mimetype:`text/plain` and the new
399 parameter value will be appended as per :rfc:`2045`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000400
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000401 Optional *header* specifies an alternative header to
402 :mailheader:`Content-Type`, and all parameters will be quoted as necessary
403 unless optional *requote* is ``False`` (the default is ``True``).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000404
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000405 If optional *charset* is specified, the parameter will be encoded
406 according to :rfc:`2231`. Optional *language* specifies the RFC 2231
407 language, defaulting to the empty string. Both *charset* and *language*
408 should be strings.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000409
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000410 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000411
412
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000413 .. method:: del_param(param[, header[, requote]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000414
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000415 Remove the given parameter completely from the :mailheader:`Content-Type`
416 header. The header will be re-written in place without the parameter or
417 its value. All values will be quoted as necessary unless *requote* is
418 ``False`` (the default is ``True``). Optional *header* specifies an
419 alternative to :mailheader:`Content-Type`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000420
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000421 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000422
Georg Brandl8ec7f652007-08-15 14:28:01 +0000423
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000424 .. method:: set_type(type[, header][, requote])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000425
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000426 Set the main type and subtype for the :mailheader:`Content-Type`
427 header. *type* must be a string in the form :mimetype:`maintype/subtype`,
428 otherwise a :exc:`ValueError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000429
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000430 This method replaces the :mailheader:`Content-Type` header, keeping all
431 the parameters in place. If *requote* is ``False``, this leaves the
432 existing header's quoting as is, otherwise the parameters will be quoted
433 (the default).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000434
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000435 An alternative header can be specified in the *header* argument. When the
436 :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version`
437 header is also added.
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
441
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000442 .. method:: get_filename([failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000443
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000444 Return the value of the ``filename`` parameter of the
445 :mailheader:`Content-Disposition` header of the message. If the header
446 does not have a ``filename`` parameter, this method falls back to looking
447 for the ``name`` parameter. If neither is found, or the header is
448 missing, then *failobj* is returned. The returned string will always be
Hirokazu Yamamoto45857462009-04-13 01:21:56 +0000449 unquoted as per :func:`email.utils.unquote`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000450
Georg Brandl8ec7f652007-08-15 14:28:01 +0000451
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000452 .. method:: get_boundary([failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000453
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000454 Return the value of the ``boundary`` parameter of the
455 :mailheader:`Content-Type` header of the message, or *failobj* if either
456 the header is missing, or has no ``boundary`` parameter. The returned
Hirokazu Yamamoto45857462009-04-13 01:21:56 +0000457 string will always be unquoted as per :func:`email.utils.unquote`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000458
Georg Brandl8ec7f652007-08-15 14:28:01 +0000459
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000460 .. method:: set_boundary(boundary)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000461
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000462 Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
463 *boundary*. :meth:`set_boundary` will always quote *boundary* if
464 necessary. A :exc:`HeaderParseError` is raised if the message object has
465 no :mailheader:`Content-Type` header.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000466
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000467 Note that using this method is subtly different than deleting the old
468 :mailheader:`Content-Type` header and adding a new one with the new
469 boundary via :meth:`add_header`, because :meth:`set_boundary` preserves
470 the order of the :mailheader:`Content-Type` header in the list of
471 headers. However, it does *not* preserve any continuation lines which may
472 have been present in the original :mailheader:`Content-Type` header.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000473
Georg Brandl8ec7f652007-08-15 14:28:01 +0000474
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000475 .. method:: get_content_charset([failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000476
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000477 Return the ``charset`` parameter of the :mailheader:`Content-Type` header,
478 coerced to lower case. If there is no :mailheader:`Content-Type` header, or if
479 that header has no ``charset`` parameter, *failobj* is returned.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000480
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000481 Note that this method differs from :meth:`get_charset` which returns the
Georg Brandlb48327a2009-04-13 13:13:25 +0000482 :class:`~email.charset.Charset` instance for the default encoding of the message body.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000483
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000484 .. versionadded:: 2.2.2
Georg Brandl8ec7f652007-08-15 14:28:01 +0000485
Georg Brandl8ec7f652007-08-15 14:28:01 +0000486
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000487 .. method:: get_charsets([failobj])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000488
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000489 Return a list containing the character set names in the message. If the
490 message is a :mimetype:`multipart`, then the list will contain one element
491 for each subpart in the payload, otherwise, it will be a list of length 1.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000492
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000493 Each item in the list will be a string which is the value of the
494 ``charset`` parameter in the :mailheader:`Content-Type` header for the
495 represented subpart. However, if the subpart has no
496 :mailheader:`Content-Type` header, no ``charset`` parameter, or is not of
497 the :mimetype:`text` main MIME type, then that item in the returned list
498 will be *failobj*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000499
Georg Brandl8ec7f652007-08-15 14:28:01 +0000500
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000501 .. method:: walk()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000502
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000503 The :meth:`walk` method is an all-purpose generator which can be used to
504 iterate over all the parts and subparts of a message object tree, in
505 depth-first traversal order. You will typically use :meth:`walk` as the
506 iterator in a ``for`` loop; each iteration returns the next subpart.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000507
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000508 Here's an example that prints the MIME type of every part of a multipart
509 message structure::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000510
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000511 >>> for part in msg.walk():
512 ... print part.get_content_type()
513 multipart/report
514 text/plain
515 message/delivery-status
516 text/plain
517 text/plain
518 message/rfc822
Georg Brandl8ec7f652007-08-15 14:28:01 +0000519
520 .. versionchanged:: 2.5
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000521 The previously deprecated methods :meth:`get_type`, :meth:`get_main_type`, and
522 :meth:`get_subtype` were removed.
523
524 :class:`Message` objects can also optionally contain two instance attributes,
525 which can be used when generating the plain text of a MIME message.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000526
527
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000528 .. attribute:: preamble
Georg Brandl8ec7f652007-08-15 14:28:01 +0000529
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000530 The format of a MIME document allows for some text between the blank line
531 following the headers, and the first multipart boundary string. Normally,
532 this text is never visible in a MIME-aware mail reader because it falls
533 outside the standard MIME armor. However, when viewing the raw text of
534 the message, or when viewing the message in a non-MIME aware reader, this
535 text can become visible.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000536
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000537 The *preamble* attribute contains this leading extra-armor text for MIME
Georg Brandlb48327a2009-04-13 13:13:25 +0000538 documents. When the :class:`~email.parser.Parser` discovers some text
539 after the headers but before the first boundary string, it assigns this
540 text to the message's *preamble* attribute. When the
541 :class:`~email.generator.Generator` is writing out the plain text
542 representation of a MIME message, and it finds the
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000543 message has a *preamble* attribute, it will write this text in the area
544 between the headers and the first boundary. See :mod:`email.parser` and
545 :mod:`email.generator` for details.
546
547 Note that if the message object has no preamble, the *preamble* attribute
548 will be ``None``.
549
550
551 .. attribute:: epilogue
552
553 The *epilogue* attribute acts the same way as the *preamble* attribute,
554 except that it contains text that appears between the last boundary and
555 the end of the message.
556
557 .. versionchanged:: 2.5
558 You do not need to set the epilogue to the empty string in order for the
559 :class:`Generator` to print a newline at the end of the file.
560
561
562 .. attribute:: defects
563
564 The *defects* attribute contains a list of all the problems found when
565 parsing this message. See :mod:`email.errors` for a detailed description
566 of the possible parsing defects.
567
568 .. versionadded:: 2.4
Georg Brandl8ec7f652007-08-15 14:28:01 +0000569