blob: 7f3cf6f71d3e005d372886a1f89ba6386bb1edcd [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
39.. method:: Message.as_string([unixfrom])
40
41 Return the entire message flatten as a string. When optional *unixfrom* is
42 ``True``, the envelope header is included in the returned string. *unixfrom*
43 defaults to ``False``.
44
45 Note that this method is provided as a convenience and may not always format the
46 message the way you want. For example, by default it mangles lines that begin
47 with ``From``. For more flexibility, instantiate a :class:`Generator` instance
48 and use its :meth:`flatten` method directly. For example::
49
50 from cStringIO import StringIO
51 from email.generator import Generator
52 fp = StringIO()
53 g = Generator(fp, mangle_from_=False, maxheaderlen=60)
54 g.flatten(msg)
55 text = fp.getvalue()
56
57
58.. method:: Message.__str__()
59
60 Equivalent to ``as_string(unixfrom=True)``.
61
62
63.. method:: Message.is_multipart()
64
65 Return ``True`` if the message's payload is a list of sub-\ :class:`Message`
66 objects, otherwise return ``False``. When :meth:`is_multipart` returns False,
67 the payload should be a string object.
68
69
70.. method:: Message.set_unixfrom(unixfrom)
71
72 Set the message's envelope header to *unixfrom*, which should be a string.
73
74
75.. method:: Message.get_unixfrom()
76
77 Return the message's envelope header. Defaults to ``None`` if the envelope
78 header was never set.
79
80
81.. method:: Message.attach(payload)
82
83 Add the given *payload* to the current payload, which must be ``None`` or a list
84 of :class:`Message` objects before the call. After the call, the payload will
85 always be a list of :class:`Message` objects. If you want to set the payload to
86 a scalar object (e.g. a string), use :meth:`set_payload` instead.
87
88
89.. method:: Message.get_payload([i[, decode]])
90
91 Return a reference the current payload, which will be a list of :class:`Message`
92 objects when :meth:`is_multipart` is ``True``, or a string when
93 :meth:`is_multipart` is ``False``. If the payload is a list and you mutate the
94 list object, you modify the message's payload in place.
95
96 With optional argument *i*, :meth:`get_payload` will return the *i*-th element
97 of the payload, counting from zero, if :meth:`is_multipart` is ``True``. An
98 :exc:`IndexError` will be raised if *i* is less than 0 or greater than or equal
99 to the number of items in the payload. If the payload is a string (i.e.
100 :meth:`is_multipart` is ``False``) and *i* is given, a :exc:`TypeError` is
101 raised.
102
103 Optional *decode* is a flag indicating whether the payload should be decoded or
104 not, according to the :mailheader:`Content-Transfer-Encoding` header. When
105 ``True`` and the message is not a multipart, the payload will be decoded if this
106 header's value is ``quoted-printable`` or ``base64``. If some other encoding is
107 used, or :mailheader:`Content-Transfer-Encoding` header is missing, or if the
108 payload has bogus base64 data, the payload is returned as-is (undecoded). If
109 the message is a multipart and the *decode* flag is ``True``, then ``None`` is
110 returned. The default for *decode* is ``False``.
111
112
113.. method:: Message.set_payload(payload[, charset])
114
115 Set the entire message object's payload to *payload*. It is the client's
116 responsibility to ensure the payload invariants. Optional *charset* sets the
117 message's default character set; see :meth:`set_charset` for details.
118
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120.. method:: Message.set_charset(charset)
121
122 Set the character set of the payload to *charset*, which can either be a
123 :class:`Charset` instance (see :mod:`email.charset`), a string naming a
124 character set, or ``None``. If it is a string, it will be converted to a
125 :class:`Charset` instance. If *charset* is ``None``, the ``charset`` parameter
126 will be removed from the :mailheader:`Content-Type` header. Anything else will
127 generate a :exc:`TypeError`.
128
129 The message will be assumed to be of type :mimetype:`text/\*` encoded with
130 *charset.input_charset*. It will be converted to *charset.output_charset* and
131 encoded properly, if needed, when generating the plain text representation of
132 the message. MIME headers (:mailheader:`MIME-Version`,
133 :mailheader:`Content-Type`, :mailheader:`Content-Transfer-Encoding`) will be
134 added as needed.
135
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137.. method:: Message.get_charset()
138
139 Return the :class:`Charset` instance associated with the message's payload.
140
Georg Brandl116aa622007-08-15 14:28:22 +0000141The following methods implement a mapping-like interface for accessing the
142message's :rfc:`2822` headers. Note that there are some semantic differences
143between these methods and a normal mapping (i.e. dictionary) interface. For
144example, in a dictionary there are no duplicate keys, but here there may be
145duplicate message headers. Also, in dictionaries there is no guaranteed order
146to the keys returned by :meth:`keys`, but in a :class:`Message` object, headers
147are always returned in the order they appeared in the original message, or were
148added to the message later. Any header deleted and then re-added are always
149appended to the end of the header list.
150
151These semantic differences are intentional and are biased toward maximal
152convenience.
153
154Note that in all cases, any envelope header present in the message is not
155included in the mapping interface.
156
157
158.. method:: Message.__len__()
159
160 Return the total number of headers, including duplicates.
161
162
163.. method:: Message.__contains__(name)
164
165 Return true if the message object has a field named *name*. Matching is done
166 case-insensitively and *name* should not include the trailing colon. Used for
167 the ``in`` operator, e.g.::
168
169 if 'message-id' in myMessage:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000170 print('Message-ID:', myMessage['message-id'])
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172
173.. method:: Message.__getitem__(name)
174
175 Return the value of the named header field. *name* should not include the colon
176 field separator. If the header is missing, ``None`` is returned; a
177 :exc:`KeyError` is never raised.
178
179 Note that if the named field appears more than once in the message's headers,
180 exactly which of those field values will be returned is undefined. Use the
181 :meth:`get_all` method to get the values of all the extant named headers.
182
183
184.. method:: Message.__setitem__(name, val)
185
186 Add a header to the message with field name *name* and value *val*. The field
187 is appended to the end of the message's existing fields.
188
189 Note that this does *not* overwrite or delete any existing header with the same
190 name. If you want to ensure that the new header is the only one present in the
191 message with field name *name*, delete the field first, e.g.::
192
193 del msg['subject']
194 msg['subject'] = 'Python roolz!'
195
196
197.. method:: Message.__delitem__(name)
198
199 Delete all occurrences of the field with name *name* from the message's headers.
200 No exception is raised if the named field isn't present in the headers.
201
202
Collin Winterc79461b2007-09-01 23:34:30 +0000203.. method:: Message.__contains__(name)
Georg Brandl116aa622007-08-15 14:28:22 +0000204
205 Return true if the message contains a header field named *name*, otherwise
206 return false.
207
208
209.. method:: Message.keys()
210
211 Return a list of all the message's header field names.
212
213
214.. method:: Message.values()
215
216 Return a list of all the message's field values.
217
218
219.. method:: Message.items()
220
221 Return a list of 2-tuples containing all the message's field headers and values.
222
223
224.. method:: Message.get(name[, failobj])
225
226 Return the value of the named header field. This is identical to
227 :meth:`__getitem__` except that optional *failobj* is returned if the named
228 header is missing (defaults to ``None``).
229
230Here are some additional useful methods:
231
232
233.. method:: Message.get_all(name[, failobj])
234
235 Return a list of all the values for the field named *name*. If there are no such
236 named headers in the message, *failobj* is returned (defaults to ``None``).
237
238
239.. method:: Message.add_header(_name, _value, **_params)
240
241 Extended header setting. This method is similar to :meth:`__setitem__` except
242 that additional header parameters can be provided as keyword arguments. *_name*
243 is the header field to add and *_value* is the *primary* value for the header.
244
245 For each item in the keyword argument dictionary *_params*, the key is taken as
246 the parameter name, with underscores converted to dashes (since dashes are
247 illegal in Python identifiers). Normally, the parameter will be added as
248 ``key="value"`` unless the value is ``None``, in which case only the key will be
249 added.
250
251 Here's an example::
252
253 msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
254
255 This will add a header that looks like ::
256
257 Content-Disposition: attachment; filename="bud.gif"
258
259
260.. method:: Message.replace_header(_name, _value)
261
262 Replace a header. Replace the first header found in the message that matches
263 *_name*, retaining header order and field name case. If no matching header was
264 found, a :exc:`KeyError` is raised.
265
Georg Brandl116aa622007-08-15 14:28:22 +0000266
267.. method:: Message.get_content_type()
268
269 Return the message's content type. The returned string is coerced to lower case
270 of the form :mimetype:`maintype/subtype`. If there was no
271 :mailheader:`Content-Type` header in the message the default type as given by
272 :meth:`get_default_type` will be returned. Since according to :rfc:`2045`,
273 messages always have a default type, :meth:`get_content_type` will always return
274 a value.
275
276 :rfc:`2045` defines a message's default type to be :mimetype:`text/plain` unless
277 it appears inside a :mimetype:`multipart/digest` container, in which case it
278 would be :mimetype:`message/rfc822`. If the :mailheader:`Content-Type` header
279 has an invalid type specification, :rfc:`2045` mandates that the default type be
280 :mimetype:`text/plain`.
281
Georg Brandl116aa622007-08-15 14:28:22 +0000282
283.. method:: Message.get_content_maintype()
284
285 Return the message's main content type. This is the :mimetype:`maintype` part
286 of the string returned by :meth:`get_content_type`.
287
Georg Brandl116aa622007-08-15 14:28:22 +0000288
289.. method:: Message.get_content_subtype()
290
291 Return the message's sub-content type. This is the :mimetype:`subtype` part of
292 the string returned by :meth:`get_content_type`.
293
Georg Brandl116aa622007-08-15 14:28:22 +0000294
295.. method:: Message.get_default_type()
296
297 Return the default content type. Most messages have a default content type of
298 :mimetype:`text/plain`, except for messages that are subparts of
299 :mimetype:`multipart/digest` containers. Such subparts have a default content
300 type of :mimetype:`message/rfc822`.
301
Georg Brandl116aa622007-08-15 14:28:22 +0000302
303.. method:: Message.set_default_type(ctype)
304
305 Set the default content type. *ctype* should either be :mimetype:`text/plain`
306 or :mimetype:`message/rfc822`, although this is not enforced. The default
307 content type is not stored in the :mailheader:`Content-Type` header.
308
Georg Brandl116aa622007-08-15 14:28:22 +0000309
310.. method:: Message.get_params([failobj[, header[, unquote]]])
311
312 Return the message's :mailheader:`Content-Type` parameters, as a list. The
313 elements of the returned list are 2-tuples of key/value pairs, as split on the
314 ``'='`` sign. The left hand side of the ``'='`` is the key, while the right
315 hand side is the value. If there is no ``'='`` sign in the parameter the value
316 is the empty string, otherwise the value is as described in :meth:`get_param`
317 and is unquoted if optional *unquote* is ``True`` (the default).
318
319 Optional *failobj* is the object to return if there is no
320 :mailheader:`Content-Type` header. Optional *header* is the header to search
321 instead of :mailheader:`Content-Type`.
322
Georg Brandl116aa622007-08-15 14:28:22 +0000323
324.. method:: Message.get_param(param[, failobj[, header[, unquote]]])
325
326 Return the value of the :mailheader:`Content-Type` header's parameter *param* as
327 a string. If the message has no :mailheader:`Content-Type` header or if there
328 is no such parameter, then *failobj* is returned (defaults to ``None``).
329
330 Optional *header* if given, specifies the message header to use instead of
331 :mailheader:`Content-Type`.
332
333 Parameter keys are always compared case insensitively. The return value can
334 either be a string, or a 3-tuple if the parameter was :rfc:`2231` encoded. When
335 it's a 3-tuple, the elements of the value are of the form ``(CHARSET, LANGUAGE,
336 VALUE)``. Note that both ``CHARSET`` and ``LANGUAGE`` can be ``None``, in which
337 case you should consider ``VALUE`` to be encoded in the ``us-ascii`` charset.
338 You can usually ignore ``LANGUAGE``.
339
340 If your application doesn't care whether the parameter was encoded as in
341 :rfc:`2231`, you can collapse the parameter value by calling
342 :func:`email.Utils.collapse_rfc2231_value`, passing in the return value from
343 :meth:`get_param`. This will return a suitably decoded Unicode string whn the
344 value is a tuple, or the original string unquoted if it isn't. For example::
345
346 rawparam = msg.get_param('foo')
347 param = email.Utils.collapse_rfc2231_value(rawparam)
348
349 In any case, the parameter value (either the returned string, or the ``VALUE``
350 item in the 3-tuple) is always unquoted, unless *unquote* is set to ``False``.
351
Georg Brandl116aa622007-08-15 14:28:22 +0000352
353.. method:: Message.set_param(param, value[, header[, requote[, charset[, language]]]])
354
355 Set a parameter in the :mailheader:`Content-Type` header. If the parameter
356 already exists in the header, its value will be replaced with *value*. If the
357 :mailheader:`Content-Type` header as not yet been defined for this message, it
358 will be set to :mimetype:`text/plain` and the new parameter value will be
359 appended as per :rfc:`2045`.
360
361 Optional *header* specifies an alternative header to :mailheader:`Content-Type`,
362 and all parameters will be quoted as necessary unless optional *requote* is
363 ``False`` (the default is ``True``).
364
365 If optional *charset* is specified, the parameter will be encoded according to
366 :rfc:`2231`. Optional *language* specifies the RFC 2231 language, defaulting to
367 the empty string. Both *charset* and *language* should be strings.
368
Georg Brandl116aa622007-08-15 14:28:22 +0000369
370.. method:: Message.del_param(param[, header[, requote]])
371
372 Remove the given parameter completely from the :mailheader:`Content-Type`
373 header. The header will be re-written in place without the parameter or its
374 value. All values will be quoted as necessary unless *requote* is ``False``
375 (the default is ``True``). Optional *header* specifies an alternative to
376 :mailheader:`Content-Type`.
377
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379.. method:: Message.set_type(type[, header][, requote])
380
381 Set the main type and subtype for the :mailheader:`Content-Type` header. *type*
382 must be a string in the form :mimetype:`maintype/subtype`, otherwise a
383 :exc:`ValueError` is raised.
384
385 This method replaces the :mailheader:`Content-Type` header, keeping all the
386 parameters in place. If *requote* is ``False``, this leaves the existing
387 header's quoting as is, otherwise the parameters will be quoted (the default).
388
389 An alternative header can be specified in the *header* argument. When the
390 :mailheader:`Content-Type` header is set a :mailheader:`MIME-Version` header is
391 also added.
392
Georg Brandl116aa622007-08-15 14:28:22 +0000393
394.. method:: Message.get_filename([failobj])
395
396 Return the value of the ``filename`` parameter of the
397 :mailheader:`Content-Disposition` header of the message. If the header does not
398 have a ``filename`` parameter, this method falls back to looking for the
399 ``name`` parameter. If neither is found, or the header is missing, then
400 *failobj* is returned. The returned string will always be unquoted as per
401 :meth:`Utils.unquote`.
402
403
404.. method:: Message.get_boundary([failobj])
405
406 Return the value of the ``boundary`` parameter of the :mailheader:`Content-Type`
407 header of the message, or *failobj* if either the header is missing, or has no
408 ``boundary`` parameter. The returned string will always be unquoted as per
409 :meth:`Utils.unquote`.
410
411
412.. method:: Message.set_boundary(boundary)
413
414 Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
415 *boundary*. :meth:`set_boundary` will always quote *boundary* if necessary. A
416 :exc:`HeaderParseError` is raised if the message object has no
417 :mailheader:`Content-Type` header.
418
419 Note that using this method is subtly different than deleting the old
420 :mailheader:`Content-Type` header and adding a new one with the new boundary via
421 :meth:`add_header`, because :meth:`set_boundary` preserves the order of the
422 :mailheader:`Content-Type` header in the list of headers. However, it does *not*
423 preserve any continuation lines which may have been present in the original
424 :mailheader:`Content-Type` header.
425
426
427.. method:: Message.get_content_charset([failobj])
428
429 Return the ``charset`` parameter of the :mailheader:`Content-Type` header,
430 coerced to lower case. If there is no :mailheader:`Content-Type` header, or if
431 that header has no ``charset`` parameter, *failobj* is returned.
432
433 Note that this method differs from :meth:`get_charset` which returns the
434 :class:`Charset` instance for the default encoding of the message body.
435
Georg Brandl116aa622007-08-15 14:28:22 +0000436
437.. method:: Message.get_charsets([failobj])
438
439 Return a list containing the character set names in the message. If the message
440 is a :mimetype:`multipart`, then the list will contain one element for each
441 subpart in the payload, otherwise, it will be a list of length 1.
442
443 Each item in the list will be a string which is the value of the ``charset``
444 parameter in the :mailheader:`Content-Type` header for the represented subpart.
445 However, if the subpart has no :mailheader:`Content-Type` header, no ``charset``
446 parameter, or is not of the :mimetype:`text` main MIME type, then that item in
447 the returned list will be *failobj*.
448
449
450.. method:: Message.walk()
451
452 The :meth:`walk` method is an all-purpose generator which can be used to iterate
453 over all the parts and subparts of a message object tree, in depth-first
454 traversal order. You will typically use :meth:`walk` as the iterator in a
455 ``for`` loop; each iteration returns the next subpart.
456
457 Here's an example that prints the MIME type of every part of a multipart message
458 structure::
459
460 >>> for part in msg.walk():
Georg Brandl6911e3c2007-09-04 07:15:32 +0000461 ... print(part.get_content_type())
Georg Brandl116aa622007-08-15 14:28:22 +0000462 multipart/report
463 text/plain
464 message/delivery-status
465 text/plain
466 text/plain
467 message/rfc822
468
Georg Brandl116aa622007-08-15 14:28:22 +0000469:class:`Message` objects can also optionally contain two instance attributes,
470which can be used when generating the plain text of a MIME message.
471
472
473.. data:: preamble
474
475 The format of a MIME document allows for some text between the blank line
476 following the headers, and the first multipart boundary string. Normally, this
477 text is never visible in a MIME-aware mail reader because it falls outside the
478 standard MIME armor. However, when viewing the raw text of the message, or when
479 viewing the message in a non-MIME aware reader, this text can become visible.
480
481 The *preamble* attribute contains this leading extra-armor text for MIME
482 documents. When the :class:`Parser` discovers some text after the headers but
483 before the first boundary string, it assigns this text to the message's
484 *preamble* attribute. When the :class:`Generator` is writing out the plain text
485 representation of a MIME message, and it finds the message has a *preamble*
486 attribute, it will write this text in the area between the headers and the first
487 boundary. See :mod:`email.parser` and :mod:`email.generator` for details.
488
489 Note that if the message object has no preamble, the *preamble* attribute will
490 be ``None``.
491
492
493.. data:: epilogue
494
495 The *epilogue* attribute acts the same way as the *preamble* attribute, except
496 that it contains text that appears between the last boundary and the end of the
497 message.
498
Georg Brandl55ac8f02007-09-01 13:51:09 +0000499 You do not need to set the epilogue to the empty string in order for the
500 :class:`Generator` to print a newline at the end of the file.
Georg Brandl116aa622007-08-15 14:28:22 +0000501
502
503.. data:: defects
504
505 The *defects* attribute contains a list of all the problems found when parsing
506 this message. See :mod:`email.errors` for a detailed description of the
507 possible parsing defects.