blob: f580edfc91206a80809981bb76baf7df6c196600 [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 Warsawb05df572003-04-18 23:03:53 +000039
40Note that this method is provided as a convenience and may not always
41format the message the way you want. For more flexibility,
42instantiate a \class{Generator} instance and use its
43\method{flatten()} method directly. For example:
44
45\begin{verbatim}
46from cStringIO import StringIO
47from email.Generator import Generator
48fp = StringIO()
49g = Generator(mangle_from_=False, maxheaderlen=60)
50g.flatten(msg)
51text = fp.getvalue()
52\end{verbatim}
Barry Warsaw5e634632001-09-26 05:23:47 +000053\end{methoddesc}
54
Fred Drake7779b202002-05-22 20:44:03 +000055\begin{methoddesc}[Message]{__str__}{}
Fred Drake6516e142002-10-01 14:29:58 +000056Equivalent to \method{as_string(unixfrom=True)}.
Barry Warsaw5e634632001-09-26 05:23:47 +000057\end{methoddesc}
58
59\begin{methoddesc}[Message]{is_multipart}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +000060Return \code{True} if the message's payload is a list of
61sub-\class{Message} objects, otherwise return \code{False}. When
62\method{is_multipart()} returns False, the payload should be a string
63object.
Barry Warsaw5e634632001-09-26 05:23:47 +000064\end{methoddesc}
65
66\begin{methoddesc}[Message]{set_unixfrom}{unixfrom}
Barry Warsaw5b9da892002-10-01 01:05:52 +000067Set the message's envelope header to \var{unixfrom}, which should be a string.
Barry Warsaw5e634632001-09-26 05:23:47 +000068\end{methoddesc}
69
70\begin{methoddesc}[Message]{get_unixfrom}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +000071Return the message's envelope header. Defaults to \code{None} if the
72envelope header was never set.
Barry Warsaw5e634632001-09-26 05:23:47 +000073\end{methoddesc}
74
75\begin{methoddesc}[Message]{attach}{payload}
Barry Warsaw5db478f2002-10-01 04:33:16 +000076Add the given \var{payload} to the current payload, which must be
Barry Warsaw5b9da892002-10-01 01:05:52 +000077\code{None} or a list of \class{Message} objects before the call.
78After the call, the payload will always be a list of \class{Message}
79objects. If you want to set the payload to a scalar object (e.g. a
80string), use \method{set_payload()} instead.
Barry Warsaw5e634632001-09-26 05:23:47 +000081\end{methoddesc}
82
83\begin{methoddesc}[Message]{get_payload}{\optional{i\optional{, decode}}}
Barry Warsaw5b9da892002-10-01 01:05:52 +000084Return a reference the current payload, which will be a list of
85\class{Message} objects when \method{is_multipart()} is \code{True}, or a
86string when \method{is_multipart()} is \code{False}. If the
87payload is a list and you mutate the list object, you modify the
88message's payload in place.
Barry Warsaw5e634632001-09-26 05:23:47 +000089
Barry Warsaw5b9da892002-10-01 01:05:52 +000090With optional argument \var{i}, \method{get_payload()} will return the
Barry Warsaw5e634632001-09-26 05:23:47 +000091\var{i}-th element of the payload, counting from zero, if
Barry Warsaw5b9da892002-10-01 01:05:52 +000092\method{is_multipart()} is \code{True}. An \exception{IndexError}
93will be raised if \var{i} is less than 0 or greater than or equal to
94the number of items in the payload. If the payload is a string
95(i.e. \method{is_multipart()} is \code{False}) and \var{i} is given, a
Barry Warsawc5f8fe32001-09-26 22:21:52 +000096\exception{TypeError} is raised.
Barry Warsaw5e634632001-09-26 05:23:47 +000097
98Optional \var{decode} is a flag indicating whether the payload should be
Barry Warsawc5f8fe32001-09-26 22:21:52 +000099decoded or not, according to the \mailheader{Content-Transfer-Encoding} header.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000100When \code{True} and the message is not a multipart, the payload will be
Barry Warsaw5e634632001-09-26 05:23:47 +0000101decoded if this header's value is \samp{quoted-printable} or
102\samp{base64}. If some other encoding is used, or
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000103\mailheader{Content-Transfer-Encoding} header is
Barry Warsaw20ebc372003-03-10 16:13:50 +0000104missing, or if the payload has bogus base64 data, the payload is
105returned as-is (undecoded). If the message is a multipart and the
106\var{decode} flag is \code{True}, then \code{None} is returned. The
107default for \var{decode} is \code{False}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000108\end{methoddesc}
109
Barry Warsaw5b9da892002-10-01 01:05:52 +0000110\begin{methoddesc}[Message]{set_payload}{payload\optional{, charset}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000111Set the entire message object's payload to \var{payload}. It is the
Barry Warsaw5b9da892002-10-01 01:05:52 +0000112client's responsibility to ensure the payload invariants. Optional
Barry Warsaw5db478f2002-10-01 04:33:16 +0000113\var{charset} sets the message's default character set; see
Barry Warsaw5b9da892002-10-01 01:05:52 +0000114\method{set_charset()} for details.
115
116\versionchanged[\var{charset} argument added]{2.2.2}
117\end{methoddesc}
118
119\begin{methoddesc}[Message]{set_charset}{charset}
120Set the character set of the payload to \var{charset}, which can
Barry Warsaw5db478f2002-10-01 04:33:16 +0000121either be a \class{Charset} instance (see \refmodule{email.Charset}), a
Barry Warsaw5b9da892002-10-01 01:05:52 +0000122string naming a character set,
123or \code{None}. If it is a string, it will be converted to a
124\class{Charset} instance. If \var{charset} is \code{None}, the
125\code{charset} parameter will be removed from the
126\mailheader{Content-Type} header. Anything else will generate a
127\exception{TypeError}.
128
129The message will be assumed to be of type \mimetype{text/*} encoded with
130\code{charset.input_charset}. It will be converted to
131\code{charset.output_charset}
132and encoded properly, if needed, when generating the plain text
133representation of the message. MIME headers
134(\mailheader{MIME-Version}, \mailheader{Content-Type},
135\mailheader{Content-Transfer-Encoding}) will be added as needed.
136
137\versionadded{2.2.2}
138\end{methoddesc}
139
140\begin{methoddesc}[Message]{get_charset}{}
141Return the \class{Charset} instance associated with the message's payload.
142\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000143\end{methoddesc}
144
145The following methods implement a mapping-like interface for accessing
Barry Warsaw5db478f2002-10-01 04:33:16 +0000146the message's \rfc{2822} headers. Note that there are some
Barry Warsaw5e634632001-09-26 05:23:47 +0000147semantic differences between these methods and a normal mapping
148(i.e. dictionary) interface. For example, in a dictionary there are
149no duplicate keys, but here there may be duplicate message headers. Also,
150in dictionaries there is no guaranteed order to the keys returned by
Barry Warsaw5db478f2002-10-01 04:33:16 +0000151\method{keys()}, but in a \class{Message} object, headers are always
152returned in the order they appeared in the original message, or were
153added to the message later. Any header deleted and then re-added are
154always appended to the end of the header list.
155
156These semantic differences are intentional and are biased toward
157maximal convenience.
Barry Warsaw5e634632001-09-26 05:23:47 +0000158
Barry Warsaw5b9da892002-10-01 01:05:52 +0000159Note that in all cases, any envelope header present in the message is
160not included in the mapping interface.
Barry Warsaw5e634632001-09-26 05:23:47 +0000161
162\begin{methoddesc}[Message]{__len__}{}
163Return the total number of headers, including duplicates.
164\end{methoddesc}
165
166\begin{methoddesc}[Message]{__contains__}{name}
167Return true if the message object has a field named \var{name}.
Andrew M. Kuchling43dc1fc2001-11-05 01:55:03 +0000168Matching is done case-insensitively and \var{name} should not include the
Barry Warsaw5e634632001-09-26 05:23:47 +0000169trailing colon. Used for the \code{in} operator,
170e.g.:
171
172\begin{verbatim}
173if 'message-id' in myMessage:
174 print 'Message-ID:', myMessage['message-id']
175\end{verbatim}
176\end{methoddesc}
177
178\begin{methoddesc}[Message]{__getitem__}{name}
179Return the value of the named header field. \var{name} should not
180include the colon field separator. If the header is missing,
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000181\code{None} is returned; a \exception{KeyError} is never raised.
Barry Warsaw5e634632001-09-26 05:23:47 +0000182
183Note that if the named field appears more than once in the message's
184headers, exactly which of those field values will be returned is
185undefined. Use the \method{get_all()} method to get the values of all
186the extant named headers.
187\end{methoddesc}
188
189\begin{methoddesc}[Message]{__setitem__}{name, val}
190Add a header to the message with field name \var{name} and value
191\var{val}. The field is appended to the end of the message's existing
192fields.
193
194Note that this does \emph{not} overwrite or delete any existing header
195with the same name. If you want to ensure that the new header is the
196only one present in the message with field name
Barry Warsaw5db478f2002-10-01 04:33:16 +0000197\var{name}, delete the field first, e.g.:
Barry Warsaw5e634632001-09-26 05:23:47 +0000198
199\begin{verbatim}
200del msg['subject']
201msg['subject'] = 'Python roolz!'
202\end{verbatim}
203\end{methoddesc}
204
205\begin{methoddesc}[Message]{__delitem__}{name}
206Delete all occurrences of the field with name \var{name} from the
207message's headers. No exception is raised if the named field isn't
208present in the headers.
209\end{methoddesc}
210
211\begin{methoddesc}[Message]{has_key}{name}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000212Return true if the message contains a header field named \var{name},
213otherwise return false.
Barry Warsaw5e634632001-09-26 05:23:47 +0000214\end{methoddesc}
215
216\begin{methoddesc}[Message]{keys}{}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000217Return a list of all the message's header field names.
Barry Warsaw5e634632001-09-26 05:23:47 +0000218\end{methoddesc}
219
220\begin{methoddesc}[Message]{values}{}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000221Return a list of all the message's field values.
Barry Warsaw5e634632001-09-26 05:23:47 +0000222\end{methoddesc}
223
224\begin{methoddesc}[Message]{items}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000225Return a list of 2-tuples containing all the message's field headers
Barry Warsaw5db478f2002-10-01 04:33:16 +0000226and values.
Barry Warsaw5e634632001-09-26 05:23:47 +0000227\end{methoddesc}
228
229\begin{methoddesc}[Message]{get}{name\optional{, failobj}}
230Return the value of the named header field. This is identical to
231\method{__getitem__()} except that optional \var{failobj} is returned
232if the named header is missing (defaults to \code{None}).
233\end{methoddesc}
234
235Here are some additional useful methods:
236
237\begin{methoddesc}[Message]{get_all}{name\optional{, failobj}}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000238Return a list of all the values for the field named \var{name}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000239If there are no such named headers in the message, \var{failobj} is
240returned (defaults to \code{None}).
241\end{methoddesc}
242
243\begin{methoddesc}[Message]{add_header}{_name, _value, **_params}
244Extended header setting. This method is similar to
245\method{__setitem__()} except that additional header parameters can be
Barry Warsaw5b9da892002-10-01 01:05:52 +0000246provided as keyword arguments. \var{_name} is the header field to add
247and \var{_value} is the \emph{primary} value for the header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000248
249For each item in the keyword argument dictionary \var{_params}, the
250key is taken as the parameter name, with underscores converted to
251dashes (since dashes are illegal in Python identifiers). Normally,
252the parameter will be added as \code{key="value"} unless the value is
253\code{None}, in which case only the key will be added.
254
255Here's an example:
256
257\begin{verbatim}
258msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
259\end{verbatim}
260
261This will add a header that looks like
262
263\begin{verbatim}
264Content-Disposition: attachment; filename="bud.gif"
265\end{verbatim}
266\end{methoddesc}
267
Barry Warsaw5b9da892002-10-01 01:05:52 +0000268\begin{methoddesc}[Message]{replace_header}{_name, _value}
269Replace a header. Replace the first header found in the message that
270matches \var{_name}, retaining header order and field name case. If
271no matching header was found, a \exception{KeyError} is raised.
272
273\versionadded{2.2.2}
274\end{methoddesc}
275
276\begin{methoddesc}[Message]{get_content_type}{}
277Return the message's content type. The returned string is coerced to
278lower case of the form \mimetype{maintype/subtype}. If there was no
279\mailheader{Content-Type} header in the message the default type as
280given by \method{get_default_type()} will be returned. Since
281according to \rfc{2045}, messages always have a default type,
282\method{get_content_type()} will always return a value.
283
284\rfc{2045} defines a message's default type to be
285\mimetype{text/plain} unless it appears inside a
286\mimetype{multipart/digest} container, in which case it would be
287\mimetype{message/rfc822}. If the \mailheader{Content-Type} header
288has an invalid type specification, \rfc{2045} mandates that the
289default type be \mimetype{text/plain}.
290
291\versionadded{2.2.2}
292\end{methoddesc}
293
294\begin{methoddesc}[Message]{get_content_maintype}{}
295Return the message's main content type. This is the
296\mimetype{maintype} part of the string returned by
297\method{get_content_type()}.
298
299\versionadded{2.2.2}
300\end{methoddesc}
301
302\begin{methoddesc}[Message]{get_content_subtype}{}
303Return the message's sub-content type. This is the \mimetype{subtype}
304part of the string returned by \method{get_content_type()}.
305
306\versionadded{2.2.2}
307\end{methoddesc}
308
309\begin{methoddesc}[Message]{get_default_type}{}
310Return the default content type. Most messages have a default content
311type of \mimetype{text/plain}, except for messages that are subparts
312of \mimetype{multipart/digest} containers. Such subparts have a
313default content type of \mimetype{message/rfc822}.
314
315\versionadded{2.2.2}
316\end{methoddesc}
317
318\begin{methoddesc}[Message]{set_default_type}{ctype}
319Set the default content type. \var{ctype} should either be
320\mimetype{text/plain} or \mimetype{message/rfc822}, although this is
321not enforced. The default content type is not stored in the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000322\mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000323
Barry Warsaw5b9da892002-10-01 01:05:52 +0000324\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000325\end{methoddesc}
326
Barry Warsaw5b9da892002-10-01 01:05:52 +0000327\begin{methoddesc}[Message]{get_params}{\optional{failobj\optional{,
328 header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000329Return the message's \mailheader{Content-Type} parameters, as a list. The
Barry Warsaw5e634632001-09-26 05:23:47 +0000330elements of the returned list are 2-tuples of key/value pairs, as
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000331split on the \character{=} sign. The left hand side of the
332\character{=} is the key, while the right hand side is the value. If
333there is no \character{=} sign in the parameter the value is the empty
Barry Warsaw5b9da892002-10-01 01:05:52 +0000334string, otherwise the value is as described in \method{get_param()} and is
335unquoted if optional \var{unquote} is \code{True} (the default).
Barry Warsaw5e634632001-09-26 05:23:47 +0000336
337Optional \var{failobj} is the object to return if there is no
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000338\mailheader{Content-Type} header. Optional \var{header} is the header to
339search instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000340
341\versionchanged[\var{unquote} argument added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000342\end{methoddesc}
343
344\begin{methoddesc}[Message]{get_param}{param\optional{,
Barry Warsaw5b9da892002-10-01 01:05:52 +0000345 failobj\optional{, header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000346Return the value of the \mailheader{Content-Type} header's parameter
347\var{param} as a string. If the message has no \mailheader{Content-Type}
Barry Warsaw5e634632001-09-26 05:23:47 +0000348header or if there is no such parameter, then \var{failobj} is
349returned (defaults to \code{None}).
350
351Optional \var{header} if given, specifies the message header to use
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000352instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000353
354Parameter keys are always compared case insensitively. The return
355value can either be a string, or a 3-tuple if the parameter was
356\rfc{2231} encoded. When it's a 3-tuple, the elements of the value are of
Barry Warsaw5db478f2002-10-01 04:33:16 +0000357the form \code{(CHARSET, LANGUAGE, VALUE)}, where \code{LANGUAGE} may
Barry Warsaw5b9da892002-10-01 01:05:52 +0000358be the empty string. Your application should be prepared to deal with
Barry Warsaw5db478f2002-10-01 04:33:16 +00003593-tuple return values, which it can convert to a Unicode string like
360so:
Barry Warsaw5b9da892002-10-01 01:05:52 +0000361
362\begin{verbatim}
363param = msg.get_param('foo')
364if isinstance(param, tuple):
365 param = unicode(param[2], param[0])
366\end{verbatim}
367
368In any case, the parameter value (either the returned string, or the
Barry Warsaw5db478f2002-10-01 04:33:16 +0000369\code{VALUE} item in the 3-tuple) is always unquoted, unless
Barry Warsaw5b9da892002-10-01 01:05:52 +0000370\var{unquote} is set to \code{False}.
371
372\versionchanged[\var{unquote} argument added, and 3-tuple return value
373possible]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000374\end{methoddesc}
375
Barry Warsaw5b9da892002-10-01 01:05:52 +0000376\begin{methoddesc}[Message]{set_param}{param, value\optional{,
377 header\optional{, requote\optional{, charset\optional{, language}}}}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000378
Barry Warsaw5b9da892002-10-01 01:05:52 +0000379Set a parameter in the \mailheader{Content-Type} header. If the
380parameter already exists in the header, its value will be replaced
381with \var{value}. If the \mailheader{Content-Type} header as not yet
382been defined for this message, it will be set to \mimetype{text/plain}
383and the new parameter value will be appended as per \rfc{2045}.
384
385Optional \var{header} specifies an alternative header to
386\mailheader{Content-Type}, and all parameters will be quoted as
387necessary unless optional \var{requote} is \code{False} (the default
388is \code{True}).
389
390If optional \var{charset} is specified, the parameter will be encoded
391according to \rfc{2231}. Optional \var{language} specifies the RFC
3922231 language, defaulting to the empty string. Both \var{charset} and
393\var{language} should be strings.
394
395\versionadded{2.2.2}
396\end{methoddesc}
397
398\begin{methoddesc}[Message]{del_param}{param\optional{, header\optional{,
399 requote}}}
400Remove the given parameter completely from the
401\mailheader{Content-Type} header. The header will be re-written in
402place without the parameter or its value. All values will be quoted
403as necessary unless \var{requote} is \code{False} (the default is
Barry Warsaw5db478f2002-10-01 04:33:16 +0000404\code{True}). Optional \var{header} specifies an alternative to
Barry Warsaw5b9da892002-10-01 01:05:52 +0000405\mailheader{Content-Type}.
406
407\versionadded{2.2.2}
408\end{methoddesc}
409
410\begin{methoddesc}[Message]{set_type}{type\optional{, header}\optional{,
411 requote}}
412Set the main type and subtype for the \mailheader{Content-Type}
413header. \var{type} must be a string in the form
414\mimetype{maintype/subtype}, otherwise a \exception{ValueError} is
415raised.
416
417This method replaces the \mailheader{Content-Type} header, keeping all
418the parameters in place. If \var{requote} is \code{False}, this
419leaves the existing header's quoting as is, otherwise the parameters
420will be quoted (the default).
421
422An alternative header can be specified in the \var{header} argument.
Barry Warsaw5db478f2002-10-01 04:33:16 +0000423When the \mailheader{Content-Type} header is set a
424\mailheader{MIME-Version} header is also added.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000425
426\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000427\end{methoddesc}
428
429\begin{methoddesc}[Message]{get_filename}{\optional{failobj}}
430Return the value of the \code{filename} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000431\mailheader{Content-Disposition} header of the message, or \var{failobj} if
Barry Warsaw5e634632001-09-26 05:23:47 +0000432either the header is missing, or has no \code{filename} parameter.
433The returned string will always be unquoted as per
434\method{Utils.unquote()}.
435\end{methoddesc}
436
437\begin{methoddesc}[Message]{get_boundary}{\optional{failobj}}
438Return the value of the \code{boundary} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000439\mailheader{Content-Type} header of the message, or \var{failobj} if either
Barry Warsaw5e634632001-09-26 05:23:47 +0000440the header is missing, or has no \code{boundary} parameter. The
441returned string will always be unquoted as per
442\method{Utils.unquote()}.
443\end{methoddesc}
444
445\begin{methoddesc}[Message]{set_boundary}{boundary}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000446Set the \code{boundary} parameter of the \mailheader{Content-Type}
447header to \var{boundary}. \method{set_boundary()} will always quote
448\var{boundary} if necessary. A \exception{HeaderParseError} is raised
449if the message object has no \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000450
451Note that using this method is subtly different than deleting the old
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000452\mailheader{Content-Type} header and adding a new one with the new boundary
Barry Warsaw5e634632001-09-26 05:23:47 +0000453via \method{add_header()}, because \method{set_boundary()} preserves the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000454order of the \mailheader{Content-Type} header in the list of headers.
Barry Warsaw5e634632001-09-26 05:23:47 +0000455However, it does \emph{not} preserve any continuation lines which may
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000456have been present in the original \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000457\end{methoddesc}
458
Barry Warsaw5b9da892002-10-01 01:05:52 +0000459\begin{methoddesc}[Message]{get_content_charset}{\optional{failobj}}
460Return the \code{charset} parameter of the \mailheader{Content-Type}
Barry Warsaw57ce1432002-10-10 15:22:16 +0000461header, coerced to lower case. If there is no
462\mailheader{Content-Type} header, or if that header has no
463\code{charset} parameter, \var{failobj} is returned.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000464
Barry Warsaw5db478f2002-10-01 04:33:16 +0000465Note that this method differs from \method{get_charset()} which
466returns the \class{Charset} instance for the default encoding of the
467message body.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000468
469\versionadded{2.2.2}
470\end{methoddesc}
471
472\begin{methoddesc}[Message]{get_charsets}{\optional{failobj}}
473Return a list containing the character set names in the message. If
474the message is a \mimetype{multipart}, then the list will contain one
475element for each subpart in the payload, otherwise, it will be a list
476of length 1.
477
478Each item in the list will be a string which is the value of the
479\code{charset} parameter in the \mailheader{Content-Type} header for the
480represented subpart. However, if the subpart has no
481\mailheader{Content-Type} header, no \code{charset} parameter, or is not of
482the \mimetype{text} main MIME type, then that item in the returned list
483will be \var{failobj}.
484\end{methoddesc}
485
Barry Warsaw5e634632001-09-26 05:23:47 +0000486\begin{methoddesc}[Message]{walk}{}
487The \method{walk()} method is an all-purpose generator which can be
488used to iterate over all the parts and subparts of a message object
489tree, in depth-first traversal order. You will typically use
Barry Warsaw5db478f2002-10-01 04:33:16 +0000490\method{walk()} as the iterator in a \code{for} loop; each
Barry Warsaw5e634632001-09-26 05:23:47 +0000491iteration returns the next subpart.
492
Barry Warsaw5db478f2002-10-01 04:33:16 +0000493Here's an example that prints the MIME type of every part of a
494multipart message structure:
Barry Warsaw5e634632001-09-26 05:23:47 +0000495
496\begin{verbatim}
497>>> for part in msg.walk():
Barry Warsaw5db478f2002-10-01 04:33:16 +0000498>>> print part.get_content_type()
Barry Warsaw5e634632001-09-26 05:23:47 +0000499multipart/report
500text/plain
501message/delivery-status
502text/plain
503text/plain
504message/rfc822
505\end{verbatim}
506\end{methoddesc}
507
508\class{Message} objects can also optionally contain two instance
509attributes, which can be used when generating the plain text of a MIME
510message.
511
512\begin{datadesc}{preamble}
513The format of a MIME document allows for some text between the blank
514line following the headers, and the first multipart boundary string.
515Normally, this text is never visible in a MIME-aware mail reader
516because it falls outside the standard MIME armor. However, when
517viewing the raw text of the message, or when viewing the message in a
518non-MIME aware reader, this text can become visible.
519
520The \var{preamble} attribute contains this leading extra-armor text
521for MIME documents. When the \class{Parser} discovers some text after
522the headers but before the first boundary string, it assigns this text
523to the message's \var{preamble} attribute. When the \class{Generator}
524is writing out the plain text representation of a MIME message, and it
525finds the message has a \var{preamble} attribute, it will write this
Barry Warsaw5b9da892002-10-01 01:05:52 +0000526text in the area between the headers and the first boundary. See
527\refmodule{email.Parser} and \refmodule{email.Generator} for details.
Barry Warsaw5e634632001-09-26 05:23:47 +0000528
529Note that if the message object has no preamble, the
530\var{preamble} attribute will be \code{None}.
531\end{datadesc}
532
533\begin{datadesc}{epilogue}
534The \var{epilogue} attribute acts the same way as the \var{preamble}
535attribute, except that it contains text that appears between the last
536boundary and the end of the message.
Barry Warsawe736d932001-10-19 04:34:42 +0000537
538One note: when generating the flat text for a \mimetype{multipart}
539message that has no \var{epilogue} (using the standard
540\class{Generator} class), no newline is added after the closing
541boundary line. If the message object has an \var{epilogue} and its
542value does not start with a newline, a newline is printed after the
543closing boundary. This seems a little clumsy, but it makes the most
544practical sense. The upshot is that if you want to ensure that a
545newline get printed after your closing \mimetype{multipart} boundary,
546set the \var{epilogue} to the empty string.
Barry Warsaw5e634632001-09-26 05:23:47 +0000547\end{datadesc}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000548
549\subsubsection{Deprecated methods}
550
551The following methods are deprecated in \module{email} version 2.
552They are documented here for completeness.
553
554\begin{methoddesc}[Message]{add_payload}{payload}
555Add \var{payload} to the message object's existing payload. If, prior
556to calling this method, the object's payload was \code{None}
557(i.e. never before set), then after this method is called, the payload
558will be the argument \var{payload}.
559
560If the object's payload was already a list
561(i.e. \method{is_multipart()} returns 1), then \var{payload} is
562appended to the end of the existing payload list.
563
564For any other type of existing payload, \method{add_payload()} will
565transform the new payload into a list consisting of the old payload
566and \var{payload}, but only if the document is already a MIME
567multipart document. This condition is satisfied if the message's
568\mailheader{Content-Type} header's main type is either
569\mimetype{multipart}, or there is no \mailheader{Content-Type}
570header. In any other situation,
571\exception{MultipartConversionError} is raised.
572
573\deprecated{2.2.2}{Use the \method{attach()} method instead.}
574\end{methoddesc}
575
576\begin{methoddesc}[Message]{get_type}{\optional{failobj}}
577Return the message's content type, as a string of the form
578\mimetype{maintype/subtype} as taken from the
579\mailheader{Content-Type} header.
580The returned string is coerced to lowercase.
581
582If there is no \mailheader{Content-Type} header in the message,
583\var{failobj} is returned (defaults to \code{None}).
584
585\deprecated{2.2.2}{Use the \method{get_content_type()} method instead.}
586\end{methoddesc}
587
588\begin{methoddesc}[Message]{get_main_type}{\optional{failobj}}
589Return the message's \emph{main} content type. This essentially returns the
590\var{maintype} part of the string returned by \method{get_type()}, with the
591same semantics for \var{failobj}.
592
593\deprecated{2.2.2}{Use the \method{get_content_maintype()} method instead.}
594\end{methoddesc}
595
596\begin{methoddesc}[Message]{get_subtype}{\optional{failobj}}
597Return the message's sub-content type. This essentially returns the
598\var{subtype} part of the string returned by \method{get_type()}, with the
599same semantics for \var{failobj}.
600
601\deprecated{2.2.2}{Use the \method{get_content_subtype()} method instead.}
602\end{methoddesc}
603