source: vendor/python/2.5/Doc/lib/emailmessage.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 22.6 KB
Line 
1\declaremodule{standard}{email.message}
2\modulesynopsis{The base class representing email messages.}
3
4The central class in the \module{email} package is the
5\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.
9
10Conceptually, 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.
14
15Headers are stored and returned in case-preserving form but are
16matched case-insensitively. There may also be a single envelope
17header, also known as the \emph{Unix-From} header or the
18\code{From_} header. The payload is either a string in the case of
19simple message objects or a list of \class{Message} objects for
20MIME container documents (e.g. \mimetype{multipart/*} and
21\mimetype{message/rfc822}).
22
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.
29
30Here are the methods of the \class{Message} class:
31
32\begin{classdesc}{Message}{}
33The constructor takes no arguments.
34\end{classdesc}
35
36\begin{methoddesc}[Message]{as_string}{\optional{unixfrom}}
37Return 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}.
40
41Note that this method is provided as a convenience and may not always format
42the message the way you want. For example, by default it mangles lines that
43begin with \code{From }. For more flexibility, instantiate a
44\class{Generator} instance and use its
45\method{flatten()} method directly. For example:
46
47\begin{verbatim}
48from cStringIO import StringIO
49from email.generator import Generator
50fp = StringIO()
51g = Generator(fp, mangle_from_=False, maxheaderlen=60)
52g.flatten(msg)
53text = fp.getvalue()
54\end{verbatim}
55\end{methoddesc}
56
57\begin{methoddesc}[Message]{__str__}{}
58Equivalent to \method{as_string(unixfrom=True)}.
59\end{methoddesc}
60
61\begin{methoddesc}[Message]{is_multipart}{}
62Return \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.
66\end{methoddesc}
67
68\begin{methoddesc}[Message]{set_unixfrom}{unixfrom}
69Set the message's envelope header to \var{unixfrom}, which should be a string.
70\end{methoddesc}
71
72\begin{methoddesc}[Message]{get_unixfrom}{}
73Return the message's envelope header. Defaults to \code{None} if the
74envelope header was never set.
75\end{methoddesc}
76
77\begin{methoddesc}[Message]{attach}{payload}
78Add the given \var{payload} to the current payload, which must be
79\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.
83\end{methoddesc}
84
85\begin{methoddesc}[Message]{get_payload}{\optional{i\optional{, decode}}}
86Return 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.
91
92With optional argument \var{i}, \method{get_payload()} will return the
93\var{i}-th element of the payload, counting from zero, if
94\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
98\exception{TypeError} is raised.
99
100Optional \var{decode} is a flag indicating whether the payload should be
101decoded or not, according to the \mailheader{Content-Transfer-Encoding} header.
102When \code{True} and the message is not a multipart, the payload will be
103decoded if this header's value is \samp{quoted-printable} or
104\samp{base64}. If some other encoding is used, or
105\mailheader{Content-Transfer-Encoding} header is
106missing, 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}.
110\end{methoddesc}
111
112\begin{methoddesc}[Message]{set_payload}{payload\optional{, charset}}
113Set the entire message object's payload to \var{payload}. It is the
114client's responsibility to ensure the payload invariants. Optional
115\var{charset} sets the message's default character set; see
116\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
123either be a \class{Charset} instance (see \refmodule{email.charset}), a
124string 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
132\var{charset.input_charset}. It will be converted to
133\var{charset.output_charset}
134and 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}
145\end{methoddesc}
146
147The following methods implement a mapping-like interface for accessing
148the message's \rfc{2822} headers. Note that there are some
149semantic 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
153\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.
160
161Note that in all cases, any envelope header present in the message is
162not included in the mapping interface.
163
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}.
170Matching is done case-insensitively and \var{name} should not include the
171trailing 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,
183\code{None} is returned; a \exception{KeyError} is never raised.
184
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
199\var{name}, delete the field first, e.g.:
200
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}
214Return true if the message contains a header field named \var{name},
215otherwise return false.
216\end{methoddesc}
217
218\begin{methoddesc}[Message]{keys}{}
219Return a list of all the message's header field names.
220\end{methoddesc}
221
222\begin{methoddesc}[Message]{values}{}
223Return a list of all the message's field values.
224\end{methoddesc}
225
226\begin{methoddesc}[Message]{items}{}
227Return a list of 2-tuples containing all the message's field headers
228and values.
229\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}}
240Return a list of all the values for the field named \var{name}.
241If 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
248provided as keyword arguments. \var{_name} is the header field to add
249and \var{_value} is the \emph{primary} value for the header.
250
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
270\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
324\mailheader{Content-Type} header.
325
326\versionadded{2.2.2}
327\end{methoddesc}
328
329\begin{methoddesc}[Message]{get_params}{\optional{failobj\optional{,
330 header\optional{, unquote}}}}
331Return the message's \mailheader{Content-Type} parameters, as a list. The
332elements of the returned list are 2-tuples of key/value pairs, as
333split 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
336string, otherwise the value is as described in \method{get_param()} and is
337unquoted if optional \var{unquote} is \code{True} (the default).
338
339Optional \var{failobj} is the object to return if there is no
340\mailheader{Content-Type} header. Optional \var{header} is the header to
341search instead of \mailheader{Content-Type}.
342
343\versionchanged[\var{unquote} argument added]{2.2.2}
344\end{methoddesc}
345
346\begin{methoddesc}[Message]{get_param}{param\optional{,
347 failobj\optional{, header\optional{, unquote}}}}
348Return the value of the \mailheader{Content-Type} header's parameter
349\var{param} as a string. If the message has no \mailheader{Content-Type}
350header 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
354instead of \mailheader{Content-Type}.
355
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
359the 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
364If 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:
370
371\begin{verbatim}
372rawparam = msg.get_param('foo')
373param = email.Utils.collapse_rfc2231_value(rawparam)
374\end{verbatim}
375
376In any case, the parameter value (either the returned string, or the
377\code{VALUE} item in the 3-tuple) is always unquoted, unless
378\var{unquote} is set to \code{False}.
379
380\versionchanged[\var{unquote} argument added, and 3-tuple return value
381possible]{2.2.2}
382\end{methoddesc}
383
384\begin{methoddesc}[Message]{set_param}{param, value\optional{,
385 header\optional{, requote\optional{, charset\optional{, language}}}}}
386
387Set 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
412\code{True}). Optional \var{header} specifies an alternative to
413\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.
431When the \mailheader{Content-Type} header is set a
432\mailheader{MIME-Version} header is also added.
433
434\versionadded{2.2.2}
435\end{methoddesc}
436
437\begin{methoddesc}[Message]{get_filename}{\optional{failobj}}
438Return the value of the \code{filename} parameter of the
439\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()}.
444\end{methoddesc}
445
446\begin{methoddesc}[Message]{get_boundary}{\optional{failobj}}
447Return the value of the \code{boundary} parameter of the
448\mailheader{Content-Type} header of the message, or \var{failobj} if either
449the 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}
455Set 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.
459
460Note that using this method is subtly different than deleting the old
461\mailheader{Content-Type} header and adding a new one with the new boundary
462via \method{add_header()}, because \method{set_boundary()} preserves the
463order of the \mailheader{Content-Type} header in the list of headers.
464However, it does \emph{not} preserve any continuation lines which may
465have been present in the original \mailheader{Content-Type} header.
466\end{methoddesc}
467
468\begin{methoddesc}[Message]{get_content_charset}{\optional{failobj}}
469Return the \code{charset} parameter of the \mailheader{Content-Type}
470header, 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.
473
474Note that this method differs from \method{get_charset()} which
475returns the \class{Charset} instance for the default encoding of the
476message body.
477
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
495\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
499\method{walk()} as the iterator in a \code{for} loop; each
500iteration returns the next subpart.
501
502Here's an example that prints the MIME type of every part of a
503multipart message structure:
504
505\begin{verbatim}
506>>> for part in msg.walk():
507... print part.get_content_type()
508multipart/report
509text/plain
510message/delivery-status
511text/plain
512text/plain
513message/rfc822
514\end{verbatim}
515\end{methoddesc}
516
517\versionchanged[The previously deprecated methods \method{get_type()},
518\method{get_main_type()}, and \method{get_subtype()} were removed]{2.5}
519
520\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
538text in the area between the headers and the first boundary. See
539\refmodule{email.parser} and \refmodule{email.generator} for details.
540
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.
549
550\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}
553\end{datadesc}
554
555\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}
Note: See TracBrowser for help on using the repository browser.