blob: 271619d6846a5da21e00d630a7380e5827a52e96 [file] [log] [blame]
Barry Warsaw5e634632001-09-26 05:23:47 +00001\declaremodule{standard}{email.Message}
2\modulesynopsis{The base class representing email messages.}
Barry Warsaw5e634632001-09-26 05:23:47 +00003
Barry Warsawc5f8fe32001-09-26 22:21:52 +00004The central class in the \module{email} package is the
5\class{Message} class; it is the base class for the \module{email}
6object model. \class{Message} provides the core functionality for
7setting and querying header fields, and for accessing message bodies.
Barry Warsaw5e634632001-09-26 05:23:47 +00008
Barry Warsawc5f8fe32001-09-26 22:21:52 +00009Conceptually, a \class{Message} object consists of \emph{headers} and
10\emph{payloads}. Headers are \rfc{2822} style field names and
11values where the field name and value are separated by a colon. The
12colon is not part of either the field name or the field value.
Barry Warsaw5e634632001-09-26 05:23:47 +000013
Barry Warsawc5f8fe32001-09-26 22:21:52 +000014Headers are stored and returned in case-preserving form but are
Barry Warsaw5b9da892002-10-01 01:05:52 +000015matched case-insensitively. There may also be a single envelope
16header, also known as the \emph{Unix-From} header or the
Barry Warsawc5f8fe32001-09-26 22:21:52 +000017\code{From_} header. The payload is either a string in the case of
Barry Warsaw5b9da892002-10-01 01:05:52 +000018simple message objects or a list of \class{Message} objects for
19MIME container documents (e.g. \mimetype{multipart/*} and
20\mimetype{message/rfc822}).
Barry Warsawc5f8fe32001-09-26 22:21:52 +000021
22\class{Message} objects provide a mapping style interface for
23accessing the message headers, and an explicit interface for accessing
24both the headers and the payload. It provides convenience methods for
25generating a flat text representation of the message object tree, for
26accessing commonly used header parameters, and for recursively walking
27over the object tree.
Barry Warsaw5e634632001-09-26 05:23:47 +000028
29Here are the methods of the \class{Message} class:
30
Barry Warsawc5f8fe32001-09-26 22:21:52 +000031\begin{classdesc}{Message}{}
32The constructor takes no arguments.
33\end{classdesc}
34
Barry Warsaw5e634632001-09-26 05:23:47 +000035\begin{methoddesc}[Message]{as_string}{\optional{unixfrom}}
36Return the entire formatted message as a string. Optional
37\var{unixfrom}, when true, specifies to include the \emph{Unix-From}
Barry Warsaw5b9da892002-10-01 01:05:52 +000038envelope header; it defaults to \code{False}.
Barry Warsaw5e634632001-09-26 05:23:47 +000039\end{methoddesc}
40
Fred Drake7779b202002-05-22 20:44:03 +000041\begin{methoddesc}[Message]{__str__}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +000042Equivalent to \method{aMessage.as_string(unixfrom=True)}.
Barry Warsaw5e634632001-09-26 05:23:47 +000043\end{methoddesc}
44
45\begin{methoddesc}[Message]{is_multipart}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +000046Return \code{True} if the message's payload is a list of
47sub-\class{Message} objects, otherwise return \code{False}. When
48\method{is_multipart()} returns False, the payload should be a string
49object.
Barry Warsaw5e634632001-09-26 05:23:47 +000050\end{methoddesc}
51
52\begin{methoddesc}[Message]{set_unixfrom}{unixfrom}
Barry Warsaw5b9da892002-10-01 01:05:52 +000053Set the message's envelope header to \var{unixfrom}, which should be a string.
Barry Warsaw5e634632001-09-26 05:23:47 +000054\end{methoddesc}
55
56\begin{methoddesc}[Message]{get_unixfrom}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +000057Return the message's envelope header. Defaults to \code{None} if the
58envelope header was never set.
Barry Warsaw5e634632001-09-26 05:23:47 +000059\end{methoddesc}
60
61\begin{methoddesc}[Message]{attach}{payload}
Barry Warsaw5b9da892002-10-01 01:05:52 +000062Add the given payload to the current payload, which must be
63\code{None} or a list of \class{Message} objects before the call.
64After the call, the payload will always be a list of \class{Message}
65objects. If you want to set the payload to a scalar object (e.g. a
66string), use \method{set_payload()} instead.
Barry Warsaw5e634632001-09-26 05:23:47 +000067\end{methoddesc}
68
69\begin{methoddesc}[Message]{get_payload}{\optional{i\optional{, decode}}}
Barry Warsaw5b9da892002-10-01 01:05:52 +000070Return a reference the current payload, which will be a list of
71\class{Message} objects when \method{is_multipart()} is \code{True}, or a
72string when \method{is_multipart()} is \code{False}. If the
73payload is a list and you mutate the list object, you modify the
74message's payload in place.
Barry Warsaw5e634632001-09-26 05:23:47 +000075
Barry Warsaw5b9da892002-10-01 01:05:52 +000076With optional argument \var{i}, \method{get_payload()} will return the
Barry Warsaw5e634632001-09-26 05:23:47 +000077\var{i}-th element of the payload, counting from zero, if
Barry Warsaw5b9da892002-10-01 01:05:52 +000078\method{is_multipart()} is \code{True}. An \exception{IndexError}
79will be raised if \var{i} is less than 0 or greater than or equal to
80the number of items in the payload. If the payload is a string
81(i.e. \method{is_multipart()} is \code{False}) and \var{i} is given, a
Barry Warsawc5f8fe32001-09-26 22:21:52 +000082\exception{TypeError} is raised.
Barry Warsaw5e634632001-09-26 05:23:47 +000083
84Optional \var{decode} is a flag indicating whether the payload should be
Barry Warsawc5f8fe32001-09-26 22:21:52 +000085decoded or not, according to the \mailheader{Content-Transfer-Encoding} header.
Barry Warsaw5b9da892002-10-01 01:05:52 +000086When \code{True} and the message is not a multipart, the payload will be
Barry Warsaw5e634632001-09-26 05:23:47 +000087decoded if this header's value is \samp{quoted-printable} or
88\samp{base64}. If some other encoding is used, or
Barry Warsawc5f8fe32001-09-26 22:21:52 +000089\mailheader{Content-Transfer-Encoding} header is
Barry Warsaw5e634632001-09-26 05:23:47 +000090missing, the payload is returned as-is (undecoded). If the message is
Barry Warsaw5b9da892002-10-01 01:05:52 +000091a multipart and the \var{decode} flag is \code{True}, then \code{None} is
92returned. The default for \var{decode} is \code{False}.
Barry Warsaw5e634632001-09-26 05:23:47 +000093\end{methoddesc}
94
Barry Warsaw5b9da892002-10-01 01:05:52 +000095\begin{methoddesc}[Message]{set_payload}{payload\optional{, charset}}
Barry Warsaw5e634632001-09-26 05:23:47 +000096Set the entire message object's payload to \var{payload}. It is the
Barry Warsaw5b9da892002-10-01 01:05:52 +000097client's responsibility to ensure the payload invariants. Optional
98\var{charset} sets the message's default character set (see
99\method{set_charset()} for details.
100
101\versionchanged[\var{charset} argument added]{2.2.2}
102\end{methoddesc}
103
104\begin{methoddesc}[Message]{set_charset}{charset}
105Set the character set of the payload to \var{charset}, which can
106either be a \class{Charset} instance (see \refmodule{email.Charset}, a
107string naming a character set,
108or \code{None}. If it is a string, it will be converted to a
109\class{Charset} instance. If \var{charset} is \code{None}, the
110\code{charset} parameter will be removed from the
111\mailheader{Content-Type} header. Anything else will generate a
112\exception{TypeError}.
113
114The message will be assumed to be of type \mimetype{text/*} encoded with
115\code{charset.input_charset}. It will be converted to
116\code{charset.output_charset}
117and encoded properly, if needed, when generating the plain text
118representation of the message. MIME headers
119(\mailheader{MIME-Version}, \mailheader{Content-Type},
120\mailheader{Content-Transfer-Encoding}) will be added as needed.
121
122\versionadded{2.2.2}
123\end{methoddesc}
124
125\begin{methoddesc}[Message]{get_charset}{}
126Return the \class{Charset} instance associated with the message's payload.
127\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000128\end{methoddesc}
129
130The following methods implement a mapping-like interface for accessing
131the message object's \rfc{2822} headers. Note that there are some
132semantic differences between these methods and a normal mapping
133(i.e. dictionary) interface. For example, in a dictionary there are
134no duplicate keys, but here there may be duplicate message headers. Also,
135in dictionaries there is no guaranteed order to the keys returned by
136\method{keys()}, but in a \class{Message} object, there is an explicit
137order. These semantic differences are intentional and are biased
138toward maximal convenience.
139
Barry Warsaw5b9da892002-10-01 01:05:52 +0000140Note that in all cases, any envelope header present in the message is
141not included in the mapping interface.
Barry Warsaw5e634632001-09-26 05:23:47 +0000142
143\begin{methoddesc}[Message]{__len__}{}
144Return the total number of headers, including duplicates.
145\end{methoddesc}
146
147\begin{methoddesc}[Message]{__contains__}{name}
148Return true if the message object has a field named \var{name}.
Andrew M. Kuchling43dc1fc2001-11-05 01:55:03 +0000149Matching is done case-insensitively and \var{name} should not include the
Barry Warsaw5e634632001-09-26 05:23:47 +0000150trailing colon. Used for the \code{in} operator,
151e.g.:
152
153\begin{verbatim}
154if 'message-id' in myMessage:
155 print 'Message-ID:', myMessage['message-id']
156\end{verbatim}
157\end{methoddesc}
158
159\begin{methoddesc}[Message]{__getitem__}{name}
160Return the value of the named header field. \var{name} should not
161include the colon field separator. If the header is missing,
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000162\code{None} is returned; a \exception{KeyError} is never raised.
Barry Warsaw5e634632001-09-26 05:23:47 +0000163
164Note that if the named field appears more than once in the message's
165headers, exactly which of those field values will be returned is
166undefined. Use the \method{get_all()} method to get the values of all
167the extant named headers.
168\end{methoddesc}
169
170\begin{methoddesc}[Message]{__setitem__}{name, val}
171Add a header to the message with field name \var{name} and value
172\var{val}. The field is appended to the end of the message's existing
173fields.
174
175Note that this does \emph{not} overwrite or delete any existing header
176with the same name. If you want to ensure that the new header is the
177only one present in the message with field name
178\var{name}, first use \method{__delitem__()} to delete all named
179fields, e.g.:
180
181\begin{verbatim}
182del msg['subject']
183msg['subject'] = 'Python roolz!'
184\end{verbatim}
185\end{methoddesc}
186
187\begin{methoddesc}[Message]{__delitem__}{name}
188Delete all occurrences of the field with name \var{name} from the
189message's headers. No exception is raised if the named field isn't
190present in the headers.
191\end{methoddesc}
192
193\begin{methoddesc}[Message]{has_key}{name}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000194Return true if the message contains a header field named \var{name},
195otherwise return false.
Barry Warsaw5e634632001-09-26 05:23:47 +0000196\end{methoddesc}
197
198\begin{methoddesc}[Message]{keys}{}
199Return a list of all the message's header field names. These keys
Barry Warsaw5b9da892002-10-01 01:05:52 +0000200will be sorted in the order in which they appeared in the original
201message, or were added to the message and may contain
202duplicates. Any fields deleted and then subsequently re-added are
203always appended to the end of the header list.
Barry Warsaw5e634632001-09-26 05:23:47 +0000204\end{methoddesc}
205
206\begin{methoddesc}[Message]{values}{}
207Return a list of all the message's field values. These will be sorted
Barry Warsaw5b9da892002-10-01 01:05:52 +0000208in the order in which they appeared in the original message, or were
209added to the message, and may contain
210duplicates. Any fields deleted and then subsequently re-added are
211always appended to the end of the header list.
Barry Warsaw5e634632001-09-26 05:23:47 +0000212\end{methoddesc}
213
214\begin{methoddesc}[Message]{items}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000215Return a list of 2-tuples containing all the message's field headers
216and values. These will be sorted in the order in which they appeared
217in the original message, or were added to the message, and may contain
218duplicates. Any fields deleted and then subsequently re-added are
219always appended to the end of the header list.
Barry Warsaw5e634632001-09-26 05:23:47 +0000220\end{methoddesc}
221
222\begin{methoddesc}[Message]{get}{name\optional{, failobj}}
223Return the value of the named header field. This is identical to
224\method{__getitem__()} except that optional \var{failobj} is returned
225if the named header is missing (defaults to \code{None}).
226\end{methoddesc}
227
228Here are some additional useful methods:
229
230\begin{methoddesc}[Message]{get_all}{name\optional{, failobj}}
231Return a list of all the values for the field named \var{name}. These
Barry Warsaw5b9da892002-10-01 01:05:52 +0000232will be sorted in the order in which they appeared in the original
233message, or were added to the message. Any fields deleted and then
234subsequently re-added are always appended to the end of the list.
Barry Warsaw5e634632001-09-26 05:23:47 +0000235
236If there are no such named headers in the message, \var{failobj} is
237returned (defaults to \code{None}).
238\end{methoddesc}
239
240\begin{methoddesc}[Message]{add_header}{_name, _value, **_params}
241Extended header setting. This method is similar to
242\method{__setitem__()} except that additional header parameters can be
Barry Warsaw5b9da892002-10-01 01:05:52 +0000243provided as keyword arguments. \var{_name} is the header field to add
244and \var{_value} is the \emph{primary} value for the header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000245
246For each item in the keyword argument dictionary \var{_params}, the
247key is taken as the parameter name, with underscores converted to
248dashes (since dashes are illegal in Python identifiers). Normally,
249the parameter will be added as \code{key="value"} unless the value is
250\code{None}, in which case only the key will be added.
251
252Here's an example:
253
254\begin{verbatim}
255msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
256\end{verbatim}
257
258This will add a header that looks like
259
260\begin{verbatim}
261Content-Disposition: attachment; filename="bud.gif"
262\end{verbatim}
263\end{methoddesc}
264
Barry Warsaw5b9da892002-10-01 01:05:52 +0000265\begin{methoddesc}[Message]{replace_header}{_name, _value}
266Replace a header. Replace the first header found in the message that
267matches \var{_name}, retaining header order and field name case. If
268no matching header was found, a \exception{KeyError} is raised.
269
270\versionadded{2.2.2}
271\end{methoddesc}
272
273\begin{methoddesc}[Message]{get_content_type}{}
274Return the message's content type. The returned string is coerced to
275lower case of the form \mimetype{maintype/subtype}. If there was no
276\mailheader{Content-Type} header in the message the default type as
277given by \method{get_default_type()} will be returned. Since
278according to \rfc{2045}, messages always have a default type,
279\method{get_content_type()} will always return a value.
280
281\rfc{2045} defines a message's default type to be
282\mimetype{text/plain} unless it appears inside a
283\mimetype{multipart/digest} container, in which case it would be
284\mimetype{message/rfc822}. If the \mailheader{Content-Type} header
285has an invalid type specification, \rfc{2045} mandates that the
286default type be \mimetype{text/plain}.
287
288\versionadded{2.2.2}
289\end{methoddesc}
290
291\begin{methoddesc}[Message]{get_content_maintype}{}
292Return the message's main content type. This is the
293\mimetype{maintype} part of the string returned by
294\method{get_content_type()}.
295
296\versionadded{2.2.2}
297\end{methoddesc}
298
299\begin{methoddesc}[Message]{get_content_subtype}{}
300Return the message's sub-content type. This is the \mimetype{subtype}
301part of the string returned by \method{get_content_type()}.
302
303\versionadded{2.2.2}
304\end{methoddesc}
305
306\begin{methoddesc}[Message]{get_default_type}{}
307Return the default content type. Most messages have a default content
308type of \mimetype{text/plain}, except for messages that are subparts
309of \mimetype{multipart/digest} containers. Such subparts have a
310default content type of \mimetype{message/rfc822}.
311
312\versionadded{2.2.2}
313\end{methoddesc}
314
315\begin{methoddesc}[Message]{set_default_type}{ctype}
316Set the default content type. \var{ctype} should either be
317\mimetype{text/plain} or \mimetype{message/rfc822}, although this is
318not enforced. The default content type is not stored in the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000319\mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000320
Barry Warsaw5b9da892002-10-01 01:05:52 +0000321\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000322\end{methoddesc}
323
Barry Warsaw5b9da892002-10-01 01:05:52 +0000324\begin{methoddesc}[Message]{get_params}{\optional{failobj\optional{,
325 header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000326Return the message's \mailheader{Content-Type} parameters, as a list. The
Barry Warsaw5e634632001-09-26 05:23:47 +0000327elements of the returned list are 2-tuples of key/value pairs, as
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000328split on the \character{=} sign. The left hand side of the
329\character{=} is the key, while the right hand side is the value. If
330there is no \character{=} sign in the parameter the value is the empty
Barry Warsaw5b9da892002-10-01 01:05:52 +0000331string, otherwise the value is as described in \method{get_param()} and is
332unquoted if optional \var{unquote} is \code{True} (the default).
Barry Warsaw5e634632001-09-26 05:23:47 +0000333
334Optional \var{failobj} is the object to return if there is no
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000335\mailheader{Content-Type} header. Optional \var{header} is the header to
336search instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000337
338\versionchanged[\var{unquote} argument added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000339\end{methoddesc}
340
341\begin{methoddesc}[Message]{get_param}{param\optional{,
Barry Warsaw5b9da892002-10-01 01:05:52 +0000342 failobj\optional{, header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000343Return the value of the \mailheader{Content-Type} header's parameter
344\var{param} as a string. If the message has no \mailheader{Content-Type}
Barry Warsaw5e634632001-09-26 05:23:47 +0000345header or if there is no such parameter, then \var{failobj} is
346returned (defaults to \code{None}).
347
348Optional \var{header} if given, specifies the message header to use
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000349instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000350
351Parameter keys are always compared case insensitively. The return
352value can either be a string, or a 3-tuple if the parameter was
353\rfc{2231} encoded. When it's a 3-tuple, the elements of the value are of
354the form \samp{(CHARSET, LANGUAGE, VALUE)}, where \var{LANGUAGE} may
355be the empty string. Your application should be prepared to deal with
3563-tuple return values, which it can convert the parameter to a Unicode
357string like so:
358
359\begin{verbatim}
360param = msg.get_param('foo')
361if isinstance(param, tuple):
362 param = unicode(param[2], param[0])
363\end{verbatim}
364
365In any case, the parameter value (either the returned string, or the
366\var{VALUE} item in the 3-tuple) is always unquoted, unless
367\var{unquote} is set to \code{False}.
368
369\versionchanged[\var{unquote} argument added, and 3-tuple return value
370possible]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000371\end{methoddesc}
372
Barry Warsaw5b9da892002-10-01 01:05:52 +0000373\begin{methoddesc}[Message]{set_param}{param, value\optional{,
374 header\optional{, requote\optional{, charset\optional{, language}}}}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000375
Barry Warsaw5b9da892002-10-01 01:05:52 +0000376Set a parameter in the \mailheader{Content-Type} header. If the
377parameter already exists in the header, its value will be replaced
378with \var{value}. If the \mailheader{Content-Type} header as not yet
379been defined for this message, it will be set to \mimetype{text/plain}
380and the new parameter value will be appended as per \rfc{2045}.
381
382Optional \var{header} specifies an alternative header to
383\mailheader{Content-Type}, and all parameters will be quoted as
384necessary unless optional \var{requote} is \code{False} (the default
385is \code{True}).
386
387If optional \var{charset} is specified, the parameter will be encoded
388according to \rfc{2231}. Optional \var{language} specifies the RFC
3892231 language, defaulting to the empty string. Both \var{charset} and
390\var{language} should be strings.
391
392\versionadded{2.2.2}
393\end{methoddesc}
394
395\begin{methoddesc}[Message]{del_param}{param\optional{, header\optional{,
396 requote}}}
397Remove the given parameter completely from the
398\mailheader{Content-Type} header. The header will be re-written in
399place without the parameter or its value. All values will be quoted
400as necessary unless \var{requote} is \code{False} (the default is
401\code{True}). Optional \var{header} specifies an alterative to
402\mailheader{Content-Type}.
403
404\versionadded{2.2.2}
405\end{methoddesc}
406
407\begin{methoddesc}[Message]{set_type}{type\optional{, header}\optional{,
408 requote}}
409Set the main type and subtype for the \mailheader{Content-Type}
410header. \var{type} must be a string in the form
411\mimetype{maintype/subtype}, otherwise a \exception{ValueError} is
412raised.
413
414This method replaces the \mailheader{Content-Type} header, keeping all
415the parameters in place. If \var{requote} is \code{False}, this
416leaves the existing header's quoting as is, otherwise the parameters
417will be quoted (the default).
418
419An alternative header can be specified in the \var{header} argument.
420When the \mailheader{Content-Type} header is set, we'll always also
421add a \mailheader{MIME-Version} header.
422
423\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000424\end{methoddesc}
425
426\begin{methoddesc}[Message]{get_filename}{\optional{failobj}}
427Return the value of the \code{filename} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000428\mailheader{Content-Disposition} header of the message, or \var{failobj} if
Barry Warsaw5e634632001-09-26 05:23:47 +0000429either the header is missing, or has no \code{filename} parameter.
430The returned string will always be unquoted as per
431\method{Utils.unquote()}.
432\end{methoddesc}
433
434\begin{methoddesc}[Message]{get_boundary}{\optional{failobj}}
435Return the value of the \code{boundary} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000436\mailheader{Content-Type} header of the message, or \var{failobj} if either
Barry Warsaw5e634632001-09-26 05:23:47 +0000437the header is missing, or has no \code{boundary} parameter. The
438returned string will always be unquoted as per
439\method{Utils.unquote()}.
440\end{methoddesc}
441
442\begin{methoddesc}[Message]{set_boundary}{boundary}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000443Set the \code{boundary} parameter of the \mailheader{Content-Type} header
Barry Warsaw5e634632001-09-26 05:23:47 +0000444to \var{boundary}. \method{set_boundary()} will always quote
445\var{boundary} so you should not quote it yourself. A
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000446\exception{HeaderParseError} is raised if the message object has no
447\mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000448
449Note that using this method is subtly different than deleting the old
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000450\mailheader{Content-Type} header and adding a new one with the new boundary
Barry Warsaw5e634632001-09-26 05:23:47 +0000451via \method{add_header()}, because \method{set_boundary()} preserves the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000452order of the \mailheader{Content-Type} header in the list of headers.
Barry Warsaw5e634632001-09-26 05:23:47 +0000453However, it does \emph{not} preserve any continuation lines which may
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000454have been present in the original \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000455\end{methoddesc}
456
Barry Warsaw5b9da892002-10-01 01:05:52 +0000457\begin{methoddesc}[Message]{get_content_charset}{\optional{failobj}}
458Return the \code{charset} parameter of the \mailheader{Content-Type}
459header. If there is no \mailheader{Content-Type} header, or if that
460header has no \code{charset} parameter, \var{failobj} is returned.
461
462Note that this method differs from \method{get_charset} which returns
463the \class{Charset} instance for the default encoding of the message
464body.
465
466\versionadded{2.2.2}
467\end{methoddesc}
468
469\begin{methoddesc}[Message]{get_charsets}{\optional{failobj}}
470Return a list containing the character set names in the message. If
471the message is a \mimetype{multipart}, then the list will contain one
472element for each subpart in the payload, otherwise, it will be a list
473of length 1.
474
475Each item in the list will be a string which is the value of the
476\code{charset} parameter in the \mailheader{Content-Type} header for the
477represented subpart. However, if the subpart has no
478\mailheader{Content-Type} header, no \code{charset} parameter, or is not of
479the \mimetype{text} main MIME type, then that item in the returned list
480will be \var{failobj}.
481\end{methoddesc}
482
Barry Warsaw5e634632001-09-26 05:23:47 +0000483\begin{methoddesc}[Message]{walk}{}
484The \method{walk()} method is an all-purpose generator which can be
485used to iterate over all the parts and subparts of a message object
486tree, in depth-first traversal order. You will typically use
487\method{walk()} as the iterator in a \code{for ... in} loop; each
488iteration returns the next subpart.
489
490Here's an example that prints the MIME type of every part of a message
491object tree:
492
493\begin{verbatim}
494>>> for part in msg.walk():
495>>> print part.get_type('text/plain')
496multipart/report
497text/plain
498message/delivery-status
499text/plain
500text/plain
501message/rfc822
502\end{verbatim}
503\end{methoddesc}
504
505\class{Message} objects can also optionally contain two instance
506attributes, which can be used when generating the plain text of a MIME
507message.
508
509\begin{datadesc}{preamble}
510The format of a MIME document allows for some text between the blank
511line following the headers, and the first multipart boundary string.
512Normally, this text is never visible in a MIME-aware mail reader
513because it falls outside the standard MIME armor. However, when
514viewing the raw text of the message, or when viewing the message in a
515non-MIME aware reader, this text can become visible.
516
517The \var{preamble} attribute contains this leading extra-armor text
518for MIME documents. When the \class{Parser} discovers some text after
519the headers but before the first boundary string, it assigns this text
520to the message's \var{preamble} attribute. When the \class{Generator}
521is writing out the plain text representation of a MIME message, and it
522finds the message has a \var{preamble} attribute, it will write this
Barry Warsaw5b9da892002-10-01 01:05:52 +0000523text in the area between the headers and the first boundary. See
524\refmodule{email.Parser} and \refmodule{email.Generator} for details.
Barry Warsaw5e634632001-09-26 05:23:47 +0000525
526Note that if the message object has no preamble, the
527\var{preamble} attribute will be \code{None}.
528\end{datadesc}
529
530\begin{datadesc}{epilogue}
531The \var{epilogue} attribute acts the same way as the \var{preamble}
532attribute, except that it contains text that appears between the last
533boundary and the end of the message.
Barry Warsawe736d932001-10-19 04:34:42 +0000534
535One note: when generating the flat text for a \mimetype{multipart}
536message that has no \var{epilogue} (using the standard
537\class{Generator} class), no newline is added after the closing
538boundary line. If the message object has an \var{epilogue} and its
539value does not start with a newline, a newline is printed after the
540closing boundary. This seems a little clumsy, but it makes the most
541practical sense. The upshot is that if you want to ensure that a
542newline get printed after your closing \mimetype{multipart} boundary,
543set the \var{epilogue} to the empty string.
Barry Warsaw5e634632001-09-26 05:23:47 +0000544\end{datadesc}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000545
546\subsubsection{Deprecated methods}
547
548The following methods are deprecated in \module{email} version 2.
549They are documented here for completeness.
550
551\begin{methoddesc}[Message]{add_payload}{payload}
552Add \var{payload} to the message object's existing payload. If, prior
553to calling this method, the object's payload was \code{None}
554(i.e. never before set), then after this method is called, the payload
555will be the argument \var{payload}.
556
557If the object's payload was already a list
558(i.e. \method{is_multipart()} returns 1), then \var{payload} is
559appended to the end of the existing payload list.
560
561For any other type of existing payload, \method{add_payload()} will
562transform the new payload into a list consisting of the old payload
563and \var{payload}, but only if the document is already a MIME
564multipart document. This condition is satisfied if the message's
565\mailheader{Content-Type} header's main type is either
566\mimetype{multipart}, or there is no \mailheader{Content-Type}
567header. In any other situation,
568\exception{MultipartConversionError} is raised.
569
570\deprecated{2.2.2}{Use the \method{attach()} method instead.}
571\end{methoddesc}
572
573\begin{methoddesc}[Message]{get_type}{\optional{failobj}}
574Return the message's content type, as a string of the form
575\mimetype{maintype/subtype} as taken from the
576\mailheader{Content-Type} header.
577The returned string is coerced to lowercase.
578
579If there is no \mailheader{Content-Type} header in the message,
580\var{failobj} is returned (defaults to \code{None}).
581
582\deprecated{2.2.2}{Use the \method{get_content_type()} method instead.}
583\end{methoddesc}
584
585\begin{methoddesc}[Message]{get_main_type}{\optional{failobj}}
586Return the message's \emph{main} content type. This essentially returns the
587\var{maintype} part of the string returned by \method{get_type()}, with the
588same semantics for \var{failobj}.
589
590\deprecated{2.2.2}{Use the \method{get_content_maintype()} method instead.}
591\end{methoddesc}
592
593\begin{methoddesc}[Message]{get_subtype}{\optional{failobj}}
594Return the message's sub-content type. This essentially returns the
595\var{subtype} part of the string returned by \method{get_type()}, with the
596same semantics for \var{failobj}.
597
598\deprecated{2.2.2}{Use the \method{get_content_subtype()} method instead.}
599\end{methoddesc}
600