blob: f732054350ce306bbfa86f12c5f6f924be633993 [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
Barry Warsawbb113862004-10-03 03:16:19 +0000362If your application doesn't care whether the parameter was encoded as in
363\rfc{2231}, you can collapse the parameter value by calling
364\function{email.Utils.collapse_rfc2231_value()}, passing in the return value
365from \method{get_param()}. This will return a suitably decoded Unicode string
366whn the value is a tuple, or the original string unquoted if it isn't. For
367example:
Barry Warsaw5b9da892002-10-01 01:05:52 +0000368
369\begin{verbatim}
Barry Warsawbb113862004-10-03 03:16:19 +0000370rawparam = msg.get_param('foo')
371param = email.Utils.collapse_rfc2231_value(rawparam)
Barry Warsaw5b9da892002-10-01 01:05:52 +0000372\end{verbatim}
373
374In any case, the parameter value (either the returned string, or the
Barry Warsaw5db478f2002-10-01 04:33:16 +0000375\code{VALUE} item in the 3-tuple) is always unquoted, unless
Barry Warsaw5b9da892002-10-01 01:05:52 +0000376\var{unquote} is set to \code{False}.
377
378\versionchanged[\var{unquote} argument added, and 3-tuple return value
379possible]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000380\end{methoddesc}
381
Barry Warsaw5b9da892002-10-01 01:05:52 +0000382\begin{methoddesc}[Message]{set_param}{param, value\optional{,
383 header\optional{, requote\optional{, charset\optional{, language}}}}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000384
Barry Warsaw5b9da892002-10-01 01:05:52 +0000385Set a parameter in the \mailheader{Content-Type} header. If the
386parameter already exists in the header, its value will be replaced
387with \var{value}. If the \mailheader{Content-Type} header as not yet
388been defined for this message, it will be set to \mimetype{text/plain}
389and the new parameter value will be appended as per \rfc{2045}.
390
391Optional \var{header} specifies an alternative header to
392\mailheader{Content-Type}, and all parameters will be quoted as
393necessary unless optional \var{requote} is \code{False} (the default
394is \code{True}).
395
396If optional \var{charset} is specified, the parameter will be encoded
397according to \rfc{2231}. Optional \var{language} specifies the RFC
3982231 language, defaulting to the empty string. Both \var{charset} and
399\var{language} should be strings.
400
401\versionadded{2.2.2}
402\end{methoddesc}
403
404\begin{methoddesc}[Message]{del_param}{param\optional{, header\optional{,
405 requote}}}
406Remove the given parameter completely from the
407\mailheader{Content-Type} header. The header will be re-written in
408place without the parameter or its value. All values will be quoted
409as necessary unless \var{requote} is \code{False} (the default is
Barry Warsaw5db478f2002-10-01 04:33:16 +0000410\code{True}). Optional \var{header} specifies an alternative to
Barry Warsaw5b9da892002-10-01 01:05:52 +0000411\mailheader{Content-Type}.
412
413\versionadded{2.2.2}
414\end{methoddesc}
415
416\begin{methoddesc}[Message]{set_type}{type\optional{, header}\optional{,
417 requote}}
418Set the main type and subtype for the \mailheader{Content-Type}
419header. \var{type} must be a string in the form
420\mimetype{maintype/subtype}, otherwise a \exception{ValueError} is
421raised.
422
423This method replaces the \mailheader{Content-Type} header, keeping all
424the parameters in place. If \var{requote} is \code{False}, this
425leaves the existing header's quoting as is, otherwise the parameters
426will be quoted (the default).
427
428An alternative header can be specified in the \var{header} argument.
Barry Warsaw5db478f2002-10-01 04:33:16 +0000429When the \mailheader{Content-Type} header is set a
430\mailheader{MIME-Version} header is also added.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000431
432\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000433\end{methoddesc}
434
435\begin{methoddesc}[Message]{get_filename}{\optional{failobj}}
436Return the value of the \code{filename} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000437\mailheader{Content-Disposition} header of the message, or \var{failobj} if
Barry Warsaw5e634632001-09-26 05:23:47 +0000438either the header is missing, or has no \code{filename} parameter.
439The returned string will always be unquoted as per
440\method{Utils.unquote()}.
441\end{methoddesc}
442
443\begin{methoddesc}[Message]{get_boundary}{\optional{failobj}}
444Return the value of the \code{boundary} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000445\mailheader{Content-Type} header of the message, or \var{failobj} if either
Barry Warsaw5e634632001-09-26 05:23:47 +0000446the header is missing, or has no \code{boundary} parameter. The
447returned string will always be unquoted as per
448\method{Utils.unquote()}.
449\end{methoddesc}
450
451\begin{methoddesc}[Message]{set_boundary}{boundary}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000452Set the \code{boundary} parameter of the \mailheader{Content-Type}
453header to \var{boundary}. \method{set_boundary()} will always quote
454\var{boundary} if necessary. A \exception{HeaderParseError} is raised
455if the message object has no \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000456
457Note that using this method is subtly different than deleting the old
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000458\mailheader{Content-Type} header and adding a new one with the new boundary
Barry Warsaw5e634632001-09-26 05:23:47 +0000459via \method{add_header()}, because \method{set_boundary()} preserves the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000460order of the \mailheader{Content-Type} header in the list of headers.
Barry Warsaw5e634632001-09-26 05:23:47 +0000461However, it does \emph{not} preserve any continuation lines which may
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000462have been present in the original \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000463\end{methoddesc}
464
Barry Warsaw5b9da892002-10-01 01:05:52 +0000465\begin{methoddesc}[Message]{get_content_charset}{\optional{failobj}}
466Return the \code{charset} parameter of the \mailheader{Content-Type}
Barry Warsaw57ce1432002-10-10 15:22:16 +0000467header, coerced to lower case. If there is no
468\mailheader{Content-Type} header, or if that header has no
469\code{charset} parameter, \var{failobj} is returned.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000470
Barry Warsaw5db478f2002-10-01 04:33:16 +0000471Note that this method differs from \method{get_charset()} which
472returns the \class{Charset} instance for the default encoding of the
473message body.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000474
475\versionadded{2.2.2}
476\end{methoddesc}
477
478\begin{methoddesc}[Message]{get_charsets}{\optional{failobj}}
479Return a list containing the character set names in the message. If
480the message is a \mimetype{multipart}, then the list will contain one
481element for each subpart in the payload, otherwise, it will be a list
482of length 1.
483
484Each item in the list will be a string which is the value of the
485\code{charset} parameter in the \mailheader{Content-Type} header for the
486represented subpart. However, if the subpart has no
487\mailheader{Content-Type} header, no \code{charset} parameter, or is not of
488the \mimetype{text} main MIME type, then that item in the returned list
489will be \var{failobj}.
490\end{methoddesc}
491
Barry Warsaw5e634632001-09-26 05:23:47 +0000492\begin{methoddesc}[Message]{walk}{}
493The \method{walk()} method is an all-purpose generator which can be
494used to iterate over all the parts and subparts of a message object
495tree, in depth-first traversal order. You will typically use
Barry Warsaw5db478f2002-10-01 04:33:16 +0000496\method{walk()} as the iterator in a \code{for} loop; each
Barry Warsaw5e634632001-09-26 05:23:47 +0000497iteration returns the next subpart.
498
Barry Warsaw5db478f2002-10-01 04:33:16 +0000499Here's an example that prints the MIME type of every part of a
500multipart message structure:
Barry Warsaw5e634632001-09-26 05:23:47 +0000501
502\begin{verbatim}
503>>> for part in msg.walk():
Edward Loperad512262004-09-28 02:56:45 +0000504... print part.get_content_type()
Barry Warsaw5e634632001-09-26 05:23:47 +0000505multipart/report
506text/plain
507message/delivery-status
508text/plain
509text/plain
510message/rfc822
511\end{verbatim}
512\end{methoddesc}
513
514\class{Message} objects can also optionally contain two instance
515attributes, which can be used when generating the plain text of a MIME
516message.
517
518\begin{datadesc}{preamble}
519The format of a MIME document allows for some text between the blank
520line following the headers, and the first multipart boundary string.
521Normally, this text is never visible in a MIME-aware mail reader
522because it falls outside the standard MIME armor. However, when
523viewing the raw text of the message, or when viewing the message in a
524non-MIME aware reader, this text can become visible.
525
526The \var{preamble} attribute contains this leading extra-armor text
527for MIME documents. When the \class{Parser} discovers some text after
528the headers but before the first boundary string, it assigns this text
529to the message's \var{preamble} attribute. When the \class{Generator}
530is writing out the plain text representation of a MIME message, and it
531finds the message has a \var{preamble} attribute, it will write this
Barry Warsaw5b9da892002-10-01 01:05:52 +0000532text in the area between the headers and the first boundary. See
533\refmodule{email.Parser} and \refmodule{email.Generator} for details.
Barry Warsaw5e634632001-09-26 05:23:47 +0000534
535Note that if the message object has no preamble, the
536\var{preamble} attribute will be \code{None}.
537\end{datadesc}
538
539\begin{datadesc}{epilogue}
540The \var{epilogue} attribute acts the same way as the \var{preamble}
541attribute, except that it contains text that appears between the last
542boundary and the end of the message.
Barry Warsawe736d932001-10-19 04:34:42 +0000543
544One note: when generating the flat text for a \mimetype{multipart}
545message that has no \var{epilogue} (using the standard
546\class{Generator} class), no newline is added after the closing
547boundary line. If the message object has an \var{epilogue} and its
548value does not start with a newline, a newline is printed after the
549closing boundary. This seems a little clumsy, but it makes the most
550practical sense. The upshot is that if you want to ensure that a
551newline get printed after your closing \mimetype{multipart} boundary,
552set the \var{epilogue} to the empty string.
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
557parsing this message. See \refmodule{email.Errors} for a detailed description
558of the possible parsing defects.
559
560\versionadded{2.4}
561\end{datadesc}
562
Barry Warsaw5b9da892002-10-01 01:05:52 +0000563\subsubsection{Deprecated methods}
564
Barry Warsawbb113862004-10-03 03:16:19 +0000565\versionchanged[The \method{add_payload()} method was removed; use the
566\method{attach()} method instead]{2.4}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000567
Barry Warsawbb113862004-10-03 03:16:19 +0000568The following methods are deprecated. They are documented here for
569completeness.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000570
571\begin{methoddesc}[Message]{get_type}{\optional{failobj}}
572Return the message's content type, as a string of the form
573\mimetype{maintype/subtype} as taken from the
574\mailheader{Content-Type} header.
575The returned string is coerced to lowercase.
576
577If there is no \mailheader{Content-Type} header in the message,
578\var{failobj} is returned (defaults to \code{None}).
579
580\deprecated{2.2.2}{Use the \method{get_content_type()} method instead.}
581\end{methoddesc}
582
583\begin{methoddesc}[Message]{get_main_type}{\optional{failobj}}
584Return the message's \emph{main} content type. This essentially returns the
585\var{maintype} part of the string returned by \method{get_type()}, with the
586same semantics for \var{failobj}.
587
588\deprecated{2.2.2}{Use the \method{get_content_maintype()} method instead.}
589\end{methoddesc}
590
591\begin{methoddesc}[Message]{get_subtype}{\optional{failobj}}
592Return the message's sub-content type. This essentially returns the
593\var{subtype} 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_subtype()} method instead.}
597\end{methoddesc}
598