blob: 34c152db9ac8b79f228d2cd30324d788648b818d [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}}
Barry Warsaw5db478f2002-10-01 04:33:16 +000036Return the entire message flatten as a string. When optional
37\var{unixfrom} is \code{True}, the envelope header is included in the
38returned string. \var{unixfrom} 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__}{}
Fred Drake6516e142002-10-01 14:29:58 +000042Equivalent to \method{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 Warsaw5db478f2002-10-01 04:33:16 +000062Add the given \var{payload} to the current payload, which must be
Barry Warsaw5b9da892002-10-01 01:05:52 +000063\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
Barry Warsaw5db478f2002-10-01 04:33:16 +000098\var{charset} sets the message's default character set; see
Barry Warsaw5b9da892002-10-01 01:05:52 +000099\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
Barry Warsaw5db478f2002-10-01 04:33:16 +0000106either be a \class{Charset} instance (see \refmodule{email.Charset}), a
Barry Warsaw5b9da892002-10-01 01:05:52 +0000107string 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
Barry Warsaw5db478f2002-10-01 04:33:16 +0000131the message's \rfc{2822} headers. Note that there are some
Barry Warsaw5e634632001-09-26 05:23:47 +0000132semantic 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
Barry Warsaw5db478f2002-10-01 04:33:16 +0000136\method{keys()}, but in a \class{Message} object, headers are always
137returned in the order they appeared in the original message, or were
138added to the message later. Any header deleted and then re-added are
139always appended to the end of the header list.
140
141These semantic differences are intentional and are biased toward
142maximal convenience.
Barry Warsaw5e634632001-09-26 05:23:47 +0000143
Barry Warsaw5b9da892002-10-01 01:05:52 +0000144Note that in all cases, any envelope header present in the message is
145not included in the mapping interface.
Barry Warsaw5e634632001-09-26 05:23:47 +0000146
147\begin{methoddesc}[Message]{__len__}{}
148Return the total number of headers, including duplicates.
149\end{methoddesc}
150
151\begin{methoddesc}[Message]{__contains__}{name}
152Return true if the message object has a field named \var{name}.
Andrew M. Kuchling43dc1fc2001-11-05 01:55:03 +0000153Matching is done case-insensitively and \var{name} should not include the
Barry Warsaw5e634632001-09-26 05:23:47 +0000154trailing colon. Used for the \code{in} operator,
155e.g.:
156
157\begin{verbatim}
158if 'message-id' in myMessage:
159 print 'Message-ID:', myMessage['message-id']
160\end{verbatim}
161\end{methoddesc}
162
163\begin{methoddesc}[Message]{__getitem__}{name}
164Return the value of the named header field. \var{name} should not
165include the colon field separator. If the header is missing,
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000166\code{None} is returned; a \exception{KeyError} is never raised.
Barry Warsaw5e634632001-09-26 05:23:47 +0000167
168Note that if the named field appears more than once in the message's
169headers, exactly which of those field values will be returned is
170undefined. Use the \method{get_all()} method to get the values of all
171the extant named headers.
172\end{methoddesc}
173
174\begin{methoddesc}[Message]{__setitem__}{name, val}
175Add a header to the message with field name \var{name} and value
176\var{val}. The field is appended to the end of the message's existing
177fields.
178
179Note that this does \emph{not} overwrite or delete any existing header
180with the same name. If you want to ensure that the new header is the
181only one present in the message with field name
Barry Warsaw5db478f2002-10-01 04:33:16 +0000182\var{name}, delete the field first, e.g.:
Barry Warsaw5e634632001-09-26 05:23:47 +0000183
184\begin{verbatim}
185del msg['subject']
186msg['subject'] = 'Python roolz!'
187\end{verbatim}
188\end{methoddesc}
189
190\begin{methoddesc}[Message]{__delitem__}{name}
191Delete all occurrences of the field with name \var{name} from the
192message's headers. No exception is raised if the named field isn't
193present in the headers.
194\end{methoddesc}
195
196\begin{methoddesc}[Message]{has_key}{name}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000197Return true if the message contains a header field named \var{name},
198otherwise return false.
Barry Warsaw5e634632001-09-26 05:23:47 +0000199\end{methoddesc}
200
201\begin{methoddesc}[Message]{keys}{}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000202Return a list of all the message's header field names.
Barry Warsaw5e634632001-09-26 05:23:47 +0000203\end{methoddesc}
204
205\begin{methoddesc}[Message]{values}{}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000206Return a list of all the message's field values.
Barry Warsaw5e634632001-09-26 05:23:47 +0000207\end{methoddesc}
208
209\begin{methoddesc}[Message]{items}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000210Return a list of 2-tuples containing all the message's field headers
Barry Warsaw5db478f2002-10-01 04:33:16 +0000211and values.
Barry Warsaw5e634632001-09-26 05:23:47 +0000212\end{methoddesc}
213
214\begin{methoddesc}[Message]{get}{name\optional{, failobj}}
215Return the value of the named header field. This is identical to
216\method{__getitem__()} except that optional \var{failobj} is returned
217if the named header is missing (defaults to \code{None}).
218\end{methoddesc}
219
220Here are some additional useful methods:
221
222\begin{methoddesc}[Message]{get_all}{name\optional{, failobj}}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000223Return a list of all the values for the field named \var{name}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000224If there are no such named headers in the message, \var{failobj} is
225returned (defaults to \code{None}).
226\end{methoddesc}
227
228\begin{methoddesc}[Message]{add_header}{_name, _value, **_params}
229Extended header setting. This method is similar to
230\method{__setitem__()} except that additional header parameters can be
Barry Warsaw5b9da892002-10-01 01:05:52 +0000231provided as keyword arguments. \var{_name} is the header field to add
232and \var{_value} is the \emph{primary} value for the header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000233
234For each item in the keyword argument dictionary \var{_params}, the
235key is taken as the parameter name, with underscores converted to
236dashes (since dashes are illegal in Python identifiers). Normally,
237the parameter will be added as \code{key="value"} unless the value is
238\code{None}, in which case only the key will be added.
239
240Here's an example:
241
242\begin{verbatim}
243msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
244\end{verbatim}
245
246This will add a header that looks like
247
248\begin{verbatim}
249Content-Disposition: attachment; filename="bud.gif"
250\end{verbatim}
251\end{methoddesc}
252
Barry Warsaw5b9da892002-10-01 01:05:52 +0000253\begin{methoddesc}[Message]{replace_header}{_name, _value}
254Replace a header. Replace the first header found in the message that
255matches \var{_name}, retaining header order and field name case. If
256no matching header was found, a \exception{KeyError} is raised.
257
258\versionadded{2.2.2}
259\end{methoddesc}
260
261\begin{methoddesc}[Message]{get_content_type}{}
262Return the message's content type. The returned string is coerced to
263lower case of the form \mimetype{maintype/subtype}. If there was no
264\mailheader{Content-Type} header in the message the default type as
265given by \method{get_default_type()} will be returned. Since
266according to \rfc{2045}, messages always have a default type,
267\method{get_content_type()} will always return a value.
268
269\rfc{2045} defines a message's default type to be
270\mimetype{text/plain} unless it appears inside a
271\mimetype{multipart/digest} container, in which case it would be
272\mimetype{message/rfc822}. If the \mailheader{Content-Type} header
273has an invalid type specification, \rfc{2045} mandates that the
274default type be \mimetype{text/plain}.
275
276\versionadded{2.2.2}
277\end{methoddesc}
278
279\begin{methoddesc}[Message]{get_content_maintype}{}
280Return the message's main content type. This is the
281\mimetype{maintype} part of the string returned by
282\method{get_content_type()}.
283
284\versionadded{2.2.2}
285\end{methoddesc}
286
287\begin{methoddesc}[Message]{get_content_subtype}{}
288Return the message's sub-content type. This is the \mimetype{subtype}
289part of the string returned by \method{get_content_type()}.
290
291\versionadded{2.2.2}
292\end{methoddesc}
293
294\begin{methoddesc}[Message]{get_default_type}{}
295Return the default content type. Most messages have a default content
296type of \mimetype{text/plain}, except for messages that are subparts
297of \mimetype{multipart/digest} containers. Such subparts have a
298default content type of \mimetype{message/rfc822}.
299
300\versionadded{2.2.2}
301\end{methoddesc}
302
303\begin{methoddesc}[Message]{set_default_type}{ctype}
304Set the default content type. \var{ctype} should either be
305\mimetype{text/plain} or \mimetype{message/rfc822}, although this is
306not enforced. The default content type is not stored in the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000307\mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000308
Barry Warsaw5b9da892002-10-01 01:05:52 +0000309\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000310\end{methoddesc}
311
Barry Warsaw5b9da892002-10-01 01:05:52 +0000312\begin{methoddesc}[Message]{get_params}{\optional{failobj\optional{,
313 header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000314Return the message's \mailheader{Content-Type} parameters, as a list. The
Barry Warsaw5e634632001-09-26 05:23:47 +0000315elements of the returned list are 2-tuples of key/value pairs, as
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000316split on the \character{=} sign. The left hand side of the
317\character{=} is the key, while the right hand side is the value. If
318there is no \character{=} sign in the parameter the value is the empty
Barry Warsaw5b9da892002-10-01 01:05:52 +0000319string, otherwise the value is as described in \method{get_param()} and is
320unquoted if optional \var{unquote} is \code{True} (the default).
Barry Warsaw5e634632001-09-26 05:23:47 +0000321
322Optional \var{failobj} is the object to return if there is no
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000323\mailheader{Content-Type} header. Optional \var{header} is the header to
324search instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000325
326\versionchanged[\var{unquote} argument added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000327\end{methoddesc}
328
329\begin{methoddesc}[Message]{get_param}{param\optional{,
Barry Warsaw5b9da892002-10-01 01:05:52 +0000330 failobj\optional{, header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000331Return the value of the \mailheader{Content-Type} header's parameter
332\var{param} as a string. If the message has no \mailheader{Content-Type}
Barry Warsaw5e634632001-09-26 05:23:47 +0000333header or if there is no such parameter, then \var{failobj} is
334returned (defaults to \code{None}).
335
336Optional \var{header} if given, specifies the message header to use
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000337instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000338
339Parameter keys are always compared case insensitively. The return
340value can either be a string, or a 3-tuple if the parameter was
341\rfc{2231} encoded. When it's a 3-tuple, the elements of the value are of
Barry Warsaw5db478f2002-10-01 04:33:16 +0000342the form \code{(CHARSET, LANGUAGE, VALUE)}, where \code{LANGUAGE} may
Barry Warsaw5b9da892002-10-01 01:05:52 +0000343be the empty string. Your application should be prepared to deal with
Barry Warsaw5db478f2002-10-01 04:33:16 +00003443-tuple return values, which it can convert to a Unicode string like
345so:
Barry Warsaw5b9da892002-10-01 01:05:52 +0000346
347\begin{verbatim}
348param = msg.get_param('foo')
349if isinstance(param, tuple):
350 param = unicode(param[2], param[0])
351\end{verbatim}
352
353In any case, the parameter value (either the returned string, or the
Barry Warsaw5db478f2002-10-01 04:33:16 +0000354\code{VALUE} item in the 3-tuple) is always unquoted, unless
Barry Warsaw5b9da892002-10-01 01:05:52 +0000355\var{unquote} is set to \code{False}.
356
357\versionchanged[\var{unquote} argument added, and 3-tuple return value
358possible]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000359\end{methoddesc}
360
Barry Warsaw5b9da892002-10-01 01:05:52 +0000361\begin{methoddesc}[Message]{set_param}{param, value\optional{,
362 header\optional{, requote\optional{, charset\optional{, language}}}}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000363
Barry Warsaw5b9da892002-10-01 01:05:52 +0000364Set a parameter in the \mailheader{Content-Type} header. If the
365parameter already exists in the header, its value will be replaced
366with \var{value}. If the \mailheader{Content-Type} header as not yet
367been defined for this message, it will be set to \mimetype{text/plain}
368and the new parameter value will be appended as per \rfc{2045}.
369
370Optional \var{header} specifies an alternative header to
371\mailheader{Content-Type}, and all parameters will be quoted as
372necessary unless optional \var{requote} is \code{False} (the default
373is \code{True}).
374
375If optional \var{charset} is specified, the parameter will be encoded
376according to \rfc{2231}. Optional \var{language} specifies the RFC
3772231 language, defaulting to the empty string. Both \var{charset} and
378\var{language} should be strings.
379
380\versionadded{2.2.2}
381\end{methoddesc}
382
383\begin{methoddesc}[Message]{del_param}{param\optional{, header\optional{,
384 requote}}}
385Remove the given parameter completely from the
386\mailheader{Content-Type} header. The header will be re-written in
387place without the parameter or its value. All values will be quoted
388as necessary unless \var{requote} is \code{False} (the default is
Barry Warsaw5db478f2002-10-01 04:33:16 +0000389\code{True}). Optional \var{header} specifies an alternative to
Barry Warsaw5b9da892002-10-01 01:05:52 +0000390\mailheader{Content-Type}.
391
392\versionadded{2.2.2}
393\end{methoddesc}
394
395\begin{methoddesc}[Message]{set_type}{type\optional{, header}\optional{,
396 requote}}
397Set the main type and subtype for the \mailheader{Content-Type}
398header. \var{type} must be a string in the form
399\mimetype{maintype/subtype}, otherwise a \exception{ValueError} is
400raised.
401
402This method replaces the \mailheader{Content-Type} header, keeping all
403the parameters in place. If \var{requote} is \code{False}, this
404leaves the existing header's quoting as is, otherwise the parameters
405will be quoted (the default).
406
407An alternative header can be specified in the \var{header} argument.
Barry Warsaw5db478f2002-10-01 04:33:16 +0000408When the \mailheader{Content-Type} header is set a
409\mailheader{MIME-Version} header is also added.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000410
411\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000412\end{methoddesc}
413
414\begin{methoddesc}[Message]{get_filename}{\optional{failobj}}
415Return the value of the \code{filename} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000416\mailheader{Content-Disposition} header of the message, or \var{failobj} if
Barry Warsaw5e634632001-09-26 05:23:47 +0000417either the header is missing, or has no \code{filename} parameter.
418The returned string will always be unquoted as per
419\method{Utils.unquote()}.
420\end{methoddesc}
421
422\begin{methoddesc}[Message]{get_boundary}{\optional{failobj}}
423Return the value of the \code{boundary} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000424\mailheader{Content-Type} header of the message, or \var{failobj} if either
Barry Warsaw5e634632001-09-26 05:23:47 +0000425the header is missing, or has no \code{boundary} parameter. The
426returned string will always be unquoted as per
427\method{Utils.unquote()}.
428\end{methoddesc}
429
430\begin{methoddesc}[Message]{set_boundary}{boundary}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000431Set the \code{boundary} parameter of the \mailheader{Content-Type}
432header to \var{boundary}. \method{set_boundary()} will always quote
433\var{boundary} if necessary. A \exception{HeaderParseError} is raised
434if the message object has no \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000435
436Note that using this method is subtly different than deleting the old
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000437\mailheader{Content-Type} header and adding a new one with the new boundary
Barry Warsaw5e634632001-09-26 05:23:47 +0000438via \method{add_header()}, because \method{set_boundary()} preserves the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000439order of the \mailheader{Content-Type} header in the list of headers.
Barry Warsaw5e634632001-09-26 05:23:47 +0000440However, it does \emph{not} preserve any continuation lines which may
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000441have been present in the original \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000442\end{methoddesc}
443
Barry Warsaw5b9da892002-10-01 01:05:52 +0000444\begin{methoddesc}[Message]{get_content_charset}{\optional{failobj}}
445Return the \code{charset} parameter of the \mailheader{Content-Type}
Barry Warsaw57ce1432002-10-10 15:22:16 +0000446header, coerced to lower case. If there is no
447\mailheader{Content-Type} header, or if that header has no
448\code{charset} parameter, \var{failobj} is returned.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000449
Barry Warsaw5db478f2002-10-01 04:33:16 +0000450Note that this method differs from \method{get_charset()} which
451returns the \class{Charset} instance for the default encoding of the
452message body.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000453
454\versionadded{2.2.2}
455\end{methoddesc}
456
457\begin{methoddesc}[Message]{get_charsets}{\optional{failobj}}
458Return a list containing the character set names in the message. If
459the message is a \mimetype{multipart}, then the list will contain one
460element for each subpart in the payload, otherwise, it will be a list
461of length 1.
462
463Each item in the list will be a string which is the value of the
464\code{charset} parameter in the \mailheader{Content-Type} header for the
465represented subpart. However, if the subpart has no
466\mailheader{Content-Type} header, no \code{charset} parameter, or is not of
467the \mimetype{text} main MIME type, then that item in the returned list
468will be \var{failobj}.
469\end{methoddesc}
470
Barry Warsaw5e634632001-09-26 05:23:47 +0000471\begin{methoddesc}[Message]{walk}{}
472The \method{walk()} method is an all-purpose generator which can be
473used to iterate over all the parts and subparts of a message object
474tree, in depth-first traversal order. You will typically use
Barry Warsaw5db478f2002-10-01 04:33:16 +0000475\method{walk()} as the iterator in a \code{for} loop; each
Barry Warsaw5e634632001-09-26 05:23:47 +0000476iteration returns the next subpart.
477
Barry Warsaw5db478f2002-10-01 04:33:16 +0000478Here's an example that prints the MIME type of every part of a
479multipart message structure:
Barry Warsaw5e634632001-09-26 05:23:47 +0000480
481\begin{verbatim}
482>>> for part in msg.walk():
Barry Warsaw5db478f2002-10-01 04:33:16 +0000483>>> print part.get_content_type()
Barry Warsaw5e634632001-09-26 05:23:47 +0000484multipart/report
485text/plain
486message/delivery-status
487text/plain
488text/plain
489message/rfc822
490\end{verbatim}
491\end{methoddesc}
492
493\class{Message} objects can also optionally contain two instance
494attributes, which can be used when generating the plain text of a MIME
495message.
496
497\begin{datadesc}{preamble}
498The format of a MIME document allows for some text between the blank
499line following the headers, and the first multipart boundary string.
500Normally, this text is never visible in a MIME-aware mail reader
501because it falls outside the standard MIME armor. However, when
502viewing the raw text of the message, or when viewing the message in a
503non-MIME aware reader, this text can become visible.
504
505The \var{preamble} attribute contains this leading extra-armor text
506for MIME documents. When the \class{Parser} discovers some text after
507the headers but before the first boundary string, it assigns this text
508to the message's \var{preamble} attribute. When the \class{Generator}
509is writing out the plain text representation of a MIME message, and it
510finds the message has a \var{preamble} attribute, it will write this
Barry Warsaw5b9da892002-10-01 01:05:52 +0000511text in the area between the headers and the first boundary. See
512\refmodule{email.Parser} and \refmodule{email.Generator} for details.
Barry Warsaw5e634632001-09-26 05:23:47 +0000513
514Note that if the message object has no preamble, the
515\var{preamble} attribute will be \code{None}.
516\end{datadesc}
517
518\begin{datadesc}{epilogue}
519The \var{epilogue} attribute acts the same way as the \var{preamble}
520attribute, except that it contains text that appears between the last
521boundary and the end of the message.
Barry Warsawe736d932001-10-19 04:34:42 +0000522
523One note: when generating the flat text for a \mimetype{multipart}
524message that has no \var{epilogue} (using the standard
525\class{Generator} class), no newline is added after the closing
526boundary line. If the message object has an \var{epilogue} and its
527value does not start with a newline, a newline is printed after the
528closing boundary. This seems a little clumsy, but it makes the most
529practical sense. The upshot is that if you want to ensure that a
530newline get printed after your closing \mimetype{multipart} boundary,
531set the \var{epilogue} to the empty string.
Barry Warsaw5e634632001-09-26 05:23:47 +0000532\end{datadesc}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000533
534\subsubsection{Deprecated methods}
535
536The following methods are deprecated in \module{email} version 2.
537They are documented here for completeness.
538
539\begin{methoddesc}[Message]{add_payload}{payload}
540Add \var{payload} to the message object's existing payload. If, prior
541to calling this method, the object's payload was \code{None}
542(i.e. never before set), then after this method is called, the payload
543will be the argument \var{payload}.
544
545If the object's payload was already a list
546(i.e. \method{is_multipart()} returns 1), then \var{payload} is
547appended to the end of the existing payload list.
548
549For any other type of existing payload, \method{add_payload()} will
550transform the new payload into a list consisting of the old payload
551and \var{payload}, but only if the document is already a MIME
552multipart document. This condition is satisfied if the message's
553\mailheader{Content-Type} header's main type is either
554\mimetype{multipart}, or there is no \mailheader{Content-Type}
555header. In any other situation,
556\exception{MultipartConversionError} is raised.
557
558\deprecated{2.2.2}{Use the \method{attach()} method instead.}
559\end{methoddesc}
560
561\begin{methoddesc}[Message]{get_type}{\optional{failobj}}
562Return the message's content type, as a string of the form
563\mimetype{maintype/subtype} as taken from the
564\mailheader{Content-Type} header.
565The returned string is coerced to lowercase.
566
567If there is no \mailheader{Content-Type} header in the message,
568\var{failobj} is returned (defaults to \code{None}).
569
570\deprecated{2.2.2}{Use the \method{get_content_type()} method instead.}
571\end{methoddesc}
572
573\begin{methoddesc}[Message]{get_main_type}{\optional{failobj}}
574Return the message's \emph{main} content type. This essentially returns the
575\var{maintype} part of the string returned by \method{get_type()}, with the
576same semantics for \var{failobj}.
577
578\deprecated{2.2.2}{Use the \method{get_content_maintype()} method instead.}
579\end{methoddesc}
580
581\begin{methoddesc}[Message]{get_subtype}{\optional{failobj}}
582Return the message's sub-content type. This essentially returns the
583\var{subtype} part of the string returned by \method{get_type()}, with the
584same semantics for \var{failobj}.
585
586\deprecated{2.2.2}{Use the \method{get_content_subtype()} method instead.}
587\end{methoddesc}
588