blob: 54e5cac991b0570c498b040bef5c328187e88935 [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
Barry Warsaw2ec48542004-10-03 03:39:47 +000040Note that this method is provided as a convenience and may not always format
41the message the way you want. For example, by default it mangels lines that
42begin with \code{From }. For more flexibility, instantiate a
43\class{Generator} instance and use its
Barry Warsawb05df572003-04-18 23:03:53 +000044\method{flatten()} method directly. For example:
45
46\begin{verbatim}
47from cStringIO import StringIO
48from email.Generator import Generator
49fp = StringIO()
Andrew M. Kuchlingb4b9ced2003-10-31 19:52:30 +000050g = Generator(fp, mangle_from_=False, maxheaderlen=60)
Barry Warsawb05df572003-04-18 23:03:53 +000051g.flatten(msg)
52text = fp.getvalue()
53\end{verbatim}
Barry Warsaw5e634632001-09-26 05:23:47 +000054\end{methoddesc}
55
Fred Drake7779b202002-05-22 20:44:03 +000056\begin{methoddesc}[Message]{__str__}{}
Fred Drake6516e142002-10-01 14:29:58 +000057Equivalent to \method{as_string(unixfrom=True)}.
Barry Warsaw5e634632001-09-26 05:23:47 +000058\end{methoddesc}
59
60\begin{methoddesc}[Message]{is_multipart}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +000061Return \code{True} if the message's payload is a list of
62sub-\class{Message} objects, otherwise return \code{False}. When
63\method{is_multipart()} returns False, the payload should be a string
64object.
Barry Warsaw5e634632001-09-26 05:23:47 +000065\end{methoddesc}
66
67\begin{methoddesc}[Message]{set_unixfrom}{unixfrom}
Barry Warsaw5b9da892002-10-01 01:05:52 +000068Set the message's envelope header to \var{unixfrom}, which should be a string.
Barry Warsaw5e634632001-09-26 05:23:47 +000069\end{methoddesc}
70
71\begin{methoddesc}[Message]{get_unixfrom}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +000072Return the message's envelope header. Defaults to \code{None} if the
73envelope header was never set.
Barry Warsaw5e634632001-09-26 05:23:47 +000074\end{methoddesc}
75
76\begin{methoddesc}[Message]{attach}{payload}
Barry Warsaw5db478f2002-10-01 04:33:16 +000077Add the given \var{payload} to the current payload, which must be
Barry Warsaw5b9da892002-10-01 01:05:52 +000078\code{None} or a list of \class{Message} objects before the call.
79After the call, the payload will always be a list of \class{Message}
80objects. If you want to set the payload to a scalar object (e.g. a
81string), use \method{set_payload()} instead.
Barry Warsaw5e634632001-09-26 05:23:47 +000082\end{methoddesc}
83
84\begin{methoddesc}[Message]{get_payload}{\optional{i\optional{, decode}}}
Barry Warsaw5b9da892002-10-01 01:05:52 +000085Return a reference the current payload, which will be a list of
86\class{Message} objects when \method{is_multipart()} is \code{True}, or a
87string when \method{is_multipart()} is \code{False}. If the
88payload is a list and you mutate the list object, you modify the
89message's payload in place.
Barry Warsaw5e634632001-09-26 05:23:47 +000090
Barry Warsaw5b9da892002-10-01 01:05:52 +000091With optional argument \var{i}, \method{get_payload()} will return the
Barry Warsaw5e634632001-09-26 05:23:47 +000092\var{i}-th element of the payload, counting from zero, if
Barry Warsaw5b9da892002-10-01 01:05:52 +000093\method{is_multipart()} is \code{True}. An \exception{IndexError}
94will be raised if \var{i} is less than 0 or greater than or equal to
95the number of items in the payload. If the payload is a string
96(i.e. \method{is_multipart()} is \code{False}) and \var{i} is given, a
Barry Warsawc5f8fe32001-09-26 22:21:52 +000097\exception{TypeError} is raised.
Barry Warsaw5e634632001-09-26 05:23:47 +000098
99Optional \var{decode} is a flag indicating whether the payload should be
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000100decoded or not, according to the \mailheader{Content-Transfer-Encoding} header.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000101When \code{True} and the message is not a multipart, the payload will be
Barry Warsaw5e634632001-09-26 05:23:47 +0000102decoded if this header's value is \samp{quoted-printable} or
103\samp{base64}. If some other encoding is used, or
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000104\mailheader{Content-Transfer-Encoding} header is
Barry Warsaw20ebc372003-03-10 16:13:50 +0000105missing, or if the payload has bogus base64 data, the payload is
106returned as-is (undecoded). If the message is a multipart and the
107\var{decode} flag is \code{True}, then \code{None} is returned. The
108default for \var{decode} is \code{False}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000109\end{methoddesc}
110
Barry Warsaw5b9da892002-10-01 01:05:52 +0000111\begin{methoddesc}[Message]{set_payload}{payload\optional{, charset}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000112Set the entire message object's payload to \var{payload}. It is the
Barry Warsaw5b9da892002-10-01 01:05:52 +0000113client's responsibility to ensure the payload invariants. Optional
Barry Warsaw5db478f2002-10-01 04:33:16 +0000114\var{charset} sets the message's default character set; see
Barry Warsaw5b9da892002-10-01 01:05:52 +0000115\method{set_charset()} for details.
116
117\versionchanged[\var{charset} argument added]{2.2.2}
118\end{methoddesc}
119
120\begin{methoddesc}[Message]{set_charset}{charset}
121Set the character set of the payload to \var{charset}, which can
Barry Warsaw5db478f2002-10-01 04:33:16 +0000122either be a \class{Charset} instance (see \refmodule{email.Charset}), a
Barry Warsaw5b9da892002-10-01 01:05:52 +0000123string naming a character set,
124or \code{None}. If it is a string, it will be converted to a
125\class{Charset} instance. If \var{charset} is \code{None}, the
126\code{charset} parameter will be removed from the
127\mailheader{Content-Type} header. Anything else will generate a
128\exception{TypeError}.
129
130The message will be assumed to be of type \mimetype{text/*} encoded with
131\code{charset.input_charset}. It will be converted to
132\code{charset.output_charset}
133and encoded properly, if needed, when generating the plain text
134representation of the message. MIME headers
135(\mailheader{MIME-Version}, \mailheader{Content-Type},
136\mailheader{Content-Transfer-Encoding}) will be added as needed.
137
138\versionadded{2.2.2}
139\end{methoddesc}
140
141\begin{methoddesc}[Message]{get_charset}{}
142Return the \class{Charset} instance associated with the message's payload.
143\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000144\end{methoddesc}
145
146The following methods implement a mapping-like interface for accessing
Barry Warsaw5db478f2002-10-01 04:33:16 +0000147the message's \rfc{2822} headers. Note that there are some
Barry Warsaw5e634632001-09-26 05:23:47 +0000148semantic differences between these methods and a normal mapping
149(i.e. dictionary) interface. For example, in a dictionary there are
150no duplicate keys, but here there may be duplicate message headers. Also,
151in dictionaries there is no guaranteed order to the keys returned by
Barry Warsaw5db478f2002-10-01 04:33:16 +0000152\method{keys()}, but in a \class{Message} object, headers are always
153returned in the order they appeared in the original message, or were
154added to the message later. Any header deleted and then re-added are
155always appended to the end of the header list.
156
157These semantic differences are intentional and are biased toward
158maximal convenience.
Barry Warsaw5e634632001-09-26 05:23:47 +0000159
Barry Warsaw5b9da892002-10-01 01:05:52 +0000160Note that in all cases, any envelope header present in the message is
161not included in the mapping interface.
Barry Warsaw5e634632001-09-26 05:23:47 +0000162
163\begin{methoddesc}[Message]{__len__}{}
164Return the total number of headers, including duplicates.
165\end{methoddesc}
166
167\begin{methoddesc}[Message]{__contains__}{name}
168Return true if the message object has a field named \var{name}.
Andrew M. Kuchling43dc1fc2001-11-05 01:55:03 +0000169Matching is done case-insensitively and \var{name} should not include the
Barry Warsaw5e634632001-09-26 05:23:47 +0000170trailing colon. Used for the \code{in} operator,
171e.g.:
172
173\begin{verbatim}
174if 'message-id' in myMessage:
175 print 'Message-ID:', myMessage['message-id']
176\end{verbatim}
177\end{methoddesc}
178
179\begin{methoddesc}[Message]{__getitem__}{name}
180Return the value of the named header field. \var{name} should not
181include the colon field separator. If the header is missing,
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000182\code{None} is returned; a \exception{KeyError} is never raised.
Barry Warsaw5e634632001-09-26 05:23:47 +0000183
184Note that if the named field appears more than once in the message's
185headers, exactly which of those field values will be returned is
186undefined. Use the \method{get_all()} method to get the values of all
187the extant named headers.
188\end{methoddesc}
189
190\begin{methoddesc}[Message]{__setitem__}{name, val}
191Add a header to the message with field name \var{name} and value
192\var{val}. The field is appended to the end of the message's existing
193fields.
194
195Note that this does \emph{not} overwrite or delete any existing header
196with the same name. If you want to ensure that the new header is the
197only one present in the message with field name
Barry Warsaw5db478f2002-10-01 04:33:16 +0000198\var{name}, delete the field first, e.g.:
Barry Warsaw5e634632001-09-26 05:23:47 +0000199
200\begin{verbatim}
201del msg['subject']
202msg['subject'] = 'Python roolz!'
203\end{verbatim}
204\end{methoddesc}
205
206\begin{methoddesc}[Message]{__delitem__}{name}
207Delete all occurrences of the field with name \var{name} from the
208message's headers. No exception is raised if the named field isn't
209present in the headers.
210\end{methoddesc}
211
212\begin{methoddesc}[Message]{has_key}{name}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000213Return true if the message contains a header field named \var{name},
214otherwise return false.
Barry Warsaw5e634632001-09-26 05:23:47 +0000215\end{methoddesc}
216
217\begin{methoddesc}[Message]{keys}{}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000218Return a list of all the message's header field names.
Barry Warsaw5e634632001-09-26 05:23:47 +0000219\end{methoddesc}
220
221\begin{methoddesc}[Message]{values}{}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000222Return a list of all the message's field values.
Barry Warsaw5e634632001-09-26 05:23:47 +0000223\end{methoddesc}
224
225\begin{methoddesc}[Message]{items}{}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000226Return a list of 2-tuples containing all the message's field headers
Barry Warsaw5db478f2002-10-01 04:33:16 +0000227and values.
Barry Warsaw5e634632001-09-26 05:23:47 +0000228\end{methoddesc}
229
230\begin{methoddesc}[Message]{get}{name\optional{, failobj}}
231Return the value of the named header field. This is identical to
232\method{__getitem__()} except that optional \var{failobj} is returned
233if the named header is missing (defaults to \code{None}).
234\end{methoddesc}
235
236Here are some additional useful methods:
237
238\begin{methoddesc}[Message]{get_all}{name\optional{, failobj}}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000239Return a list of all the values for the field named \var{name}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000240If there are no such named headers in the message, \var{failobj} is
241returned (defaults to \code{None}).
242\end{methoddesc}
243
244\begin{methoddesc}[Message]{add_header}{_name, _value, **_params}
245Extended header setting. This method is similar to
246\method{__setitem__()} except that additional header parameters can be
Barry Warsaw5b9da892002-10-01 01:05:52 +0000247provided as keyword arguments. \var{_name} is the header field to add
248and \var{_value} is the \emph{primary} value for the header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000249
250For each item in the keyword argument dictionary \var{_params}, the
251key is taken as the parameter name, with underscores converted to
252dashes (since dashes are illegal in Python identifiers). Normally,
253the parameter will be added as \code{key="value"} unless the value is
254\code{None}, in which case only the key will be added.
255
256Here's an example:
257
258\begin{verbatim}
259msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
260\end{verbatim}
261
262This will add a header that looks like
263
264\begin{verbatim}
265Content-Disposition: attachment; filename="bud.gif"
266\end{verbatim}
267\end{methoddesc}
268
Barry Warsaw5b9da892002-10-01 01:05:52 +0000269\begin{methoddesc}[Message]{replace_header}{_name, _value}
270Replace a header. Replace the first header found in the message that
271matches \var{_name}, retaining header order and field name case. If
272no matching header was found, a \exception{KeyError} is raised.
273
274\versionadded{2.2.2}
275\end{methoddesc}
276
277\begin{methoddesc}[Message]{get_content_type}{}
278Return the message's content type. The returned string is coerced to
279lower case of the form \mimetype{maintype/subtype}. If there was no
280\mailheader{Content-Type} header in the message the default type as
281given by \method{get_default_type()} will be returned. Since
282according to \rfc{2045}, messages always have a default type,
283\method{get_content_type()} will always return a value.
284
285\rfc{2045} defines a message's default type to be
286\mimetype{text/plain} unless it appears inside a
287\mimetype{multipart/digest} container, in which case it would be
288\mimetype{message/rfc822}. If the \mailheader{Content-Type} header
289has an invalid type specification, \rfc{2045} mandates that the
290default type be \mimetype{text/plain}.
291
292\versionadded{2.2.2}
293\end{methoddesc}
294
295\begin{methoddesc}[Message]{get_content_maintype}{}
296Return the message's main content type. This is the
297\mimetype{maintype} part of the string returned by
298\method{get_content_type()}.
299
300\versionadded{2.2.2}
301\end{methoddesc}
302
303\begin{methoddesc}[Message]{get_content_subtype}{}
304Return the message's sub-content type. This is the \mimetype{subtype}
305part of the string returned by \method{get_content_type()}.
306
307\versionadded{2.2.2}
308\end{methoddesc}
309
310\begin{methoddesc}[Message]{get_default_type}{}
311Return the default content type. Most messages have a default content
312type of \mimetype{text/plain}, except for messages that are subparts
313of \mimetype{multipart/digest} containers. Such subparts have a
314default content type of \mimetype{message/rfc822}.
315
316\versionadded{2.2.2}
317\end{methoddesc}
318
319\begin{methoddesc}[Message]{set_default_type}{ctype}
320Set the default content type. \var{ctype} should either be
321\mimetype{text/plain} or \mimetype{message/rfc822}, although this is
322not enforced. The default content type is not stored in the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000323\mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000324
Barry Warsaw5b9da892002-10-01 01:05:52 +0000325\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000326\end{methoddesc}
327
Barry Warsaw5b9da892002-10-01 01:05:52 +0000328\begin{methoddesc}[Message]{get_params}{\optional{failobj\optional{,
329 header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000330Return the message's \mailheader{Content-Type} parameters, as a list. The
Barry Warsaw5e634632001-09-26 05:23:47 +0000331elements of the returned list are 2-tuples of key/value pairs, as
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000332split on the \character{=} sign. The left hand side of the
333\character{=} is the key, while the right hand side is the value. If
334there is no \character{=} sign in the parameter the value is the empty
Barry Warsaw5b9da892002-10-01 01:05:52 +0000335string, otherwise the value is as described in \method{get_param()} and is
336unquoted if optional \var{unquote} is \code{True} (the default).
Barry Warsaw5e634632001-09-26 05:23:47 +0000337
338Optional \var{failobj} is the object to return if there is no
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000339\mailheader{Content-Type} header. Optional \var{header} is the header to
340search instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000341
342\versionchanged[\var{unquote} argument added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000343\end{methoddesc}
344
345\begin{methoddesc}[Message]{get_param}{param\optional{,
Barry Warsaw5b9da892002-10-01 01:05:52 +0000346 failobj\optional{, header\optional{, unquote}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000347Return the value of the \mailheader{Content-Type} header's parameter
348\var{param} as a string. If the message has no \mailheader{Content-Type}
Barry Warsaw5e634632001-09-26 05:23:47 +0000349header or if there is no such parameter, then \var{failobj} is
350returned (defaults to \code{None}).
351
352Optional \var{header} if given, specifies the message header to use
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000353instead of \mailheader{Content-Type}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000354
355Parameter keys are always compared case insensitively. The return
356value can either be a string, or a 3-tuple if the parameter was
357\rfc{2231} encoded. When it's a 3-tuple, the elements of the value are of
Barry Warsaw463c5a82003-08-19 04:26:59 +0000358the form \code{(CHARSET, LANGUAGE, VALUE)}. Note that both \code{CHARSET} and
359\code{LANGUAGE} can be \code{None}, in which case you should consider
360\code{VALUE} to be encoded in the \code{us-ascii} charset. You can
361usually ignore \code{LANGUAGE}.
362
Barry Warsawbb113862004-10-03 03:16:19 +0000363If your application doesn't care whether the parameter was encoded as in
364\rfc{2231}, you can collapse the parameter value by calling
365\function{email.Utils.collapse_rfc2231_value()}, passing in the return value
366from \method{get_param()}. This will return a suitably decoded Unicode string
367whn the value is a tuple, or the original string unquoted if it isn't. For
368example:
Barry Warsaw5b9da892002-10-01 01:05:52 +0000369
370\begin{verbatim}
Barry Warsawbb113862004-10-03 03:16:19 +0000371rawparam = msg.get_param('foo')
372param = email.Utils.collapse_rfc2231_value(rawparam)
Barry Warsaw5b9da892002-10-01 01:05:52 +0000373\end{verbatim}
374
375In any case, the parameter value (either the returned string, or the
Barry Warsaw5db478f2002-10-01 04:33:16 +0000376\code{VALUE} item in the 3-tuple) is always unquoted, unless
Barry Warsaw5b9da892002-10-01 01:05:52 +0000377\var{unquote} is set to \code{False}.
378
379\versionchanged[\var{unquote} argument added, and 3-tuple return value
380possible]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000381\end{methoddesc}
382
Barry Warsaw5b9da892002-10-01 01:05:52 +0000383\begin{methoddesc}[Message]{set_param}{param, value\optional{,
384 header\optional{, requote\optional{, charset\optional{, language}}}}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000385
Barry Warsaw5b9da892002-10-01 01:05:52 +0000386Set a parameter in the \mailheader{Content-Type} header. If the
387parameter already exists in the header, its value will be replaced
388with \var{value}. If the \mailheader{Content-Type} header as not yet
389been defined for this message, it will be set to \mimetype{text/plain}
390and the new parameter value will be appended as per \rfc{2045}.
391
392Optional \var{header} specifies an alternative header to
393\mailheader{Content-Type}, and all parameters will be quoted as
394necessary unless optional \var{requote} is \code{False} (the default
395is \code{True}).
396
397If optional \var{charset} is specified, the parameter will be encoded
398according to \rfc{2231}. Optional \var{language} specifies the RFC
3992231 language, defaulting to the empty string. Both \var{charset} and
400\var{language} should be strings.
401
402\versionadded{2.2.2}
403\end{methoddesc}
404
405\begin{methoddesc}[Message]{del_param}{param\optional{, header\optional{,
406 requote}}}
407Remove the given parameter completely from the
408\mailheader{Content-Type} header. The header will be re-written in
409place without the parameter or its value. All values will be quoted
410as necessary unless \var{requote} is \code{False} (the default is
Barry Warsaw5db478f2002-10-01 04:33:16 +0000411\code{True}). Optional \var{header} specifies an alternative to
Barry Warsaw5b9da892002-10-01 01:05:52 +0000412\mailheader{Content-Type}.
413
414\versionadded{2.2.2}
415\end{methoddesc}
416
417\begin{methoddesc}[Message]{set_type}{type\optional{, header}\optional{,
418 requote}}
419Set the main type and subtype for the \mailheader{Content-Type}
420header. \var{type} must be a string in the form
421\mimetype{maintype/subtype}, otherwise a \exception{ValueError} is
422raised.
423
424This method replaces the \mailheader{Content-Type} header, keeping all
425the parameters in place. If \var{requote} is \code{False}, this
426leaves the existing header's quoting as is, otherwise the parameters
427will be quoted (the default).
428
429An alternative header can be specified in the \var{header} argument.
Barry Warsaw5db478f2002-10-01 04:33:16 +0000430When the \mailheader{Content-Type} header is set a
431\mailheader{MIME-Version} header is also added.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000432
433\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000434\end{methoddesc}
435
436\begin{methoddesc}[Message]{get_filename}{\optional{failobj}}
437Return the value of the \code{filename} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000438\mailheader{Content-Disposition} header of the message, or \var{failobj} if
Barry Warsaw5e634632001-09-26 05:23:47 +0000439either the header is missing, or has no \code{filename} parameter.
440The returned string will always be unquoted as per
441\method{Utils.unquote()}.
442\end{methoddesc}
443
444\begin{methoddesc}[Message]{get_boundary}{\optional{failobj}}
445Return the value of the \code{boundary} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000446\mailheader{Content-Type} header of the message, or \var{failobj} if either
Barry Warsaw5e634632001-09-26 05:23:47 +0000447the header is missing, or has no \code{boundary} parameter. The
448returned string will always be unquoted as per
449\method{Utils.unquote()}.
450\end{methoddesc}
451
452\begin{methoddesc}[Message]{set_boundary}{boundary}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000453Set the \code{boundary} parameter of the \mailheader{Content-Type}
454header to \var{boundary}. \method{set_boundary()} will always quote
455\var{boundary} if necessary. A \exception{HeaderParseError} is raised
456if the message object has no \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000457
458Note that using this method is subtly different than deleting the old
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000459\mailheader{Content-Type} header and adding a new one with the new boundary
Barry Warsaw5e634632001-09-26 05:23:47 +0000460via \method{add_header()}, because \method{set_boundary()} preserves the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000461order of the \mailheader{Content-Type} header in the list of headers.
Barry Warsaw5e634632001-09-26 05:23:47 +0000462However, it does \emph{not} preserve any continuation lines which may
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000463have been present in the original \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000464\end{methoddesc}
465
Barry Warsaw5b9da892002-10-01 01:05:52 +0000466\begin{methoddesc}[Message]{get_content_charset}{\optional{failobj}}
467Return the \code{charset} parameter of the \mailheader{Content-Type}
Barry Warsaw57ce1432002-10-10 15:22:16 +0000468header, coerced to lower case. If there is no
469\mailheader{Content-Type} header, or if that header has no
470\code{charset} parameter, \var{failobj} is returned.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000471
Barry Warsaw5db478f2002-10-01 04:33:16 +0000472Note that this method differs from \method{get_charset()} which
473returns the \class{Charset} instance for the default encoding of the
474message body.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000475
476\versionadded{2.2.2}
477\end{methoddesc}
478
479\begin{methoddesc}[Message]{get_charsets}{\optional{failobj}}
480Return a list containing the character set names in the message. If
481the message is a \mimetype{multipart}, then the list will contain one
482element for each subpart in the payload, otherwise, it will be a list
483of length 1.
484
485Each item in the list will be a string which is the value of the
486\code{charset} parameter in the \mailheader{Content-Type} header for the
487represented subpart. However, if the subpart has no
488\mailheader{Content-Type} header, no \code{charset} parameter, or is not of
489the \mimetype{text} main MIME type, then that item in the returned list
490will be \var{failobj}.
491\end{methoddesc}
492
Barry Warsaw5e634632001-09-26 05:23:47 +0000493\begin{methoddesc}[Message]{walk}{}
494The \method{walk()} method is an all-purpose generator which can be
495used to iterate over all the parts and subparts of a message object
496tree, in depth-first traversal order. You will typically use
Barry Warsaw5db478f2002-10-01 04:33:16 +0000497\method{walk()} as the iterator in a \code{for} loop; each
Barry Warsaw5e634632001-09-26 05:23:47 +0000498iteration returns the next subpart.
499
Barry Warsaw5db478f2002-10-01 04:33:16 +0000500Here's an example that prints the MIME type of every part of a
501multipart message structure:
Barry Warsaw5e634632001-09-26 05:23:47 +0000502
503\begin{verbatim}
504>>> for part in msg.walk():
Edward Loperad512262004-09-28 02:56:45 +0000505... print part.get_content_type()
Barry Warsaw5e634632001-09-26 05:23:47 +0000506multipart/report
507text/plain
508message/delivery-status
509text/plain
510text/plain
511message/rfc822
512\end{verbatim}
513\end{methoddesc}
514
515\class{Message} objects can also optionally contain two instance
516attributes, which can be used when generating the plain text of a MIME
517message.
518
519\begin{datadesc}{preamble}
520The format of a MIME document allows for some text between the blank
521line following the headers, and the first multipart boundary string.
522Normally, this text is never visible in a MIME-aware mail reader
523because it falls outside the standard MIME armor. However, when
524viewing the raw text of the message, or when viewing the message in a
525non-MIME aware reader, this text can become visible.
526
527The \var{preamble} attribute contains this leading extra-armor text
528for MIME documents. When the \class{Parser} discovers some text after
529the headers but before the first boundary string, it assigns this text
530to the message's \var{preamble} attribute. When the \class{Generator}
531is writing out the plain text representation of a MIME message, and it
532finds the message has a \var{preamble} attribute, it will write this
Barry Warsaw5b9da892002-10-01 01:05:52 +0000533text in the area between the headers and the first boundary. See
534\refmodule{email.Parser} and \refmodule{email.Generator} for details.
Barry Warsaw5e634632001-09-26 05:23:47 +0000535
536Note that if the message object has no preamble, the
537\var{preamble} attribute will be \code{None}.
538\end{datadesc}
539
540\begin{datadesc}{epilogue}
541The \var{epilogue} attribute acts the same way as the \var{preamble}
542attribute, except that it contains text that appears between the last
543boundary and the end of the message.
Barry Warsawe736d932001-10-19 04:34:42 +0000544
545One note: when generating the flat text for a \mimetype{multipart}
546message that has no \var{epilogue} (using the standard
547\class{Generator} class), no newline is added after the closing
548boundary line. If the message object has an \var{epilogue} and its
549value does not start with a newline, a newline is printed after the
550closing boundary. This seems a little clumsy, but it makes the most
551practical sense. The upshot is that if you want to ensure that a
552newline get printed after your closing \mimetype{multipart} boundary,
553set the \var{epilogue} to the empty string.
Barry Warsaw5e634632001-09-26 05:23:47 +0000554\end{datadesc}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000555
Barry Warsawbb113862004-10-03 03:16:19 +0000556\begin{datadesc}{defects}
557The \var{defects} attribute contains a list of all the problems found when
558parsing this message. See \refmodule{email.Errors} for a detailed description
559of the possible parsing defects.
560
561\versionadded{2.4}
562\end{datadesc}
563
Barry Warsaw5b9da892002-10-01 01:05:52 +0000564\subsubsection{Deprecated methods}
565
Barry Warsawbb113862004-10-03 03:16:19 +0000566\versionchanged[The \method{add_payload()} method was removed; use the
567\method{attach()} method instead]{2.4}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000568
Barry Warsawbb113862004-10-03 03:16:19 +0000569The following methods are deprecated. They are documented here for
570completeness.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000571
572\begin{methoddesc}[Message]{get_type}{\optional{failobj}}
573Return the message's content type, as a string of the form
574\mimetype{maintype/subtype} as taken from the
575\mailheader{Content-Type} header.
576The returned string is coerced to lowercase.
577
578If there is no \mailheader{Content-Type} header in the message,
579\var{failobj} is returned (defaults to \code{None}).
580
581\deprecated{2.2.2}{Use the \method{get_content_type()} method instead.}
582\end{methoddesc}
583
584\begin{methoddesc}[Message]{get_main_type}{\optional{failobj}}
585Return the message's \emph{main} content type. This essentially returns the
586\var{maintype} part of the string returned by \method{get_type()}, with the
587same semantics for \var{failobj}.
588
589\deprecated{2.2.2}{Use the \method{get_content_maintype()} method instead.}
590\end{methoddesc}
591
592\begin{methoddesc}[Message]{get_subtype}{\optional{failobj}}
593Return the message's sub-content type. This essentially returns the
594\var{subtype} part of the string returned by \method{get_type()}, with the
595same semantics for \var{failobj}.
596
597\deprecated{2.2.2}{Use the \method{get_content_subtype()} method instead.}
598\end{methoddesc}
599