blob: 7618b20b79e600e2cab51ed5ce00639f3696e927 [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()
Andrew M. Kuchlingb4b9ced2003-10-31 19:52:30 +000049g = Generator(fp, mangle_from_=False, maxheaderlen=60)
Barry Warsawb05df572003-04-18 23:03:53 +000050g.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 Warsaw463c5a82003-08-19 04:26:59 +0000357the form \code{(CHARSET, LANGUAGE, VALUE)}. Note that both \code{CHARSET} and
358\code{LANGUAGE} can be \code{None}, in which case you should consider
359\code{VALUE} to be encoded in the \code{us-ascii} charset. You can
360usually ignore \code{LANGUAGE}.
361
362Your application should be prepared to deal with 3-tuple return
363values, and can convert the parameter to a Unicode string like so:
Barry Warsaw5b9da892002-10-01 01:05:52 +0000364
365\begin{verbatim}
366param = msg.get_param('foo')
367if isinstance(param, tuple):
Barry Warsaw463c5a82003-08-19 04:26:59 +0000368 param = unicode(param[2], param[0] or 'us-ascii')
Barry Warsaw5b9da892002-10-01 01:05:52 +0000369\end{verbatim}
370
371In any case, the parameter value (either the returned string, or the
Barry Warsaw5db478f2002-10-01 04:33:16 +0000372\code{VALUE} item in the 3-tuple) is always unquoted, unless
Barry Warsaw5b9da892002-10-01 01:05:52 +0000373\var{unquote} is set to \code{False}.
374
375\versionchanged[\var{unquote} argument added, and 3-tuple return value
376possible]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000377\end{methoddesc}
378
Barry Warsaw5b9da892002-10-01 01:05:52 +0000379\begin{methoddesc}[Message]{set_param}{param, value\optional{,
380 header\optional{, requote\optional{, charset\optional{, language}}}}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000381
Barry Warsaw5b9da892002-10-01 01:05:52 +0000382Set a parameter in the \mailheader{Content-Type} header. If the
383parameter already exists in the header, its value will be replaced
384with \var{value}. If the \mailheader{Content-Type} header as not yet
385been defined for this message, it will be set to \mimetype{text/plain}
386and the new parameter value will be appended as per \rfc{2045}.
387
388Optional \var{header} specifies an alternative header to
389\mailheader{Content-Type}, and all parameters will be quoted as
390necessary unless optional \var{requote} is \code{False} (the default
391is \code{True}).
392
393If optional \var{charset} is specified, the parameter will be encoded
394according to \rfc{2231}. Optional \var{language} specifies the RFC
3952231 language, defaulting to the empty string. Both \var{charset} and
396\var{language} should be strings.
397
398\versionadded{2.2.2}
399\end{methoddesc}
400
401\begin{methoddesc}[Message]{del_param}{param\optional{, header\optional{,
402 requote}}}
403Remove the given parameter completely from the
404\mailheader{Content-Type} header. The header will be re-written in
405place without the parameter or its value. All values will be quoted
406as necessary unless \var{requote} is \code{False} (the default is
Barry Warsaw5db478f2002-10-01 04:33:16 +0000407\code{True}). Optional \var{header} specifies an alternative to
Barry Warsaw5b9da892002-10-01 01:05:52 +0000408\mailheader{Content-Type}.
409
410\versionadded{2.2.2}
411\end{methoddesc}
412
413\begin{methoddesc}[Message]{set_type}{type\optional{, header}\optional{,
414 requote}}
415Set the main type and subtype for the \mailheader{Content-Type}
416header. \var{type} must be a string in the form
417\mimetype{maintype/subtype}, otherwise a \exception{ValueError} is
418raised.
419
420This method replaces the \mailheader{Content-Type} header, keeping all
421the parameters in place. If \var{requote} is \code{False}, this
422leaves the existing header's quoting as is, otherwise the parameters
423will be quoted (the default).
424
425An alternative header can be specified in the \var{header} argument.
Barry Warsaw5db478f2002-10-01 04:33:16 +0000426When the \mailheader{Content-Type} header is set a
427\mailheader{MIME-Version} header is also added.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000428
429\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000430\end{methoddesc}
431
432\begin{methoddesc}[Message]{get_filename}{\optional{failobj}}
433Return the value of the \code{filename} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000434\mailheader{Content-Disposition} header of the message, or \var{failobj} if
Barry Warsaw5e634632001-09-26 05:23:47 +0000435either the header is missing, or has no \code{filename} parameter.
436The returned string will always be unquoted as per
437\method{Utils.unquote()}.
438\end{methoddesc}
439
440\begin{methoddesc}[Message]{get_boundary}{\optional{failobj}}
441Return the value of the \code{boundary} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000442\mailheader{Content-Type} header of the message, or \var{failobj} if either
Barry Warsaw5e634632001-09-26 05:23:47 +0000443the header is missing, or has no \code{boundary} parameter. The
444returned string will always be unquoted as per
445\method{Utils.unquote()}.
446\end{methoddesc}
447
448\begin{methoddesc}[Message]{set_boundary}{boundary}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000449Set the \code{boundary} parameter of the \mailheader{Content-Type}
450header to \var{boundary}. \method{set_boundary()} will always quote
451\var{boundary} if necessary. A \exception{HeaderParseError} is raised
452if the message object has no \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000453
454Note that using this method is subtly different than deleting the old
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000455\mailheader{Content-Type} header and adding a new one with the new boundary
Barry Warsaw5e634632001-09-26 05:23:47 +0000456via \method{add_header()}, because \method{set_boundary()} preserves the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000457order of the \mailheader{Content-Type} header in the list of headers.
Barry Warsaw5e634632001-09-26 05:23:47 +0000458However, it does \emph{not} preserve any continuation lines which may
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000459have been present in the original \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000460\end{methoddesc}
461
Barry Warsaw5b9da892002-10-01 01:05:52 +0000462\begin{methoddesc}[Message]{get_content_charset}{\optional{failobj}}
463Return the \code{charset} parameter of the \mailheader{Content-Type}
Barry Warsaw57ce1432002-10-10 15:22:16 +0000464header, coerced to lower case. If there is no
465\mailheader{Content-Type} header, or if that header has no
466\code{charset} parameter, \var{failobj} is returned.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000467
Barry Warsaw5db478f2002-10-01 04:33:16 +0000468Note that this method differs from \method{get_charset()} which
469returns the \class{Charset} instance for the default encoding of the
470message body.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000471
472\versionadded{2.2.2}
473\end{methoddesc}
474
475\begin{methoddesc}[Message]{get_charsets}{\optional{failobj}}
476Return a list containing the character set names in the message. If
477the message is a \mimetype{multipart}, then the list will contain one
478element for each subpart in the payload, otherwise, it will be a list
479of length 1.
480
481Each item in the list will be a string which is the value of the
482\code{charset} parameter in the \mailheader{Content-Type} header for the
483represented subpart. However, if the subpart has no
484\mailheader{Content-Type} header, no \code{charset} parameter, or is not of
485the \mimetype{text} main MIME type, then that item in the returned list
486will be \var{failobj}.
487\end{methoddesc}
488
Barry Warsaw5e634632001-09-26 05:23:47 +0000489\begin{methoddesc}[Message]{walk}{}
490The \method{walk()} method is an all-purpose generator which can be
491used to iterate over all the parts and subparts of a message object
492tree, in depth-first traversal order. You will typically use
Barry Warsaw5db478f2002-10-01 04:33:16 +0000493\method{walk()} as the iterator in a \code{for} loop; each
Barry Warsaw5e634632001-09-26 05:23:47 +0000494iteration returns the next subpart.
495
Barry Warsaw5db478f2002-10-01 04:33:16 +0000496Here's an example that prints the MIME type of every part of a
497multipart message structure:
Barry Warsaw5e634632001-09-26 05:23:47 +0000498
499\begin{verbatim}
500>>> for part in msg.walk():
Barry Warsaw5db478f2002-10-01 04:33:16 +0000501>>> print part.get_content_type()
Barry Warsaw5e634632001-09-26 05:23:47 +0000502multipart/report
503text/plain
504message/delivery-status
505text/plain
506text/plain
507message/rfc822
508\end{verbatim}
509\end{methoddesc}
510
511\class{Message} objects can also optionally contain two instance
512attributes, which can be used when generating the plain text of a MIME
513message.
514
515\begin{datadesc}{preamble}
516The format of a MIME document allows for some text between the blank
517line following the headers, and the first multipart boundary string.
518Normally, this text is never visible in a MIME-aware mail reader
519because it falls outside the standard MIME armor. However, when
520viewing the raw text of the message, or when viewing the message in a
521non-MIME aware reader, this text can become visible.
522
523The \var{preamble} attribute contains this leading extra-armor text
524for MIME documents. When the \class{Parser} discovers some text after
525the headers but before the first boundary string, it assigns this text
526to the message's \var{preamble} attribute. When the \class{Generator}
527is writing out the plain text representation of a MIME message, and it
528finds the message has a \var{preamble} attribute, it will write this
Barry Warsaw5b9da892002-10-01 01:05:52 +0000529text in the area between the headers and the first boundary. See
530\refmodule{email.Parser} and \refmodule{email.Generator} for details.
Barry Warsaw5e634632001-09-26 05:23:47 +0000531
532Note that if the message object has no preamble, the
533\var{preamble} attribute will be \code{None}.
534\end{datadesc}
535
536\begin{datadesc}{epilogue}
537The \var{epilogue} attribute acts the same way as the \var{preamble}
538attribute, except that it contains text that appears between the last
539boundary and the end of the message.
Barry Warsawe736d932001-10-19 04:34:42 +0000540
541One note: when generating the flat text for a \mimetype{multipart}
542message that has no \var{epilogue} (using the standard
543\class{Generator} class), no newline is added after the closing
544boundary line. If the message object has an \var{epilogue} and its
545value does not start with a newline, a newline is printed after the
546closing boundary. This seems a little clumsy, but it makes the most
547practical sense. The upshot is that if you want to ensure that a
548newline get printed after your closing \mimetype{multipart} boundary,
549set the \var{epilogue} to the empty string.
Barry Warsaw5e634632001-09-26 05:23:47 +0000550\end{datadesc}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000551
552\subsubsection{Deprecated methods}
553
554The following methods are deprecated in \module{email} version 2.
555They are documented here for completeness.
556
557\begin{methoddesc}[Message]{add_payload}{payload}
558Add \var{payload} to the message object's existing payload. If, prior
559to calling this method, the object's payload was \code{None}
560(i.e. never before set), then after this method is called, the payload
561will be the argument \var{payload}.
562
563If the object's payload was already a list
Raymond Hettinger9b4dab42003-12-31 18:37:28 +0000564(i.e. \method{is_multipart()} returns \code{True}), then \var{payload} is
Barry Warsaw5b9da892002-10-01 01:05:52 +0000565appended to the end of the existing payload list.
566
567For any other type of existing payload, \method{add_payload()} will
568transform the new payload into a list consisting of the old payload
569and \var{payload}, but only if the document is already a MIME
570multipart document. This condition is satisfied if the message's
571\mailheader{Content-Type} header's main type is either
572\mimetype{multipart}, or there is no \mailheader{Content-Type}
573header. In any other situation,
574\exception{MultipartConversionError} is raised.
575
576\deprecated{2.2.2}{Use the \method{attach()} method instead.}
577\end{methoddesc}
578
579\begin{methoddesc}[Message]{get_type}{\optional{failobj}}
580Return the message's content type, as a string of the form
581\mimetype{maintype/subtype} as taken from the
582\mailheader{Content-Type} header.
583The returned string is coerced to lowercase.
584
585If there is no \mailheader{Content-Type} header in the message,
586\var{failobj} is returned (defaults to \code{None}).
587
588\deprecated{2.2.2}{Use the \method{get_content_type()} method instead.}
589\end{methoddesc}
590
591\begin{methoddesc}[Message]{get_main_type}{\optional{failobj}}
592Return the message's \emph{main} content type. This essentially returns the
593\var{maintype} part of the string returned by \method{get_type()}, with the
594same semantics for \var{failobj}.
595
596\deprecated{2.2.2}{Use the \method{get_content_maintype()} method instead.}
597\end{methoddesc}
598
599\begin{methoddesc}[Message]{get_subtype}{\optional{failobj}}
600Return the message's sub-content type. This essentially returns the
601\var{subtype} part of the string returned by \method{get_type()}, with the
602same semantics for \var{failobj}.
603
604\deprecated{2.2.2}{Use the \method{get_content_subtype()} method instead.}
605\end{methoddesc}
606