blob: 5344b454c38335f3baf00fbced61dbe4b350bf21 [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
Benjamin Petersone41251e2008-04-25 01:59:09 +000039 .. method:: as_string([unixfrom])
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.
43 *unixfrom* defaults to ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +000044
Benjamin Petersone41251e2008-04-25 01:59:09 +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
48 :class:`Generator` instance and use its :meth:`flatten` method directly.
49 For example::
Georg Brandl116aa622007-08-15 14:28:22 +000050
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +000057
58
Benjamin Petersone41251e2008-04-25 01:59:09 +000059 .. method:: __str__()
Georg Brandl116aa622007-08-15 14:28:22 +000060
Benjamin Petersone41251e2008-04-25 01:59:09 +000061 Equivalent to ``as_string(unixfrom=True)``.
Georg Brandl116aa622007-08-15 14:28:22 +000062
63
Benjamin Petersone41251e2008-04-25 01:59:09 +000064 .. method:: is_multipart()
Georg Brandl116aa622007-08-15 14:28:22 +000065
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +000069
70
Benjamin Petersone41251e2008-04-25 01:59:09 +000071 .. method:: set_unixfrom(unixfrom)
Georg Brandl116aa622007-08-15 14:28:22 +000072
Benjamin Petersone41251e2008-04-25 01:59:09 +000073 Set the message's envelope header to *unixfrom*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +000074
75
Benjamin Petersone41251e2008-04-25 01:59:09 +000076 .. method:: get_unixfrom()
Georg Brandl116aa622007-08-15 14:28:22 +000077
Benjamin Petersone41251e2008-04-25 01:59:09 +000078 Return the message's envelope header. Defaults to ``None`` if the
79 envelope header was never set.
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Benjamin Petersone41251e2008-04-25 01:59:09 +000082 .. method:: attach(payload)
Georg Brandl116aa622007-08-15 14:28:22 +000083
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +000089
90
Benjamin Petersone41251e2008-04-25 01:59:09 +000091 .. method:: get_payload([i[, decode]])
Georg Brandl116aa622007-08-15 14:28:22 +000092
Benjamin Petersone41251e2008-04-25 01:59:09 +000093 Return a reference the current payload, which will be a list of
94 :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 Brandl116aa622007-08-15 14:28:22 +000097
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +0000104
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +0000114
115
Benjamin Petersone41251e2008-04-25 01:59:09 +0000116 .. method:: set_payload(payload[, charset])
Georg Brandl116aa622007-08-15 14:28:22 +0000117
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +0000121
Benjamin Petersone41251e2008-04-25 01:59:09 +0000122 .. method:: set_charset(charset)
Georg Brandl116aa622007-08-15 14:28:22 +0000123
Benjamin Petersone41251e2008-04-25 01:59:09 +0000124 Set the character set of the payload to *charset*, which can either be a
125 :class:`Charset` instance (see :mod:`email.charset`), a string naming a
126 character set, or ``None``. If it is a string, it will be converted to a
127 :class:`Charset` instance. If *charset* is ``None``, the ``charset``
128 parameter will be removed from the :mailheader:`Content-Type`
129 header. Anything else will generate a :exc:`TypeError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Benjamin Petersone41251e2008-04-25 01:59:09 +0000131 The message will be assumed to be of type :mimetype:`text/\*` encoded with
132 *charset.input_charset*. It will be converted to *charset.output_charset*
133 and encoded properly, if needed, when generating the plain text
134 representation of the message. MIME headers (:mailheader:`MIME-Version`,
135 :mailheader:`Content-Type`, :mailheader:`Content-Transfer-Encoding`) will
136 be added as needed.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Benjamin Petersone41251e2008-04-25 01:59:09 +0000138 .. method:: get_charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000139
Benjamin Petersone41251e2008-04-25 01:59:09 +0000140 Return the :class:`Charset` instance associated with the message's
141 payload.
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Benjamin Petersone41251e2008-04-25 01:59:09 +0000143 The following methods implement a mapping-like interface for accessing the
144 message's :rfc:`2822` headers. Note that there are some semantic differences
145 between these methods and a normal mapping (i.e. dictionary) interface. For
146 example, in a dictionary there are no duplicate keys, but here there may be
147 duplicate message headers. Also, in dictionaries there is no guaranteed
148 order to the keys returned by :meth:`keys`, but in a :class:`Message` object,
149 headers are always returned in the order they appeared in the original
150 message, or were added to the message later. Any header deleted and then
151 re-added are always appended to the end of the header list.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
Benjamin Petersone41251e2008-04-25 01:59:09 +0000153 These semantic differences are intentional and are biased toward maximal
154 convenience.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Benjamin Petersone41251e2008-04-25 01:59:09 +0000156 Note that in all cases, any envelope header present in the message is not
157 included in the mapping interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000158
159
Benjamin Petersone41251e2008-04-25 01:59:09 +0000160 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000161
Benjamin Petersone41251e2008-04-25 01:59:09 +0000162 Return the total number of headers, including duplicates.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
164
Benjamin Petersone41251e2008-04-25 01:59:09 +0000165 .. method:: __contains__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Benjamin Petersone41251e2008-04-25 01:59:09 +0000167 Return true if the message object has a field named *name*. Matching is
168 done case-insensitively and *name* should not include the trailing colon.
169 Used for the ``in`` operator, e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000170
Benjamin Petersone41251e2008-04-25 01:59:09 +0000171 if 'message-id' in myMessage:
172 print('Message-ID:', myMessage['message-id'])
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174
Benjamin Petersone41251e2008-04-25 01:59:09 +0000175 .. method:: __getitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000176
Benjamin Petersone41251e2008-04-25 01:59:09 +0000177 Return the value of the named header field. *name* should not include the
178 colon field separator. If the header is missing, ``None`` is returned; a
179 :exc:`KeyError` is never raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000180
Benjamin Petersone41251e2008-04-25 01:59:09 +0000181 Note that if the named field appears more than once in the message's
182 headers, exactly which of those field values will be returned is
183 undefined. Use the :meth:`get_all` method to get the values of all the
184 extant named headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000185
186
Benjamin Petersone41251e2008-04-25 01:59:09 +0000187 .. method:: __setitem__(name, val)
Georg Brandl116aa622007-08-15 14:28:22 +0000188
Benjamin Petersone41251e2008-04-25 01:59:09 +0000189 Add a header to the message with field name *name* and value *val*. The
190 field is appended to the end of the message's existing fields.
Georg Brandl116aa622007-08-15 14:28:22 +0000191
Benjamin Petersone41251e2008-04-25 01:59:09 +0000192 Note that this does *not* overwrite or delete any existing header with the same
193 name. If you want to ensure that the new header is the only one present in the
194 message with field name *name*, delete the field first, e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000195
Benjamin Petersone41251e2008-04-25 01:59:09 +0000196 del msg['subject']
197 msg['subject'] = 'Python roolz!'
Georg Brandl116aa622007-08-15 14:28:22 +0000198
199
Benjamin Petersone41251e2008-04-25 01:59:09 +0000200 .. method:: __delitem__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000201
Benjamin Petersone41251e2008-04-25 01:59:09 +0000202 Delete all occurrences of the field with name *name* from the message's
203 headers. No exception is raised if the named field isn't present in the headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000204
205
Benjamin Petersone41251e2008-04-25 01:59:09 +0000206 .. method:: Message.__contains__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000207
Benjamin Petersone41251e2008-04-25 01:59:09 +0000208 Return true if the message contains a header field named *name*, otherwise
209 return false.
Georg Brandl116aa622007-08-15 14:28:22 +0000210
211
Benjamin Petersone41251e2008-04-25 01:59:09 +0000212 .. method:: keys()
Georg Brandl116aa622007-08-15 14:28:22 +0000213
Benjamin Petersone41251e2008-04-25 01:59:09 +0000214 Return a list of all the message's header field names.
Georg Brandl116aa622007-08-15 14:28:22 +0000215
216
Benjamin Petersone41251e2008-04-25 01:59:09 +0000217 .. method:: values()
Georg Brandl116aa622007-08-15 14:28:22 +0000218
Benjamin Petersone41251e2008-04-25 01:59:09 +0000219 Return a list of all the message's field values.
Georg Brandl116aa622007-08-15 14:28:22 +0000220
221
Benjamin Petersone41251e2008-04-25 01:59:09 +0000222 .. method:: items()
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Benjamin Petersone41251e2008-04-25 01:59:09 +0000224 Return a list of 2-tuples containing all the message's field headers and
225 values.
Georg Brandl116aa622007-08-15 14:28:22 +0000226
227
Benjamin Petersone41251e2008-04-25 01:59:09 +0000228 .. method:: get(name[, failobj])
Georg Brandl116aa622007-08-15 14:28:22 +0000229
Benjamin Petersone41251e2008-04-25 01:59:09 +0000230 Return the value of the named header field. This is identical to
231 :meth:`__getitem__` except that optional *failobj* is returned if the
232 named header is missing (defaults to ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000233
Benjamin Petersone41251e2008-04-25 01:59:09 +0000234 Here are some additional useful methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000235
236
Benjamin Petersone41251e2008-04-25 01:59:09 +0000237 .. method:: get_all(name[, failobj])
Georg Brandl116aa622007-08-15 14:28:22 +0000238
Benjamin Petersone41251e2008-04-25 01:59:09 +0000239 Return a list of all the values for the field named *name*. If there are
240 no such named headers in the message, *failobj* is returned (defaults to
241 ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000242
243
Benjamin Petersone41251e2008-04-25 01:59:09 +0000244 .. method:: add_header(_name, _value, **_params)
Georg Brandl116aa622007-08-15 14:28:22 +0000245
Benjamin Petersone41251e2008-04-25 01:59:09 +0000246 Extended header setting. This method is similar to :meth:`__setitem__`
247 except that additional header parameters can be provided as keyword
248 arguments. *_name* is the header field to add and *_value* is the
249 *primary* value for the header.
Georg Brandl116aa622007-08-15 14:28:22 +0000250
Benjamin Petersone41251e2008-04-25 01:59:09 +0000251 For each item in the keyword argument dictionary *_params*, the key is
252 taken as the parameter name, with underscores converted to dashes (since
253 dashes are illegal in Python identifiers). Normally, the parameter will
254 be added as ``key="value"`` unless the value is ``None``, in which case
255 only the key will be added.
Georg Brandl116aa622007-08-15 14:28:22 +0000256
Benjamin Petersone41251e2008-04-25 01:59:09 +0000257 Here's an example::
Georg Brandl116aa622007-08-15 14:28:22 +0000258
Benjamin Petersone41251e2008-04-25 01:59:09 +0000259 msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
Georg Brandl116aa622007-08-15 14:28:22 +0000260
Benjamin Petersone41251e2008-04-25 01:59:09 +0000261 This will add a header that looks like ::
Georg Brandl116aa622007-08-15 14:28:22 +0000262
Benjamin Petersone41251e2008-04-25 01:59:09 +0000263 Content-Disposition: attachment; filename="bud.gif"
Georg Brandl116aa622007-08-15 14:28:22 +0000264
265
Benjamin Petersone41251e2008-04-25 01:59:09 +0000266 .. method:: replace_header(_name, _value)
Georg Brandl116aa622007-08-15 14:28:22 +0000267
Benjamin Petersone41251e2008-04-25 01:59:09 +0000268 Replace a header. Replace the first header found in the message that
269 matches *_name*, retaining header order and field name case. If no
270 matching header was found, a :exc:`KeyError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000271
Georg Brandl116aa622007-08-15 14:28:22 +0000272
Benjamin Petersone41251e2008-04-25 01:59:09 +0000273 .. method:: get_content_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Benjamin Petersone41251e2008-04-25 01:59:09 +0000275 Return the message's content type. The returned string is coerced to
276 lower case of the form :mimetype:`maintype/subtype`. If there was no
277 :mailheader:`Content-Type` header in the message the default type as given
278 by :meth:`get_default_type` will be returned. Since according to
279 :rfc:`2045`, messages always have a default type, :meth:`get_content_type`
280 will always return a value.
Georg Brandl116aa622007-08-15 14:28:22 +0000281
Benjamin Petersone41251e2008-04-25 01:59:09 +0000282 :rfc:`2045` defines a message's default type to be :mimetype:`text/plain`
283 unless it appears inside a :mimetype:`multipart/digest` container, in
284 which case it would be :mimetype:`message/rfc822`. If the
285 :mailheader:`Content-Type` header has an invalid type specification,
286 :rfc:`2045` mandates that the default type be :mimetype:`text/plain`.
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Georg Brandl116aa622007-08-15 14:28:22 +0000288
Benjamin Petersone41251e2008-04-25 01:59:09 +0000289 .. method:: get_content_maintype()
Georg Brandl116aa622007-08-15 14:28:22 +0000290
Benjamin Petersone41251e2008-04-25 01:59:09 +0000291 Return the message's main content type. This is the :mimetype:`maintype`
292 part of the string returned by :meth:`get_content_type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000293
Georg Brandl116aa622007-08-15 14:28:22 +0000294
Benjamin Petersone41251e2008-04-25 01:59:09 +0000295 .. method:: get_content_subtype()
Georg Brandl116aa622007-08-15 14:28:22 +0000296
Benjamin Petersone41251e2008-04-25 01:59:09 +0000297 Return the message's sub-content type. This is the :mimetype:`subtype`
298 part of the string returned by :meth:`get_content_type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000299
Georg Brandl116aa622007-08-15 14:28:22 +0000300
Benjamin Petersone41251e2008-04-25 01:59:09 +0000301 .. method:: get_default_type()
Georg Brandl116aa622007-08-15 14:28:22 +0000302
Benjamin Petersone41251e2008-04-25 01:59:09 +0000303 Return the default content type. Most messages have a default content
304 type of :mimetype:`text/plain`, except for messages that are subparts of
305 :mimetype:`multipart/digest` containers. Such subparts have a default
306 content type of :mimetype:`message/rfc822`.
Georg Brandl116aa622007-08-15 14:28:22 +0000307
Georg Brandl116aa622007-08-15 14:28:22 +0000308
Benjamin Petersone41251e2008-04-25 01:59:09 +0000309 .. method:: set_default_type(ctype)
Georg Brandl116aa622007-08-15 14:28:22 +0000310
Benjamin Petersone41251e2008-04-25 01:59:09 +0000311 Set the default content type. *ctype* should either be
312 :mimetype:`text/plain` or :mimetype:`message/rfc822`, although this is not
313 enforced. The default content type is not stored in the
314 :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000315
Georg Brandl116aa622007-08-15 14:28:22 +0000316
Benjamin Petersone41251e2008-04-25 01:59:09 +0000317 .. method:: get_params([failobj[, header[, unquote]]])
Georg Brandl116aa622007-08-15 14:28:22 +0000318
Benjamin Petersone41251e2008-04-25 01:59:09 +0000319 Return the message's :mailheader:`Content-Type` parameters, as a list.
320 The elements of the returned list are 2-tuples of key/value pairs, as
321 split on the ``'='`` sign. The left hand side of the ``'='`` is the key,
322 while the right hand side is the value. If there is no ``'='`` sign in
323 the parameter the value is the empty string, otherwise the value is as
324 described in :meth:`get_param` and is unquoted if optional *unquote* is
325 ``True`` (the default).
Georg Brandl116aa622007-08-15 14:28:22 +0000326
Benjamin Petersone41251e2008-04-25 01:59:09 +0000327 Optional *failobj* is the object to return if there is no
328 :mailheader:`Content-Type` header. Optional *header* is the header to
329 search instead of :mailheader:`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_param(param[, failobj[, header[, unquote]]])
Georg Brandl116aa622007-08-15 14:28:22 +0000333
Benjamin Petersone41251e2008-04-25 01:59:09 +0000334 Return the value of the :mailheader:`Content-Type` header's parameter
335 *param* as a string. If the message has no :mailheader:`Content-Type`
336 header or if there is no such parameter, then *failobj* is returned
337 (defaults to ``None``).
Georg Brandl116aa622007-08-15 14:28:22 +0000338
Benjamin Petersone41251e2008-04-25 01:59:09 +0000339 Optional *header* if given, specifies the message header to use instead of
340 :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000341
Benjamin Petersone41251e2008-04-25 01:59:09 +0000342 Parameter keys are always compared case insensitively. The return value
343 can either be a string, or a 3-tuple if the parameter was :rfc:`2231`
344 encoded. When it's a 3-tuple, the elements of the value are of the form
345 ``(CHARSET, LANGUAGE, VALUE)``. Note that both ``CHARSET`` and
346 ``LANGUAGE`` can be ``None``, in which case you should consider ``VALUE``
347 to be encoded in the ``us-ascii`` charset. You can usually ignore
348 ``LANGUAGE``.
Georg Brandl116aa622007-08-15 14:28:22 +0000349
Benjamin Petersone41251e2008-04-25 01:59:09 +0000350 If your application doesn't care whether the parameter was encoded as in
351 :rfc:`2231`, you can collapse the parameter value by calling
352 :func:`email.Utils.collapse_rfc2231_value`, passing in the return value
353 from :meth:`get_param`. This will return a suitably decoded Unicode
354 string whn the value is a tuple, or the original string unquoted if it
355 isn't. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000356
Benjamin Petersone41251e2008-04-25 01:59:09 +0000357 rawparam = msg.get_param('foo')
358 param = email.Utils.collapse_rfc2231_value(rawparam)
Georg Brandl116aa622007-08-15 14:28:22 +0000359
Benjamin Petersone41251e2008-04-25 01:59:09 +0000360 In any case, the parameter value (either the returned string, or the
361 ``VALUE`` item in the 3-tuple) is always unquoted, unless *unquote* is set
362 to ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000363
Georg Brandl116aa622007-08-15 14:28:22 +0000364
Benjamin Petersone41251e2008-04-25 01:59:09 +0000365 .. method:: set_param(param, value[, header[, requote[, charset[, language]]]])
Georg Brandl116aa622007-08-15 14:28:22 +0000366
Benjamin Petersone41251e2008-04-25 01:59:09 +0000367 Set a parameter in the :mailheader:`Content-Type` header. If the
368 parameter already exists in the header, its value will be replaced with
369 *value*. If the :mailheader:`Content-Type` header as not yet been defined
370 for this message, it will be set to :mimetype:`text/plain` and the new
371 parameter value will be appended as per :rfc:`2045`.
Georg Brandl116aa622007-08-15 14:28:22 +0000372
Benjamin Petersone41251e2008-04-25 01:59:09 +0000373 Optional *header* specifies an alternative header to
374 :mailheader:`Content-Type`, and all parameters will be quoted as necessary
375 unless optional *requote* is ``False`` (the default is ``True``).
Georg Brandl116aa622007-08-15 14:28:22 +0000376
Benjamin Petersone41251e2008-04-25 01:59:09 +0000377 If optional *charset* is specified, the parameter will be encoded
378 according to :rfc:`2231`. Optional *language* specifies the RFC 2231
379 language, defaulting to the empty string. Both *charset* and *language*
380 should be strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000381
Georg Brandl116aa622007-08-15 14:28:22 +0000382
Benjamin Petersone41251e2008-04-25 01:59:09 +0000383 .. method:: del_param(param[, header[, requote]])
Georg Brandl116aa622007-08-15 14:28:22 +0000384
Benjamin Petersone41251e2008-04-25 01:59:09 +0000385 Remove the given parameter completely from the :mailheader:`Content-Type`
386 header. The header will be re-written in place without the parameter or
387 its value. All values will be quoted as necessary unless *requote* is
388 ``False`` (the default is ``True``). Optional *header* specifies an
389 alternative to :mailheader:`Content-Type`.
Georg Brandl116aa622007-08-15 14:28:22 +0000390
Georg Brandl116aa622007-08-15 14:28:22 +0000391
Benjamin Petersone41251e2008-04-25 01:59:09 +0000392 .. method:: set_type(type[, header][, requote])
Georg Brandl116aa622007-08-15 14:28:22 +0000393
Benjamin Petersone41251e2008-04-25 01:59:09 +0000394 Set the main type and subtype for the :mailheader:`Content-Type`
395 header. *type* must be a string in the form :mimetype:`maintype/subtype`,
396 otherwise a :exc:`ValueError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000397
Benjamin Petersone41251e2008-04-25 01:59:09 +0000398 This method replaces the :mailheader:`Content-Type` header, keeping all
399 the parameters in place. If *requote* is ``False``, this leaves the
400 existing header's quoting as is, otherwise the parameters will be quoted
401 (the default).
Georg Brandl116aa622007-08-15 14:28:22 +0000402
Benjamin Petersone41251e2008-04-25 01:59:09 +0000403 An alternative header can be specified in the *header* argument. When the
404 :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version`
405 header is also added.
Georg Brandl116aa622007-08-15 14:28:22 +0000406
Georg Brandl116aa622007-08-15 14:28:22 +0000407
Benjamin Petersone41251e2008-04-25 01:59:09 +0000408 .. method:: get_filename([failobj])
Georg Brandl116aa622007-08-15 14:28:22 +0000409
Benjamin Petersone41251e2008-04-25 01:59:09 +0000410 Return the value of the ``filename`` parameter of the
411 :mailheader:`Content-Disposition` header of the message. If the header
412 does not have a ``filename`` parameter, this method falls back to looking
413 for the ``name`` parameter. If neither is found, or the header is
414 missing, then *failobj* is returned. The returned string will always be
415 unquoted as per :meth:`Utils.unquote`.
Georg Brandl116aa622007-08-15 14:28:22 +0000416
417
Benjamin Petersone41251e2008-04-25 01:59:09 +0000418 .. method:: get_boundary([failobj])
Georg Brandl116aa622007-08-15 14:28:22 +0000419
Benjamin Petersone41251e2008-04-25 01:59:09 +0000420 Return the value of the ``boundary`` parameter of the
421 :mailheader:`Content-Type` header of the message, or *failobj* if either
422 the header is missing, or has no ``boundary`` parameter. The returned
423 string will always be unquoted as per :meth:`Utils.unquote`.
Georg Brandl116aa622007-08-15 14:28:22 +0000424
425
Benjamin Petersone41251e2008-04-25 01:59:09 +0000426 .. method:: set_boundary(boundary)
Georg Brandl116aa622007-08-15 14:28:22 +0000427
Benjamin Petersone41251e2008-04-25 01:59:09 +0000428 Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
429 *boundary*. :meth:`set_boundary` will always quote *boundary* if
430 necessary. A :exc:`HeaderParseError` is raised if the message object has
431 no :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000432
Benjamin Petersone41251e2008-04-25 01:59:09 +0000433 Note that using this method is subtly different than deleting the old
434 :mailheader:`Content-Type` header and adding a new one with the new
435 boundary via :meth:`add_header`, because :meth:`set_boundary` preserves
436 the order of the :mailheader:`Content-Type` header in the list of
437 headers. However, it does *not* preserve any continuation lines which may
438 have been present in the original :mailheader:`Content-Type` header.
Georg Brandl116aa622007-08-15 14:28:22 +0000439
440
Benjamin Petersone41251e2008-04-25 01:59:09 +0000441 .. method:: get_content_charset([failobj])
Georg Brandl116aa622007-08-15 14:28:22 +0000442
Benjamin Petersone41251e2008-04-25 01:59:09 +0000443 Return the ``charset`` parameter of the :mailheader:`Content-Type` header,
444 coerced to lower case. If there is no :mailheader:`Content-Type` header, or if
445 that header has no ``charset`` parameter, *failobj* is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000446
Benjamin Petersone41251e2008-04-25 01:59:09 +0000447 Note that this method differs from :meth:`get_charset` which returns the
448 :class:`Charset` instance for the default encoding of the message body.
Georg Brandl116aa622007-08-15 14:28:22 +0000449
Georg Brandl116aa622007-08-15 14:28:22 +0000450
Benjamin Petersone41251e2008-04-25 01:59:09 +0000451 .. method:: get_charsets([failobj])
Georg Brandl116aa622007-08-15 14:28:22 +0000452
Benjamin Petersone41251e2008-04-25 01:59:09 +0000453 Return a list containing the character set names in the message. If the
454 message is a :mimetype:`multipart`, then the list will contain one element
455 for each subpart in the payload, otherwise, it will be a list of length 1.
Georg Brandl116aa622007-08-15 14:28:22 +0000456
Benjamin Petersone41251e2008-04-25 01:59:09 +0000457 Each item in the list will be a string which is the value of the
458 ``charset`` parameter in the :mailheader:`Content-Type` header for the
459 represented subpart. However, if the subpart has no
460 :mailheader:`Content-Type` header, no ``charset`` parameter, or is not of
461 the :mimetype:`text` main MIME type, then that item in the returned list
462 will be *failobj*.
Georg Brandl116aa622007-08-15 14:28:22 +0000463
464
Benjamin Petersone41251e2008-04-25 01:59:09 +0000465 .. method:: walk()
Georg Brandl116aa622007-08-15 14:28:22 +0000466
Benjamin Petersone41251e2008-04-25 01:59:09 +0000467 The :meth:`walk` method is an all-purpose generator which can be used to
468 iterate over all the parts and subparts of a message object tree, in
469 depth-first traversal order. You will typically use :meth:`walk` as the
470 iterator in a ``for`` loop; each iteration returns the next subpart.
Georg Brandl116aa622007-08-15 14:28:22 +0000471
Benjamin Petersone41251e2008-04-25 01:59:09 +0000472 Here's an example that prints the MIME type of every part of a multipart
473 message structure::
Georg Brandl116aa622007-08-15 14:28:22 +0000474
Benjamin Petersone41251e2008-04-25 01:59:09 +0000475 >>> for part in msg.walk():
476 ... print(part.get_content_type())
477 multipart/report
478 text/plain
479 message/delivery-status
480 text/plain
481 text/plain
482 message/rfc822
Georg Brandl116aa622007-08-15 14:28:22 +0000483
Benjamin Petersone41251e2008-04-25 01:59:09 +0000484 :class:`Message` objects can also optionally contain two instance attributes,
485 which can be used when generating the plain text of a MIME message.
Georg Brandl116aa622007-08-15 14:28:22 +0000486
487
Benjamin Petersone41251e2008-04-25 01:59:09 +0000488 .. attribute:: preamble
Georg Brandl116aa622007-08-15 14:28:22 +0000489
Benjamin Petersone41251e2008-04-25 01:59:09 +0000490 The format of a MIME document allows for some text between the blank line
491 following the headers, and the first multipart boundary string. Normally,
492 this text is never visible in a MIME-aware mail reader because it falls
493 outside the standard MIME armor. However, when viewing the raw text of
494 the message, or when viewing the message in a non-MIME aware reader, this
495 text can become visible.
Georg Brandl116aa622007-08-15 14:28:22 +0000496
Benjamin Petersone41251e2008-04-25 01:59:09 +0000497 The *preamble* attribute contains this leading extra-armor text for MIME
498 documents. When the :class:`Parser` discovers some text after the headers
499 but before the first boundary string, it assigns this text to the
500 message's *preamble* attribute. When the :class:`Generator` is writing
501 out the plain text representation of a MIME message, and it finds the
502 message has a *preamble* attribute, it will write this text in the area
503 between the headers and the first boundary. See :mod:`email.parser` and
504 :mod:`email.generator` for details.
Georg Brandl116aa622007-08-15 14:28:22 +0000505
Benjamin Petersone41251e2008-04-25 01:59:09 +0000506 Note that if the message object has no preamble, the *preamble* attribute
507 will be ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000508
509
Benjamin Petersone41251e2008-04-25 01:59:09 +0000510 .. attribute:: epilogue
Georg Brandl116aa622007-08-15 14:28:22 +0000511
Benjamin Petersone41251e2008-04-25 01:59:09 +0000512 The *epilogue* attribute acts the same way as the *preamble* attribute,
513 except that it contains text that appears between the last boundary and
514 the end of the message.
Georg Brandl116aa622007-08-15 14:28:22 +0000515
Benjamin Petersone41251e2008-04-25 01:59:09 +0000516 You do not need to set the epilogue to the empty string in order for the
517 :class:`Generator` to print a newline at the end of the file.
Georg Brandl116aa622007-08-15 14:28:22 +0000518
519
Benjamin Petersone41251e2008-04-25 01:59:09 +0000520 .. attribute:: defects
Georg Brandl116aa622007-08-15 14:28:22 +0000521
Benjamin Petersone41251e2008-04-25 01:59:09 +0000522 The *defects* attribute contains a list of all the problems found when
523 parsing this message. See :mod:`email.errors` for a detailed description
524 of the possible parsing defects.