blob: 61dc74b255a4b29c6b34761e00cd2a904bdcd180 [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 Warsaw20ebc372003-03-10 16:13:50 +000090missing, or if the payload has bogus base64 data, the payload is
91returned as-is (undecoded). If the message is a multipart and the
92\var{decode} flag is \code{True}, then \code{None} is returned. The
93default for \var{decode} is \code{False}.
Barry Warsaw5e634632001-09-26 05:23:47 +000094\end{methoddesc}
95
Barry Warsaw5b9da892002-10-01 01:05:52 +000096\begin{methoddesc}[Message]{set_payload}{payload\optional{, charset}}
Barry Warsaw5e634632001-09-26 05:23:47 +000097Set the entire message object's payload to \var{payload}. It is the
Barry Warsaw5b9da892002-10-01 01:05:52 +000098client's responsibility to ensure the payload invariants. Optional
Barry Warsaw5db478f2002-10-01 04:33:16 +000099\var{charset} sets the message's default character set; see
Barry Warsaw5b9da892002-10-01 01:05:52 +0000100\method{set_charset()} for details.
101
102\versionchanged[\var{charset} argument added]{2.2.2}
103\end{methoddesc}
104
105\begin{methoddesc}[Message]{set_charset}{charset}
106Set the character set of the payload to \var{charset}, which can
Barry Warsaw5db478f2002-10-01 04:33:16 +0000107either be a \class{Charset} instance (see \refmodule{email.Charset}), a
Barry Warsaw5b9da892002-10-01 01:05:52 +0000108string naming a character set,
109or \code{None}. If it is a string, it will be converted to a
110\class{Charset} instance. If \var{charset} is \code{None}, the
111\code{charset} parameter will be removed from the
112\mailheader{Content-Type} header. Anything else will generate a
113\exception{TypeError}.
114
115The message will be assumed to be of type \mimetype{text/*} encoded with
116\code{charset.input_charset}. It will be converted to
117\code{charset.output_charset}
118and encoded properly, if needed, when generating the plain text
119representation of the message. MIME headers
120(\mailheader{MIME-Version}, \mailheader{Content-Type},
121\mailheader{Content-Transfer-Encoding}) will be added as needed.
122
123\versionadded{2.2.2}
124\end{methoddesc}
125
126\begin{methoddesc}[Message]{get_charset}{}
127Return the \class{Charset} instance associated with the message's payload.
128\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000129\end{methoddesc}
130
131The following methods implement a mapping-like interface for accessing
Barry Warsaw5db478f2002-10-01 04:33:16 +0000132the message's \rfc{2822} headers. Note that there are some
Barry Warsaw5e634632001-09-26 05:23:47 +0000133semantic differences between these methods and a normal mapping
134(i.e. dictionary) interface. For example, in a dictionary there are
135no duplicate keys, but here there may be duplicate message headers. Also,
136in dictionaries there is no guaranteed order to the keys returned by
Barry Warsaw5db478f2002-10-01 04:33:16 +0000137\method{keys()}, but in a \class{Message} object, headers are always
138returned in the order they appeared in the original message, or were
139added to the message later. Any header deleted and then re-added are
140always appended to the end of the header list.
141
142These semantic differences are intentional and are biased toward
143maximal convenience.
Barry Warsaw5e634632001-09-26 05:23:47 +0000144
Barry Warsaw5b9da892002-10-01 01:05:52 +0000145Note that in all cases, any envelope header present in the message is
146not included in the mapping interface.
Barry Warsaw5e634632001-09-26 05:23:47 +0000147
148\begin{methoddesc}[Message]{__len__}{}
149Return the total number of headers, including duplicates.
150\end{methoddesc}
151
152\begin{methoddesc}[Message]{__contains__}{name}
153Return true if the message object has a field named \var{name}.
Andrew M. Kuchling43dc1fc2001-11-05 01:55:03 +0000154Matching is done case-insensitively and \var{name} should not include the
Barry Warsaw5e634632001-09-26 05:23:47 +0000155trailing colon. Used for the \code{in} operator,
156e.g.:
157
158\begin{verbatim}
159if 'message-id' in myMessage:
160 print 'Message-ID:', myMessage['message-id']
161\end{verbatim}
162\end{methoddesc}
163
164\begin{methoddesc}[Message]{__getitem__}{name}
165Return the value of the named header field. \var{name} should not
166include the colon field separator. If the header is missing,
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000167\code{None} is returned; a \exception{KeyError} is never raised.
Barry Warsaw5e634632001-09-26 05:23:47 +0000168
169Note that if the named field appears more than once in the message's
170headers, exactly which of those field values will be returned is
171undefined. Use the \method{get_all()} method to get the values of all
172the extant named headers.
173\end{methoddesc}
174
175\begin{methoddesc}[Message]{__setitem__}{name, val}
176Add a header to the message with field name \var{name} and value
177\var{val}. The field is appended to the end of the message's existing
178fields.
179
180Note that this does \emph{not} overwrite or delete any existing header
181with the same name. If you want to ensure that the new header is the
182only one present in the message with field name
Barry Warsaw5db478f2002-10-01 04:33:16 +0000183\var{name}, delete the field first, e.g.:
Barry Warsaw5e634632001-09-26 05:23:47 +0000184
185\begin{verbatim}
186del msg['subject']
187msg['subject'] = 'Python roolz!'
188\end{verbatim}
189\end{methoddesc}
190
191\begin{methoddesc}[Message]{__delitem__}{name}
192Delete all occurrences of the field with name \var{name} from the
193message's headers. No exception is raised if the named field isn't
194present in the headers.
195\end{methoddesc}
196
197\begin{methoddesc}[Message]{has_key}{name}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000198Return true if the message contains a header field named \var{name},
199otherwise return false.
Barry Warsaw5e634632001-09-26 05:23:47 +0000200\end{methoddesc}
201
202\begin{methoddesc}[Message]{keys}{}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000203Return a list of all the message's header field names.
Barry Warsaw5e634632001-09-26 05:23:47 +0000204\end{methoddesc}
205
206\begin{methoddesc}[Message]{values}{}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000207Return a list of all the message's field values.
Barry Warsaw5e634632001-09-26 05:23:47 +0000208\end{methoddesc}
209
210\begin{methoddesc}[Message]{items}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000211Return a list of 2-tuples containing all the message's field headers
Barry Warsaw5db478f2002-10-01 04:33:16 +0000212and values.
Barry Warsaw5e634632001-09-26 05:23:47 +0000213\end{methoddesc}
214
215\begin{methoddesc}[Message]{get}{name\optional{, failobj}}
216Return the value of the named header field. This is identical to
217\method{__getitem__()} except that optional \var{failobj} is returned
218if the named header is missing (defaults to \code{None}).
219\end{methoddesc}
220
221Here are some additional useful methods:
222
223\begin{methoddesc}[Message]{get_all}{name\optional{, failobj}}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000224Return a list of all the values for the field named \var{name}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000225If there are no such named headers in the message, \var{failobj} is
226returned (defaults to \code{None}).
227\end{methoddesc}
228
229\begin{methoddesc}[Message]{add_header}{_name, _value, **_params}
230Extended header setting. This method is similar to
231\method{__setitem__()} except that additional header parameters can be
Barry Warsaw5b9da892002-10-01 01:05:52 +0000232provided as keyword arguments. \var{_name} is the header field to add
233and \var{_value} is the \emph{primary} value for the header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000234
235For each item in the keyword argument dictionary \var{_params}, the
236key is taken as the parameter name, with underscores converted to
237dashes (since dashes are illegal in Python identifiers). Normally,
238the parameter will be added as \code{key="value"} unless the value is
239\code{None}, in which case only the key will be added.
240
241Here's an example:
242
243\begin{verbatim}
244msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
245\end{verbatim}
246
247This will add a header that looks like
248
249\begin{verbatim}
250Content-Disposition: attachment; filename="bud.gif"
251\end{verbatim}
252\end{methoddesc}
253
Barry Warsaw5b9da892002-10-01 01:05:52 +0000254\begin{methoddesc}[Message]{replace_header}{_name, _value}
255Replace a header. Replace the first header found in the message that
256matches \var{_name}, retaining header order and field name case. If
257no matching header was found, a \exception{KeyError} is raised.
258
259\versionadded{2.2.2}
260\end{methoddesc}
261
262\begin{methoddesc}[Message]{get_content_type}{}
263Return the message's content type. The returned string is coerced to
264lower case of the form \mimetype{maintype/subtype}. If there was no
265\mailheader{Content-Type} header in the message the default type as
266given by \method{get_default_type()} will be returned. Since
267according to \rfc{2045}, messages always have a default type,
268\method{get_content_type()} will always return a value.
269
270\rfc{2045} defines a message's default type to be
271\mimetype{text/plain} unless it appears inside a
272\mimetype{multipart/digest} container, in which case it would be
273\mimetype{message/rfc822}. If the \mailheader{Content-Type} header
274has an invalid type specification, \rfc{2045} mandates that the
275default type be \mimetype{text/plain}.
276
277\versionadded{2.2.2}
278\end{methoddesc}
279
280\begin{methoddesc}[Message]{get_content_maintype}{}
281Return the message's main content type. This is the
282\mimetype{maintype} part of the string returned by
283\method{get_content_type()}.
284
285\versionadded{2.2.2}
286\end{methoddesc}
287
288\begin{methoddesc}[Message]{get_content_subtype}{}
289Return the message's sub-content type. This is the \mimetype{subtype}
290part of the string returned by \method{get_content_type()}.
291
292\versionadded{2.2.2}
293\end{methoddesc}
294
295\begin{methoddesc}[Message]{get_default_type}{}
296Return the default content type. Most messages have a default content
297type of \mimetype{text/plain}, except for messages that are subparts
298of \mimetype{multipart/digest} containers. Such subparts have a
299default content type of \mimetype{message/rfc822}.
300
301\versionadded{2.2.2}
302\end{methoddesc}
303
304\begin{methoddesc}[Message]{set_default_type}{ctype}
305Set the default content type. \var{ctype} should either be
306\mimetype{text/plain} or \mimetype{message/rfc822}, although this is
307not enforced. The default content type is not stored in the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000308\mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000309
Barry Warsaw5b9da892002-10-01 01:05:52 +0000310\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000311\end{methoddesc}
312
Barry Warsaw5b9da892002-10-01 01:05:52 +0000313\begin{methoddesc}[Message]{get_params}{\optional{failobj\optional{,
314 header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000315Return the message's \mailheader{Content-Type} parameters, as a list. The
Barry Warsaw5e634632001-09-26 05:23:47 +0000316elements of the returned list are 2-tuples of key/value pairs, as
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000317split on the \character{=} sign. The left hand side of the
318\character{=} is the key, while the right hand side is the value. If
319there is no \character{=} sign in the parameter the value is the empty
Barry Warsaw5b9da892002-10-01 01:05:52 +0000320string, otherwise the value is as described in \method{get_param()} and is
321unquoted if optional \var{unquote} is \code{True} (the default).
Barry Warsaw5e634632001-09-26 05:23:47 +0000322
323Optional \var{failobj} is the object to return if there is no
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000324\mailheader{Content-Type} header. Optional \var{header} is the header to
325search instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000326
327\versionchanged[\var{unquote} argument added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000328\end{methoddesc}
329
330\begin{methoddesc}[Message]{get_param}{param\optional{,
Barry Warsaw5b9da892002-10-01 01:05:52 +0000331 failobj\optional{, header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000332Return the value of the \mailheader{Content-Type} header's parameter
333\var{param} as a string. If the message has no \mailheader{Content-Type}
Barry Warsaw5e634632001-09-26 05:23:47 +0000334header or if there is no such parameter, then \var{failobj} is
335returned (defaults to \code{None}).
336
337Optional \var{header} if given, specifies the message header to use
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000338instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000339
340Parameter keys are always compared case insensitively. The return
341value can either be a string, or a 3-tuple if the parameter was
342\rfc{2231} encoded. When it's a 3-tuple, the elements of the value are of
Barry Warsaw5db478f2002-10-01 04:33:16 +0000343the form \code{(CHARSET, LANGUAGE, VALUE)}, where \code{LANGUAGE} may
Barry Warsaw5b9da892002-10-01 01:05:52 +0000344be the empty string. Your application should be prepared to deal with
Barry Warsaw5db478f2002-10-01 04:33:16 +00003453-tuple return values, which it can convert to a Unicode string like
346so:
Barry Warsaw5b9da892002-10-01 01:05:52 +0000347
348\begin{verbatim}
349param = msg.get_param('foo')
350if isinstance(param, tuple):
351 param = unicode(param[2], param[0])
352\end{verbatim}
353
354In any case, the parameter value (either the returned string, or the
Barry Warsaw5db478f2002-10-01 04:33:16 +0000355\code{VALUE} item in the 3-tuple) is always unquoted, unless
Barry Warsaw5b9da892002-10-01 01:05:52 +0000356\var{unquote} is set to \code{False}.
357
358\versionchanged[\var{unquote} argument added, and 3-tuple return value
359possible]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000360\end{methoddesc}
361
Barry Warsaw5b9da892002-10-01 01:05:52 +0000362\begin{methoddesc}[Message]{set_param}{param, value\optional{,
363 header\optional{, requote\optional{, charset\optional{, language}}}}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000364
Barry Warsaw5b9da892002-10-01 01:05:52 +0000365Set a parameter in the \mailheader{Content-Type} header. If the
366parameter already exists in the header, its value will be replaced
367with \var{value}. If the \mailheader{Content-Type} header as not yet
368been defined for this message, it will be set to \mimetype{text/plain}
369and the new parameter value will be appended as per \rfc{2045}.
370
371Optional \var{header} specifies an alternative header to
372\mailheader{Content-Type}, and all parameters will be quoted as
373necessary unless optional \var{requote} is \code{False} (the default
374is \code{True}).
375
376If optional \var{charset} is specified, the parameter will be encoded
377according to \rfc{2231}. Optional \var{language} specifies the RFC
3782231 language, defaulting to the empty string. Both \var{charset} and
379\var{language} should be strings.
380
381\versionadded{2.2.2}
382\end{methoddesc}
383
384\begin{methoddesc}[Message]{del_param}{param\optional{, header\optional{,
385 requote}}}
386Remove the given parameter completely from the
387\mailheader{Content-Type} header. The header will be re-written in
388place without the parameter or its value. All values will be quoted
389as necessary unless \var{requote} is \code{False} (the default is
Barry Warsaw5db478f2002-10-01 04:33:16 +0000390\code{True}). Optional \var{header} specifies an alternative to
Barry Warsaw5b9da892002-10-01 01:05:52 +0000391\mailheader{Content-Type}.
392
393\versionadded{2.2.2}
394\end{methoddesc}
395
396\begin{methoddesc}[Message]{set_type}{type\optional{, header}\optional{,
397 requote}}
398Set the main type and subtype for the \mailheader{Content-Type}
399header. \var{type} must be a string in the form
400\mimetype{maintype/subtype}, otherwise a \exception{ValueError} is
401raised.
402
403This method replaces the \mailheader{Content-Type} header, keeping all
404the parameters in place. If \var{requote} is \code{False}, this
405leaves the existing header's quoting as is, otherwise the parameters
406will be quoted (the default).
407
408An alternative header can be specified in the \var{header} argument.
Barry Warsaw5db478f2002-10-01 04:33:16 +0000409When the \mailheader{Content-Type} header is set a
410\mailheader{MIME-Version} header is also added.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000411
412\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000413\end{methoddesc}
414
415\begin{methoddesc}[Message]{get_filename}{\optional{failobj}}
416Return the value of the \code{filename} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000417\mailheader{Content-Disposition} header of the message, or \var{failobj} if
Barry Warsaw5e634632001-09-26 05:23:47 +0000418either the header is missing, or has no \code{filename} parameter.
419The returned string will always be unquoted as per
420\method{Utils.unquote()}.
421\end{methoddesc}
422
423\begin{methoddesc}[Message]{get_boundary}{\optional{failobj}}
424Return the value of the \code{boundary} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000425\mailheader{Content-Type} header of the message, or \var{failobj} if either
Barry Warsaw5e634632001-09-26 05:23:47 +0000426the header is missing, or has no \code{boundary} parameter. The
427returned string will always be unquoted as per
428\method{Utils.unquote()}.
429\end{methoddesc}
430
431\begin{methoddesc}[Message]{set_boundary}{boundary}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000432Set the \code{boundary} parameter of the \mailheader{Content-Type}
433header to \var{boundary}. \method{set_boundary()} will always quote
434\var{boundary} if necessary. A \exception{HeaderParseError} is raised
435if the message object has no \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000436
437Note that using this method is subtly different than deleting the old
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000438\mailheader{Content-Type} header and adding a new one with the new boundary
Barry Warsaw5e634632001-09-26 05:23:47 +0000439via \method{add_header()}, because \method{set_boundary()} preserves the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000440order of the \mailheader{Content-Type} header in the list of headers.
Barry Warsaw5e634632001-09-26 05:23:47 +0000441However, it does \emph{not} preserve any continuation lines which may
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000442have been present in the original \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000443\end{methoddesc}
444
Barry Warsaw5b9da892002-10-01 01:05:52 +0000445\begin{methoddesc}[Message]{get_content_charset}{\optional{failobj}}
446Return the \code{charset} parameter of the \mailheader{Content-Type}
Barry Warsaw57ce1432002-10-10 15:22:16 +0000447header, coerced to lower case. If there is no
448\mailheader{Content-Type} header, or if that header has no
449\code{charset} parameter, \var{failobj} is returned.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000450
Barry Warsaw5db478f2002-10-01 04:33:16 +0000451Note that this method differs from \method{get_charset()} which
452returns the \class{Charset} instance for the default encoding of the
453message body.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000454
455\versionadded{2.2.2}
456\end{methoddesc}
457
458\begin{methoddesc}[Message]{get_charsets}{\optional{failobj}}
459Return a list containing the character set names in the message. If
460the message is a \mimetype{multipart}, then the list will contain one
461element for each subpart in the payload, otherwise, it will be a list
462of length 1.
463
464Each item in the list will be a string which is the value of the
465\code{charset} parameter in the \mailheader{Content-Type} header for the
466represented subpart. However, if the subpart has no
467\mailheader{Content-Type} header, no \code{charset} parameter, or is not of
468the \mimetype{text} main MIME type, then that item in the returned list
469will be \var{failobj}.
470\end{methoddesc}
471
Barry Warsaw5e634632001-09-26 05:23:47 +0000472\begin{methoddesc}[Message]{walk}{}
473The \method{walk()} method is an all-purpose generator which can be
474used to iterate over all the parts and subparts of a message object
475tree, in depth-first traversal order. You will typically use
Barry Warsaw5db478f2002-10-01 04:33:16 +0000476\method{walk()} as the iterator in a \code{for} loop; each
Barry Warsaw5e634632001-09-26 05:23:47 +0000477iteration returns the next subpart.
478
Barry Warsaw5db478f2002-10-01 04:33:16 +0000479Here's an example that prints the MIME type of every part of a
480multipart message structure:
Barry Warsaw5e634632001-09-26 05:23:47 +0000481
482\begin{verbatim}
483>>> for part in msg.walk():
Barry Warsaw5db478f2002-10-01 04:33:16 +0000484>>> print part.get_content_type()
Barry Warsaw5e634632001-09-26 05:23:47 +0000485multipart/report
486text/plain
487message/delivery-status
488text/plain
489text/plain
490message/rfc822
491\end{verbatim}
492\end{methoddesc}
493
494\class{Message} objects can also optionally contain two instance
495attributes, which can be used when generating the plain text of a MIME
496message.
497
498\begin{datadesc}{preamble}
499The format of a MIME document allows for some text between the blank
500line following the headers, and the first multipart boundary string.
501Normally, this text is never visible in a MIME-aware mail reader
502because it falls outside the standard MIME armor. However, when
503viewing the raw text of the message, or when viewing the message in a
504non-MIME aware reader, this text can become visible.
505
506The \var{preamble} attribute contains this leading extra-armor text
507for MIME documents. When the \class{Parser} discovers some text after
508the headers but before the first boundary string, it assigns this text
509to the message's \var{preamble} attribute. When the \class{Generator}
510is writing out the plain text representation of a MIME message, and it
511finds the message has a \var{preamble} attribute, it will write this
Barry Warsaw5b9da892002-10-01 01:05:52 +0000512text in the area between the headers and the first boundary. See
513\refmodule{email.Parser} and \refmodule{email.Generator} for details.
Barry Warsaw5e634632001-09-26 05:23:47 +0000514
515Note that if the message object has no preamble, the
516\var{preamble} attribute will be \code{None}.
517\end{datadesc}
518
519\begin{datadesc}{epilogue}
520The \var{epilogue} attribute acts the same way as the \var{preamble}
521attribute, except that it contains text that appears between the last
522boundary and the end of the message.
Barry Warsawe736d932001-10-19 04:34:42 +0000523
524One note: when generating the flat text for a \mimetype{multipart}
525message that has no \var{epilogue} (using the standard
526\class{Generator} class), no newline is added after the closing
527boundary line. If the message object has an \var{epilogue} and its
528value does not start with a newline, a newline is printed after the
529closing boundary. This seems a little clumsy, but it makes the most
530practical sense. The upshot is that if you want to ensure that a
531newline get printed after your closing \mimetype{multipart} boundary,
532set the \var{epilogue} to the empty string.
Barry Warsaw5e634632001-09-26 05:23:47 +0000533\end{datadesc}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000534
535\subsubsection{Deprecated methods}
536
537The following methods are deprecated in \module{email} version 2.
538They are documented here for completeness.
539
540\begin{methoddesc}[Message]{add_payload}{payload}
541Add \var{payload} to the message object's existing payload. If, prior
542to calling this method, the object's payload was \code{None}
543(i.e. never before set), then after this method is called, the payload
544will be the argument \var{payload}.
545
546If the object's payload was already a list
547(i.e. \method{is_multipart()} returns 1), then \var{payload} is
548appended to the end of the existing payload list.
549
550For any other type of existing payload, \method{add_payload()} will
551transform the new payload into a list consisting of the old payload
552and \var{payload}, but only if the document is already a MIME
553multipart document. This condition is satisfied if the message's
554\mailheader{Content-Type} header's main type is either
555\mimetype{multipart}, or there is no \mailheader{Content-Type}
556header. In any other situation,
557\exception{MultipartConversionError} is raised.
558
559\deprecated{2.2.2}{Use the \method{attach()} method instead.}
560\end{methoddesc}
561
562\begin{methoddesc}[Message]{get_type}{\optional{failobj}}
563Return the message's content type, as a string of the form
564\mimetype{maintype/subtype} as taken from the
565\mailheader{Content-Type} header.
566The returned string is coerced to lowercase.
567
568If there is no \mailheader{Content-Type} header in the message,
569\var{failobj} is returned (defaults to \code{None}).
570
571\deprecated{2.2.2}{Use the \method{get_content_type()} method instead.}
572\end{methoddesc}
573
574\begin{methoddesc}[Message]{get_main_type}{\optional{failobj}}
575Return the message's \emph{main} content type. This essentially returns the
576\var{maintype} part of the string returned by \method{get_type()}, with the
577same semantics for \var{failobj}.
578
579\deprecated{2.2.2}{Use the \method{get_content_maintype()} method instead.}
580\end{methoddesc}
581
582\begin{methoddesc}[Message]{get_subtype}{\optional{failobj}}
583Return the message's sub-content type. This essentially returns the
584\var{subtype} part of the string returned by \method{get_type()}, with the
585same semantics for \var{failobj}.
586
587\deprecated{2.2.2}{Use the \method{get_content_subtype()} method instead.}
588\end{methoddesc}
589