blob: 7bd7dd8b3093b66aa4a682b99eb957d3a111b7f3 [file] [log] [blame]
Barry Warsaw40ef0062006-03-18 15:41:53 +00001\declaremodule{standard}{email.message}
Barry Warsaw5e634632001-09-26 05:23:47 +00002\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
Barry Warsaw40ef0062006-03-18 15:41:53 +00005\class{Message} class, imported from the \module{email.message} module. It is
6the base class for the \module{email} object model. \class{Message} provides
7the core functionality for setting and querying header fields, and for
8accessing message bodies.
Barry Warsaw5e634632001-09-26 05:23:47 +00009
Barry Warsawc5f8fe32001-09-26 22:21:52 +000010Conceptually, a \class{Message} object consists of \emph{headers} and
11\emph{payloads}. Headers are \rfc{2822} style field names and
12values where the field name and value are separated by a colon. The
13colon is not part of either the field name or the field value.
Barry Warsaw5e634632001-09-26 05:23:47 +000014
Barry Warsawc5f8fe32001-09-26 22:21:52 +000015Headers are stored and returned in case-preserving form but are
Barry Warsaw5b9da892002-10-01 01:05:52 +000016matched case-insensitively. There may also be a single envelope
17header, also known as the \emph{Unix-From} header or the
Barry Warsawc5f8fe32001-09-26 22:21:52 +000018\code{From_} header. The payload is either a string in the case of
Barry Warsaw5b9da892002-10-01 01:05:52 +000019simple message objects or a list of \class{Message} objects for
20MIME container documents (e.g. \mimetype{multipart/*} and
21\mimetype{message/rfc822}).
Barry Warsawc5f8fe32001-09-26 22:21:52 +000022
23\class{Message} objects provide a mapping style interface for
24accessing the message headers, and an explicit interface for accessing
25both the headers and the payload. It provides convenience methods for
26generating a flat text representation of the message object tree, for
27accessing commonly used header parameters, and for recursively walking
28over the object tree.
Barry Warsaw5e634632001-09-26 05:23:47 +000029
30Here are the methods of the \class{Message} class:
31
Barry Warsawc5f8fe32001-09-26 22:21:52 +000032\begin{classdesc}{Message}{}
33The constructor takes no arguments.
34\end{classdesc}
35
Barry Warsaw5e634632001-09-26 05:23:47 +000036\begin{methoddesc}[Message]{as_string}{\optional{unixfrom}}
Barry Warsaw5db478f2002-10-01 04:33:16 +000037Return the entire message flatten as a string. When optional
38\var{unixfrom} is \code{True}, the envelope header is included in the
39returned string. \var{unixfrom} defaults to \code{False}.
Barry Warsawb05df572003-04-18 23:03:53 +000040
Barry Warsaw2ec48542004-10-03 03:39:47 +000041Note that this method is provided as a convenience and may not always format
Michael W. Hudsonbfe56842004-10-03 09:41:26 +000042the message the way you want. For example, by default it mangles lines that
Barry Warsaw2ec48542004-10-03 03:39:47 +000043begin with \code{From }. For more flexibility, instantiate a
44\class{Generator} instance and use its
Barry Warsawb05df572003-04-18 23:03:53 +000045\method{flatten()} method directly. For example:
46
47\begin{verbatim}
48from cStringIO import StringIO
Barry Warsaw40ef0062006-03-18 15:41:53 +000049from email.generator import Generator
Barry Warsawb05df572003-04-18 23:03:53 +000050fp = StringIO()
Andrew M. Kuchlingb4b9ced2003-10-31 19:52:30 +000051g = Generator(fp, mangle_from_=False, maxheaderlen=60)
Barry Warsawb05df572003-04-18 23:03:53 +000052g.flatten(msg)
53text = fp.getvalue()
54\end{verbatim}
Barry Warsaw5e634632001-09-26 05:23:47 +000055\end{methoddesc}
56
Fred Drake7779b202002-05-22 20:44:03 +000057\begin{methoddesc}[Message]{__str__}{}
Fred Drake6516e142002-10-01 14:29:58 +000058Equivalent to \method{as_string(unixfrom=True)}.
Barry Warsaw5e634632001-09-26 05:23:47 +000059\end{methoddesc}
60
61\begin{methoddesc}[Message]{is_multipart}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +000062Return \code{True} if the message's payload is a list of
63sub-\class{Message} objects, otherwise return \code{False}. When
64\method{is_multipart()} returns False, the payload should be a string
65object.
Barry Warsaw5e634632001-09-26 05:23:47 +000066\end{methoddesc}
67
68\begin{methoddesc}[Message]{set_unixfrom}{unixfrom}
Barry Warsaw5b9da892002-10-01 01:05:52 +000069Set the message's envelope header to \var{unixfrom}, which should be a string.
Barry Warsaw5e634632001-09-26 05:23:47 +000070\end{methoddesc}
71
72\begin{methoddesc}[Message]{get_unixfrom}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +000073Return the message's envelope header. Defaults to \code{None} if the
74envelope header was never set.
Barry Warsaw5e634632001-09-26 05:23:47 +000075\end{methoddesc}
76
77\begin{methoddesc}[Message]{attach}{payload}
Barry Warsaw5db478f2002-10-01 04:33:16 +000078Add the given \var{payload} to the current payload, which must be
Barry Warsaw5b9da892002-10-01 01:05:52 +000079\code{None} or a list of \class{Message} objects before the call.
80After the call, the payload will always be a list of \class{Message}
81objects. If you want to set the payload to a scalar object (e.g. a
82string), use \method{set_payload()} instead.
Barry Warsaw5e634632001-09-26 05:23:47 +000083\end{methoddesc}
84
85\begin{methoddesc}[Message]{get_payload}{\optional{i\optional{, decode}}}
Barry Warsaw5b9da892002-10-01 01:05:52 +000086Return a reference the current payload, which will be a list of
87\class{Message} objects when \method{is_multipart()} is \code{True}, or a
88string when \method{is_multipart()} is \code{False}. If the
89payload is a list and you mutate the list object, you modify the
90message's payload in place.
Barry Warsaw5e634632001-09-26 05:23:47 +000091
Barry Warsaw5b9da892002-10-01 01:05:52 +000092With optional argument \var{i}, \method{get_payload()} will return the
Barry Warsaw5e634632001-09-26 05:23:47 +000093\var{i}-th element of the payload, counting from zero, if
Barry Warsaw5b9da892002-10-01 01:05:52 +000094\method{is_multipart()} is \code{True}. An \exception{IndexError}
95will be raised if \var{i} is less than 0 or greater than or equal to
96the number of items in the payload. If the payload is a string
97(i.e. \method{is_multipart()} is \code{False}) and \var{i} is given, a
Barry Warsawc5f8fe32001-09-26 22:21:52 +000098\exception{TypeError} is raised.
Barry Warsaw5e634632001-09-26 05:23:47 +000099
100Optional \var{decode} is a flag indicating whether the payload should be
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000101decoded or not, according to the \mailheader{Content-Transfer-Encoding} header.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000102When \code{True} and the message is not a multipart, the payload will be
Barry Warsaw5e634632001-09-26 05:23:47 +0000103decoded if this header's value is \samp{quoted-printable} or
104\samp{base64}. If some other encoding is used, or
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000105\mailheader{Content-Transfer-Encoding} header is
Barry Warsaw20ebc372003-03-10 16:13:50 +0000106missing, or if the payload has bogus base64 data, the payload is
107returned as-is (undecoded). If the message is a multipart and the
108\var{decode} flag is \code{True}, then \code{None} is returned. The
109default for \var{decode} is \code{False}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000110\end{methoddesc}
111
Barry Warsaw5b9da892002-10-01 01:05:52 +0000112\begin{methoddesc}[Message]{set_payload}{payload\optional{, charset}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000113Set the entire message object's payload to \var{payload}. It is the
Barry Warsaw5b9da892002-10-01 01:05:52 +0000114client's responsibility to ensure the payload invariants. Optional
Barry Warsaw5db478f2002-10-01 04:33:16 +0000115\var{charset} sets the message's default character set; see
Barry Warsaw5b9da892002-10-01 01:05:52 +0000116\method{set_charset()} for details.
117
118\versionchanged[\var{charset} argument added]{2.2.2}
119\end{methoddesc}
120
121\begin{methoddesc}[Message]{set_charset}{charset}
122Set the character set of the payload to \var{charset}, which can
Barry Warsaw40ef0062006-03-18 15:41:53 +0000123either be a \class{Charset} instance (see \refmodule{email.charset}), a
Barry Warsaw5b9da892002-10-01 01:05:52 +0000124string naming a character set,
125or \code{None}. If it is a string, it will be converted to a
126\class{Charset} instance. If \var{charset} is \code{None}, the
127\code{charset} parameter will be removed from the
128\mailheader{Content-Type} header. Anything else will generate a
129\exception{TypeError}.
130
131The message will be assumed to be of type \mimetype{text/*} encoded with
Barry Warsaw40ef0062006-03-18 15:41:53 +0000132\var{charset.input_charset}. It will be converted to
133\var{charset.output_charset}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000134and encoded properly, if needed, when generating the plain text
135representation of the message. MIME headers
136(\mailheader{MIME-Version}, \mailheader{Content-Type},
137\mailheader{Content-Transfer-Encoding}) will be added as needed.
138
139\versionadded{2.2.2}
140\end{methoddesc}
141
142\begin{methoddesc}[Message]{get_charset}{}
143Return the \class{Charset} instance associated with the message's payload.
144\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000145\end{methoddesc}
146
147The following methods implement a mapping-like interface for accessing
Barry Warsaw5db478f2002-10-01 04:33:16 +0000148the message's \rfc{2822} headers. Note that there are some
Barry Warsaw5e634632001-09-26 05:23:47 +0000149semantic differences between these methods and a normal mapping
150(i.e. dictionary) interface. For example, in a dictionary there are
151no duplicate keys, but here there may be duplicate message headers. Also,
152in dictionaries there is no guaranteed order to the keys returned by
Barry Warsaw5db478f2002-10-01 04:33:16 +0000153\method{keys()}, but in a \class{Message} object, headers are always
154returned in the order they appeared in the original message, or were
155added to the message later. Any header deleted and then re-added are
156always appended to the end of the header list.
157
158These semantic differences are intentional and are biased toward
159maximal convenience.
Barry Warsaw5e634632001-09-26 05:23:47 +0000160
Barry Warsaw5b9da892002-10-01 01:05:52 +0000161Note that in all cases, any envelope header present in the message is
162not included in the mapping interface.
Barry Warsaw5e634632001-09-26 05:23:47 +0000163
164\begin{methoddesc}[Message]{__len__}{}
165Return the total number of headers, including duplicates.
166\end{methoddesc}
167
168\begin{methoddesc}[Message]{__contains__}{name}
169Return true if the message object has a field named \var{name}.
Andrew M. Kuchling43dc1fc2001-11-05 01:55:03 +0000170Matching is done case-insensitively and \var{name} should not include the
Barry Warsaw5e634632001-09-26 05:23:47 +0000171trailing colon. Used for the \code{in} operator,
172e.g.:
173
174\begin{verbatim}
175if 'message-id' in myMessage:
176 print 'Message-ID:', myMessage['message-id']
177\end{verbatim}
178\end{methoddesc}
179
180\begin{methoddesc}[Message]{__getitem__}{name}
181Return the value of the named header field. \var{name} should not
182include the colon field separator. If the header is missing,
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000183\code{None} is returned; a \exception{KeyError} is never raised.
Barry Warsaw5e634632001-09-26 05:23:47 +0000184
185Note that if the named field appears more than once in the message's
186headers, exactly which of those field values will be returned is
187undefined. Use the \method{get_all()} method to get the values of all
188the extant named headers.
189\end{methoddesc}
190
191\begin{methoddesc}[Message]{__setitem__}{name, val}
192Add a header to the message with field name \var{name} and value
193\var{val}. The field is appended to the end of the message's existing
194fields.
195
196Note that this does \emph{not} overwrite or delete any existing header
197with the same name. If you want to ensure that the new header is the
198only one present in the message with field name
Barry Warsaw5db478f2002-10-01 04:33:16 +0000199\var{name}, delete the field first, e.g.:
Barry Warsaw5e634632001-09-26 05:23:47 +0000200
201\begin{verbatim}
202del msg['subject']
203msg['subject'] = 'Python roolz!'
204\end{verbatim}
205\end{methoddesc}
206
207\begin{methoddesc}[Message]{__delitem__}{name}
208Delete all occurrences of the field with name \var{name} from the
209message's headers. No exception is raised if the named field isn't
210present in the headers.
211\end{methoddesc}
212
213\begin{methoddesc}[Message]{has_key}{name}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000214Return true if the message contains a header field named \var{name},
215otherwise return false.
Barry Warsaw5e634632001-09-26 05:23:47 +0000216\end{methoddesc}
217
218\begin{methoddesc}[Message]{keys}{}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000219Return a list of all the message's header field names.
Barry Warsaw5e634632001-09-26 05:23:47 +0000220\end{methoddesc}
221
222\begin{methoddesc}[Message]{values}{}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000223Return a list of all the message's field values.
Barry Warsaw5e634632001-09-26 05:23:47 +0000224\end{methoddesc}
225
226\begin{methoddesc}[Message]{items}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000227Return a list of 2-tuples containing all the message's field headers
Barry Warsaw5db478f2002-10-01 04:33:16 +0000228and values.
Barry Warsaw5e634632001-09-26 05:23:47 +0000229\end{methoddesc}
230
231\begin{methoddesc}[Message]{get}{name\optional{, failobj}}
232Return the value of the named header field. This is identical to
233\method{__getitem__()} except that optional \var{failobj} is returned
234if the named header is missing (defaults to \code{None}).
235\end{methoddesc}
236
237Here are some additional useful methods:
238
239\begin{methoddesc}[Message]{get_all}{name\optional{, failobj}}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000240Return a list of all the values for the field named \var{name}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000241If there are no such named headers in the message, \var{failobj} is
242returned (defaults to \code{None}).
243\end{methoddesc}
244
245\begin{methoddesc}[Message]{add_header}{_name, _value, **_params}
246Extended header setting. This method is similar to
247\method{__setitem__()} except that additional header parameters can be
Barry Warsaw5b9da892002-10-01 01:05:52 +0000248provided as keyword arguments. \var{_name} is the header field to add
249and \var{_value} is the \emph{primary} value for the header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000250
251For each item in the keyword argument dictionary \var{_params}, the
252key is taken as the parameter name, with underscores converted to
253dashes (since dashes are illegal in Python identifiers). Normally,
254the parameter will be added as \code{key="value"} unless the value is
255\code{None}, in which case only the key will be added.
256
257Here's an example:
258
259\begin{verbatim}
260msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
261\end{verbatim}
262
263This will add a header that looks like
264
265\begin{verbatim}
266Content-Disposition: attachment; filename="bud.gif"
267\end{verbatim}
268\end{methoddesc}
269
Barry Warsaw5b9da892002-10-01 01:05:52 +0000270\begin{methoddesc}[Message]{replace_header}{_name, _value}
271Replace a header. Replace the first header found in the message that
272matches \var{_name}, retaining header order and field name case. If
273no matching header was found, a \exception{KeyError} is raised.
274
275\versionadded{2.2.2}
276\end{methoddesc}
277
278\begin{methoddesc}[Message]{get_content_type}{}
279Return the message's content type. The returned string is coerced to
280lower case of the form \mimetype{maintype/subtype}. If there was no
281\mailheader{Content-Type} header in the message the default type as
282given by \method{get_default_type()} will be returned. Since
283according to \rfc{2045}, messages always have a default type,
284\method{get_content_type()} will always return a value.
285
286\rfc{2045} defines a message's default type to be
287\mimetype{text/plain} unless it appears inside a
288\mimetype{multipart/digest} container, in which case it would be
289\mimetype{message/rfc822}. If the \mailheader{Content-Type} header
290has an invalid type specification, \rfc{2045} mandates that the
291default type be \mimetype{text/plain}.
292
293\versionadded{2.2.2}
294\end{methoddesc}
295
296\begin{methoddesc}[Message]{get_content_maintype}{}
297Return the message's main content type. This is the
298\mimetype{maintype} part of the string returned by
299\method{get_content_type()}.
300
301\versionadded{2.2.2}
302\end{methoddesc}
303
304\begin{methoddesc}[Message]{get_content_subtype}{}
305Return the message's sub-content type. This is the \mimetype{subtype}
306part of the string returned by \method{get_content_type()}.
307
308\versionadded{2.2.2}
309\end{methoddesc}
310
311\begin{methoddesc}[Message]{get_default_type}{}
312Return the default content type. Most messages have a default content
313type of \mimetype{text/plain}, except for messages that are subparts
314of \mimetype{multipart/digest} containers. Such subparts have a
315default content type of \mimetype{message/rfc822}.
316
317\versionadded{2.2.2}
318\end{methoddesc}
319
320\begin{methoddesc}[Message]{set_default_type}{ctype}
321Set the default content type. \var{ctype} should either be
322\mimetype{text/plain} or \mimetype{message/rfc822}, although this is
323not enforced. The default content type is not stored in the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000324\mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000325
Barry Warsaw5b9da892002-10-01 01:05:52 +0000326\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000327\end{methoddesc}
328
Barry Warsaw5b9da892002-10-01 01:05:52 +0000329\begin{methoddesc}[Message]{get_params}{\optional{failobj\optional{,
330 header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000331Return the message's \mailheader{Content-Type} parameters, as a list. The
Barry Warsaw5e634632001-09-26 05:23:47 +0000332elements of the returned list are 2-tuples of key/value pairs, as
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000333split on the \character{=} sign. The left hand side of the
334\character{=} is the key, while the right hand side is the value. If
335there is no \character{=} sign in the parameter the value is the empty
Barry Warsaw5b9da892002-10-01 01:05:52 +0000336string, otherwise the value is as described in \method{get_param()} and is
337unquoted if optional \var{unquote} is \code{True} (the default).
Barry Warsaw5e634632001-09-26 05:23:47 +0000338
339Optional \var{failobj} is the object to return if there is no
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000340\mailheader{Content-Type} header. Optional \var{header} is the header to
341search instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000342
343\versionchanged[\var{unquote} argument added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000344\end{methoddesc}
345
346\begin{methoddesc}[Message]{get_param}{param\optional{,
Barry Warsaw5b9da892002-10-01 01:05:52 +0000347 failobj\optional{, header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000348Return the value of the \mailheader{Content-Type} header's parameter
349\var{param} as a string. If the message has no \mailheader{Content-Type}
Barry Warsaw5e634632001-09-26 05:23:47 +0000350header or if there is no such parameter, then \var{failobj} is
351returned (defaults to \code{None}).
352
353Optional \var{header} if given, specifies the message header to use
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000354instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000355
356Parameter keys are always compared case insensitively. The return
357value can either be a string, or a 3-tuple if the parameter was
358\rfc{2231} encoded. When it's a 3-tuple, the elements of the value are of
Barry Warsaw463c5a82003-08-19 04:26:59 +0000359the form \code{(CHARSET, LANGUAGE, VALUE)}. Note that both \code{CHARSET} and
360\code{LANGUAGE} can be \code{None}, in which case you should consider
361\code{VALUE} to be encoded in the \code{us-ascii} charset. You can
362usually ignore \code{LANGUAGE}.
363
Barry Warsawbb113862004-10-03 03:16:19 +0000364If your application doesn't care whether the parameter was encoded as in
365\rfc{2231}, you can collapse the parameter value by calling
366\function{email.Utils.collapse_rfc2231_value()}, passing in the return value
367from \method{get_param()}. This will return a suitably decoded Unicode string
368whn the value is a tuple, or the original string unquoted if it isn't. For
369example:
Barry Warsaw5b9da892002-10-01 01:05:52 +0000370
371\begin{verbatim}
Barry Warsawbb113862004-10-03 03:16:19 +0000372rawparam = msg.get_param('foo')
373param = email.Utils.collapse_rfc2231_value(rawparam)
Barry Warsaw5b9da892002-10-01 01:05:52 +0000374\end{verbatim}
375
376In any case, the parameter value (either the returned string, or the
Barry Warsaw5db478f2002-10-01 04:33:16 +0000377\code{VALUE} item in the 3-tuple) is always unquoted, unless
Barry Warsaw5b9da892002-10-01 01:05:52 +0000378\var{unquote} is set to \code{False}.
379
380\versionchanged[\var{unquote} argument added, and 3-tuple return value
381possible]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000382\end{methoddesc}
383
Barry Warsaw5b9da892002-10-01 01:05:52 +0000384\begin{methoddesc}[Message]{set_param}{param, value\optional{,
385 header\optional{, requote\optional{, charset\optional{, language}}}}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000386
Barry Warsaw5b9da892002-10-01 01:05:52 +0000387Set a parameter in the \mailheader{Content-Type} header. If the
388parameter already exists in the header, its value will be replaced
389with \var{value}. If the \mailheader{Content-Type} header as not yet
390been defined for this message, it will be set to \mimetype{text/plain}
391and the new parameter value will be appended as per \rfc{2045}.
392
393Optional \var{header} specifies an alternative header to
394\mailheader{Content-Type}, and all parameters will be quoted as
395necessary unless optional \var{requote} is \code{False} (the default
396is \code{True}).
397
398If optional \var{charset} is specified, the parameter will be encoded
399according to \rfc{2231}. Optional \var{language} specifies the RFC
4002231 language, defaulting to the empty string. Both \var{charset} and
401\var{language} should be strings.
402
403\versionadded{2.2.2}
404\end{methoddesc}
405
406\begin{methoddesc}[Message]{del_param}{param\optional{, header\optional{,
407 requote}}}
408Remove the given parameter completely from the
409\mailheader{Content-Type} header. The header will be re-written in
410place without the parameter or its value. All values will be quoted
411as necessary unless \var{requote} is \code{False} (the default is
Barry Warsaw5db478f2002-10-01 04:33:16 +0000412\code{True}). Optional \var{header} specifies an alternative to
Barry Warsaw5b9da892002-10-01 01:05:52 +0000413\mailheader{Content-Type}.
414
415\versionadded{2.2.2}
416\end{methoddesc}
417
418\begin{methoddesc}[Message]{set_type}{type\optional{, header}\optional{,
419 requote}}
420Set the main type and subtype for the \mailheader{Content-Type}
421header. \var{type} must be a string in the form
422\mimetype{maintype/subtype}, otherwise a \exception{ValueError} is
423raised.
424
425This method replaces the \mailheader{Content-Type} header, keeping all
426the parameters in place. If \var{requote} is \code{False}, this
427leaves the existing header's quoting as is, otherwise the parameters
428will be quoted (the default).
429
430An alternative header can be specified in the \var{header} argument.
Barry Warsaw5db478f2002-10-01 04:33:16 +0000431When the \mailheader{Content-Type} header is set a
432\mailheader{MIME-Version} header is also added.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000433
434\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000435\end{methoddesc}
436
437\begin{methoddesc}[Message]{get_filename}{\optional{failobj}}
438Return the value of the \code{filename} parameter of the
Barry Warsaw48653af2006-01-17 05:24:25 +0000439\mailheader{Content-Disposition} header of the message. If the header does
440not have a \code{filename} parameter, this method falls back to looking for
441the \code{name} parameter. If neither is found, or the header is missing,
442then \var{failobj} is returned. The returned string will always be unquoted
443as per \method{Utils.unquote()}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000444\end{methoddesc}
445
446\begin{methoddesc}[Message]{get_boundary}{\optional{failobj}}
447Return the value of the \code{boundary} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000448\mailheader{Content-Type} header of the message, or \var{failobj} if either
Barry Warsaw5e634632001-09-26 05:23:47 +0000449the header is missing, or has no \code{boundary} parameter. The
450returned string will always be unquoted as per
451\method{Utils.unquote()}.
452\end{methoddesc}
453
454\begin{methoddesc}[Message]{set_boundary}{boundary}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000455Set the \code{boundary} parameter of the \mailheader{Content-Type}
456header to \var{boundary}. \method{set_boundary()} will always quote
457\var{boundary} if necessary. A \exception{HeaderParseError} is raised
458if the message object has no \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000459
460Note that using this method is subtly different than deleting the old
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000461\mailheader{Content-Type} header and adding a new one with the new boundary
Barry Warsaw5e634632001-09-26 05:23:47 +0000462via \method{add_header()}, because \method{set_boundary()} preserves the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000463order of the \mailheader{Content-Type} header in the list of headers.
Barry Warsaw5e634632001-09-26 05:23:47 +0000464However, it does \emph{not} preserve any continuation lines which may
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000465have been present in the original \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000466\end{methoddesc}
467
Barry Warsaw5b9da892002-10-01 01:05:52 +0000468\begin{methoddesc}[Message]{get_content_charset}{\optional{failobj}}
469Return the \code{charset} parameter of the \mailheader{Content-Type}
Barry Warsaw57ce1432002-10-10 15:22:16 +0000470header, coerced to lower case. If there is no
471\mailheader{Content-Type} header, or if that header has no
472\code{charset} parameter, \var{failobj} is returned.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000473
Barry Warsaw5db478f2002-10-01 04:33:16 +0000474Note that this method differs from \method{get_charset()} which
475returns the \class{Charset} instance for the default encoding of the
476message body.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000477
478\versionadded{2.2.2}
479\end{methoddesc}
480
481\begin{methoddesc}[Message]{get_charsets}{\optional{failobj}}
482Return a list containing the character set names in the message. If
483the message is a \mimetype{multipart}, then the list will contain one
484element for each subpart in the payload, otherwise, it will be a list
485of length 1.
486
487Each item in the list will be a string which is the value of the
488\code{charset} parameter in the \mailheader{Content-Type} header for the
489represented subpart. However, if the subpart has no
490\mailheader{Content-Type} header, no \code{charset} parameter, or is not of
491the \mimetype{text} main MIME type, then that item in the returned list
492will be \var{failobj}.
493\end{methoddesc}
494
Barry Warsaw5e634632001-09-26 05:23:47 +0000495\begin{methoddesc}[Message]{walk}{}
496The \method{walk()} method is an all-purpose generator which can be
497used to iterate over all the parts and subparts of a message object
498tree, in depth-first traversal order. You will typically use
Barry Warsaw5db478f2002-10-01 04:33:16 +0000499\method{walk()} as the iterator in a \code{for} loop; each
Barry Warsaw5e634632001-09-26 05:23:47 +0000500iteration returns the next subpart.
501
Barry Warsaw5db478f2002-10-01 04:33:16 +0000502Here's an example that prints the MIME type of every part of a
503multipart message structure:
Barry Warsaw5e634632001-09-26 05:23:47 +0000504
505\begin{verbatim}
506>>> for part in msg.walk():
Edward Loperad512262004-09-28 02:56:45 +0000507... print part.get_content_type()
Barry Warsaw5e634632001-09-26 05:23:47 +0000508multipart/report
509text/plain
510message/delivery-status
511text/plain
512text/plain
513message/rfc822
514\end{verbatim}
515\end{methoddesc}
516
Barry Warsaw40ef0062006-03-18 15:41:53 +0000517\versionchanged[The previously deprecated methods \method{get_type()},
518\method{get_main_type()}, and \method{get_subtype()} were removed]{2.5}
519
Barry Warsaw5e634632001-09-26 05:23:47 +0000520\class{Message} objects can also optionally contain two instance
521attributes, which can be used when generating the plain text of a MIME
522message.
523
524\begin{datadesc}{preamble}
525The format of a MIME document allows for some text between the blank
526line following the headers, and the first multipart boundary string.
527Normally, this text is never visible in a MIME-aware mail reader
528because it falls outside the standard MIME armor. However, when
529viewing the raw text of the message, or when viewing the message in a
530non-MIME aware reader, this text can become visible.
531
532The \var{preamble} attribute contains this leading extra-armor text
533for MIME documents. When the \class{Parser} discovers some text after
534the headers but before the first boundary string, it assigns this text
535to the message's \var{preamble} attribute. When the \class{Generator}
536is writing out the plain text representation of a MIME message, and it
537finds the message has a \var{preamble} attribute, it will write this
Barry Warsaw5b9da892002-10-01 01:05:52 +0000538text in the area between the headers and the first boundary. See
Barry Warsaw40ef0062006-03-18 15:41:53 +0000539\refmodule{email.parser} and \refmodule{email.generator} for details.
Barry Warsaw5e634632001-09-26 05:23:47 +0000540
541Note that if the message object has no preamble, the
542\var{preamble} attribute will be \code{None}.
543\end{datadesc}
544
545\begin{datadesc}{epilogue}
546The \var{epilogue} attribute acts the same way as the \var{preamble}
547attribute, except that it contains text that appears between the last
548boundary and the end of the message.
Barry Warsawe736d932001-10-19 04:34:42 +0000549
Barry Warsaw40ef0062006-03-18 15:41:53 +0000550\versionchanged[You do not need to set the epilogue to the empty string in
551order for the \class{Generator} to print a newline at the end of the
552file]{2.5}
Barry Warsaw5e634632001-09-26 05:23:47 +0000553\end{datadesc}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000554
Barry Warsawbb113862004-10-03 03:16:19 +0000555\begin{datadesc}{defects}
556The \var{defects} attribute contains a list of all the problems found when
Barry Warsaw40ef0062006-03-18 15:41:53 +0000557parsing this message. See \refmodule{email.errors} for a detailed description
Barry Warsawbb113862004-10-03 03:16:19 +0000558of the possible parsing defects.
559
560\versionadded{2.4}
561\end{datadesc}