blob: ecf24eb7faaa7e48c8b6ceb9bc4d78410bf7259e [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
15matched case-insensitively. There may also be a single
16\emph{Unix-From} header, also known as the envelope header or the
17\code{From_} header. The payload is either a string in the case of
18simple message objects, a list of \class{Message} objects for
19multipart MIME documents, or a single \class{Message} instance for
20\mimetype{message/rfc822} type objects.
21
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}}
36Return the entire formatted message as a string. Optional
37\var{unixfrom}, when true, specifies to include the \emph{Unix-From}
38envelope header; it defaults to 0.
39\end{methoddesc}
40
41\begin{methoddesc}[Message]{__str__()}{}
42Equivalent to \method{aMessage.as_string(unixfrom=1)}.
43\end{methoddesc}
44
45\begin{methoddesc}[Message]{is_multipart}{}
46Return 1 if the message's payload is a list of sub-\class{Message}
47objects, otherwise return 0. When \method{is_multipart()} returns 0,
48the payload should either be a string object, or a single
49\class{Message} instance.
50\end{methoddesc}
51
52\begin{methoddesc}[Message]{set_unixfrom}{unixfrom}
53Set the \emph{Unix-From} (a.k.a envelope header or \code{From_}
54header) to \var{unixfrom}, which should be a string.
55\end{methoddesc}
56
57\begin{methoddesc}[Message]{get_unixfrom}{}
58Return the \emph{Unix-From} header. Defaults to \code{None} if the
59\emph{Unix-From} header was never set.
60\end{methoddesc}
61
62\begin{methoddesc}[Message]{add_payload}{payload}
63Add \var{payload} to the message object's existing payload. If, prior
64to calling this method, the object's payload was \code{None}
65(i.e. never before set), then after this method is called, the payload
66will be the argument \var{payload}.
67
68If the object's payload was already a list
69(i.e. \method{is_multipart()} returns 1), then \var{payload} is
70appended to the end of the existing payload list.
71
72For any other type of existing payload, \method{add_payload()} will
73transform the new payload into a list consisting of the old payload
74and \var{payload}, but only if the document is already a MIME
75multipart document. This condition is satisfied if the message's
Barry Warsawc5f8fe32001-09-26 22:21:52 +000076\mailheader{Content-Type} header's main type is either
77\mimetype{multipart}, or there is no \mailheader{Content-Type}
78header. In any other situation,
Barry Warsaw5e634632001-09-26 05:23:47 +000079\exception{MultipartConversionError} is raised.
80\end{methoddesc}
81
82\begin{methoddesc}[Message]{attach}{payload}
83Synonymous with \method{add_payload()}.
84\end{methoddesc}
85
86\begin{methoddesc}[Message]{get_payload}{\optional{i\optional{, decode}}}
87Return the current payload, which will be a list of \class{Message}
88objects when \method{is_multipart()} returns 1, or a scalar (either a
89string or a single \class{Message} instance) when
90\method{is_multipart()} returns 0.
91
92With optional \var{i}, \method{get_payload()} will return the
93\var{i}-th element of the payload, counting from zero, if
Barry Warsawc5f8fe32001-09-26 22:21:52 +000094\method{is_multipart()} returns 1. An \exception{IndexError} will be raised
Barry Warsaw5e634632001-09-26 05:23:47 +000095if \var{i} is less than 0 or greater than or equal to the number of
96items in the payload. If the payload is scalar
97(i.e. \method{is_multipart()} returns 0) 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 Warsaw5e634632001-09-26 05:23:47 +0000102When 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
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000105\mailheader{Content-Transfer-Encoding} header is
Barry Warsaw5e634632001-09-26 05:23:47 +0000106missing, the payload is returned as-is (undecoded). If the message is
107a multipart and the \var{decode} flag is true, then \code{None} is
108returned.
109\end{methoddesc}
110
111\begin{methoddesc}[Message]{set_payload}{payload}
112Set the entire message object's payload to \var{payload}. It is the
113client's responsibility to ensure the payload invariants.
114\end{methoddesc}
115
116The following methods implement a mapping-like interface for accessing
117the message object's \rfc{2822} headers. Note that there are some
118semantic differences between these methods and a normal mapping
119(i.e. dictionary) interface. For example, in a dictionary there are
120no duplicate keys, but here there may be duplicate message headers. Also,
121in dictionaries there is no guaranteed order to the keys returned by
122\method{keys()}, but in a \class{Message} object, there is an explicit
123order. These semantic differences are intentional and are biased
124toward maximal convenience.
125
126Note that in all cases, any optional \emph{Unix-From} header the message
127may have is not included in the mapping interface.
128
129\begin{methoddesc}[Message]{__len__}{}
130Return the total number of headers, including duplicates.
131\end{methoddesc}
132
133\begin{methoddesc}[Message]{__contains__}{name}
134Return true if the message object has a field named \var{name}.
Andrew M. Kuchling43dc1fc2001-11-05 01:55:03 +0000135Matching is done case-insensitively and \var{name} should not include the
Barry Warsaw5e634632001-09-26 05:23:47 +0000136trailing colon. Used for the \code{in} operator,
137e.g.:
138
139\begin{verbatim}
140if 'message-id' in myMessage:
141 print 'Message-ID:', myMessage['message-id']
142\end{verbatim}
143\end{methoddesc}
144
145\begin{methoddesc}[Message]{__getitem__}{name}
146Return the value of the named header field. \var{name} should not
147include the colon field separator. If the header is missing,
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000148\code{None} is returned; a \exception{KeyError} is never raised.
Barry Warsaw5e634632001-09-26 05:23:47 +0000149
150Note that if the named field appears more than once in the message's
151headers, exactly which of those field values will be returned is
152undefined. Use the \method{get_all()} method to get the values of all
153the extant named headers.
154\end{methoddesc}
155
156\begin{methoddesc}[Message]{__setitem__}{name, val}
157Add a header to the message with field name \var{name} and value
158\var{val}. The field is appended to the end of the message's existing
159fields.
160
161Note that this does \emph{not} overwrite or delete any existing header
162with the same name. If you want to ensure that the new header is the
163only one present in the message with field name
164\var{name}, first use \method{__delitem__()} to delete all named
165fields, e.g.:
166
167\begin{verbatim}
168del msg['subject']
169msg['subject'] = 'Python roolz!'
170\end{verbatim}
171\end{methoddesc}
172
173\begin{methoddesc}[Message]{__delitem__}{name}
174Delete all occurrences of the field with name \var{name} from the
175message's headers. No exception is raised if the named field isn't
176present in the headers.
177\end{methoddesc}
178
179\begin{methoddesc}[Message]{has_key}{name}
180Return 1 if the message contains a header field named \var{name},
181otherwise return 0.
182\end{methoddesc}
183
184\begin{methoddesc}[Message]{keys}{}
185Return a list of all the message's header field names. These keys
186will be sorted in the order in which they were added to the message
187via \method{__setitem__()}, and may contain duplicates. Any fields
188deleted and then subsequently re-added are always appended to the end
189of the header list.
190\end{methoddesc}
191
192\begin{methoddesc}[Message]{values}{}
193Return a list of all the message's field values. These will be sorted
194in the order in which they were added to the message via
195\method{__setitem__()}, and may contain duplicates. Any fields
196deleted and then subsequently re-added are always appended to the end
197of the header list.
198\end{methoddesc}
199
200\begin{methoddesc}[Message]{items}{}
201Return a list of 2-tuples containing all the message's field headers and
202values. These will be sorted in the order in which they were added to
203the message via \method{__setitem__()}, and may contain duplicates.
204Any fields deleted and then subsequently re-added are always appended
205to the end of the header list.
206\end{methoddesc}
207
208\begin{methoddesc}[Message]{get}{name\optional{, failobj}}
209Return the value of the named header field. This is identical to
210\method{__getitem__()} except that optional \var{failobj} is returned
211if the named header is missing (defaults to \code{None}).
212\end{methoddesc}
213
214Here are some additional useful methods:
215
216\begin{methoddesc}[Message]{get_all}{name\optional{, failobj}}
217Return a list of all the values for the field named \var{name}. These
218will be sorted in the order in which they were added to the message
219via \method{__setitem__()}. Any fields
220deleted and then subsequently re-added are always appended to the end
221of the list.
222
223If there are no such named headers in the message, \var{failobj} is
224returned (defaults to \code{None}).
225\end{methoddesc}
226
227\begin{methoddesc}[Message]{add_header}{_name, _value, **_params}
228Extended header setting. This method is similar to
229\method{__setitem__()} except that additional header parameters can be
230provided as keyword arguments. \var{_name} is the header to set and
231\var{_value} is the \emph{primary} value for the header.
232
233For each item in the keyword argument dictionary \var{_params}, the
234key is taken as the parameter name, with underscores converted to
235dashes (since dashes are illegal in Python identifiers). Normally,
236the parameter will be added as \code{key="value"} unless the value is
237\code{None}, in which case only the key will be added.
238
239Here's an example:
240
241\begin{verbatim}
242msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
243\end{verbatim}
244
245This will add a header that looks like
246
247\begin{verbatim}
248Content-Disposition: attachment; filename="bud.gif"
249\end{verbatim}
250\end{methoddesc}
251
252\begin{methoddesc}[Message]{get_type}{\optional{failobj}}
253Return the message's content type, as a string of the form
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000254\mimetype{maintype/subtype} as taken from the
255\mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000256The returned string is coerced to lowercase.
257
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000258If there is no \mailheader{Content-Type} header in the message,
Barry Warsaw5e634632001-09-26 05:23:47 +0000259\var{failobj} is returned (defaults to \code{None}).
260\end{methoddesc}
261
262\begin{methoddesc}[Message]{get_main_type}{\optional{failobj}}
263Return the message's \emph{main} content type. This essentially returns the
264\var{maintype} part of the string returned by \method{get_type()}, with the
265same semantics for \var{failobj}.
266\end{methoddesc}
267
268\begin{methoddesc}[Message]{get_subtype}{\optional{failobj}}
269Return the message's sub-content type. This essentially returns the
270\var{subtype} part of the string returned by \method{get_type()}, with the
271same semantics for \var{failobj}.
272\end{methoddesc}
273
274\begin{methoddesc}[Message]{get_params}{\optional{failobj\optional{, header}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000275Return the message's \mailheader{Content-Type} parameters, as a list. The
Barry Warsaw5e634632001-09-26 05:23:47 +0000276elements of the returned list are 2-tuples of key/value pairs, as
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000277split on the \character{=} sign. The left hand side of the
278\character{=} is the key, while the right hand side is the value. If
279there is no \character{=} sign in the parameter the value is the empty
280string. The value is always unquoted with \method{Utils.unquote()}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000281
282Optional \var{failobj} is the object to return if there is no
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000283\mailheader{Content-Type} header. Optional \var{header} is the header to
284search instead of \mailheader{Content-Type}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000285\end{methoddesc}
286
287\begin{methoddesc}[Message]{get_param}{param\optional{,
288 failobj\optional{, header}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000289Return the value of the \mailheader{Content-Type} header's parameter
290\var{param} as a string. If the message has no \mailheader{Content-Type}
Barry Warsaw5e634632001-09-26 05:23:47 +0000291header or if there is no such parameter, then \var{failobj} is
292returned (defaults to \code{None}).
293
294Optional \var{header} if given, specifies the message header to use
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000295instead of \mailheader{Content-Type}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000296\end{methoddesc}
297
298\begin{methoddesc}[Message]{get_charsets}{\optional{failobj}}
299Return a list containing the character set names in the message. If
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000300the message is a \mimetype{multipart}, then the list will contain one
Barry Warsaw5e634632001-09-26 05:23:47 +0000301element for each subpart in the payload, otherwise, it will be a list
302of length 1.
303
304Each item in the list will be a string which is the value of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000305\code{charset} parameter in the \mailheader{Content-Type} header for the
Barry Warsaw5e634632001-09-26 05:23:47 +0000306represented subpart. However, if the subpart has no
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000307\mailheader{Content-Type} header, no \code{charset} parameter, or is not of
308the \mimetype{text} main MIME type, then that item in the returned list
Barry Warsaw5e634632001-09-26 05:23:47 +0000309will be \var{failobj}.
310\end{methoddesc}
311
312\begin{methoddesc}[Message]{get_filename}{\optional{failobj}}
313Return the value of the \code{filename} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000314\mailheader{Content-Disposition} header of the message, or \var{failobj} if
Barry Warsaw5e634632001-09-26 05:23:47 +0000315either the header is missing, or has no \code{filename} parameter.
316The returned string will always be unquoted as per
317\method{Utils.unquote()}.
318\end{methoddesc}
319
320\begin{methoddesc}[Message]{get_boundary}{\optional{failobj}}
321Return the value of the \code{boundary} parameter of the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000322\mailheader{Content-Type} header of the message, or \var{failobj} if either
Barry Warsaw5e634632001-09-26 05:23:47 +0000323the header is missing, or has no \code{boundary} parameter. The
324returned string will always be unquoted as per
325\method{Utils.unquote()}.
326\end{methoddesc}
327
328\begin{methoddesc}[Message]{set_boundary}{boundary}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000329Set the \code{boundary} parameter of the \mailheader{Content-Type} header
Barry Warsaw5e634632001-09-26 05:23:47 +0000330to \var{boundary}. \method{set_boundary()} will always quote
331\var{boundary} so you should not quote it yourself. A
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000332\exception{HeaderParseError} is raised if the message object has no
333\mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000334
335Note that using this method is subtly different than deleting the old
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000336\mailheader{Content-Type} header and adding a new one with the new boundary
Barry Warsaw5e634632001-09-26 05:23:47 +0000337via \method{add_header()}, because \method{set_boundary()} preserves the
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000338order of the \mailheader{Content-Type} header in the list of headers.
Barry Warsaw5e634632001-09-26 05:23:47 +0000339However, it does \emph{not} preserve any continuation lines which may
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000340have been present in the original \mailheader{Content-Type} header.
Barry Warsaw5e634632001-09-26 05:23:47 +0000341\end{methoddesc}
342
343\begin{methoddesc}[Message]{walk}{}
344The \method{walk()} method is an all-purpose generator which can be
345used to iterate over all the parts and subparts of a message object
346tree, in depth-first traversal order. You will typically use
347\method{walk()} as the iterator in a \code{for ... in} loop; each
348iteration returns the next subpart.
349
350Here's an example that prints the MIME type of every part of a message
351object tree:
352
353\begin{verbatim}
354>>> for part in msg.walk():
355>>> print part.get_type('text/plain')
356multipart/report
357text/plain
358message/delivery-status
359text/plain
360text/plain
361message/rfc822
362\end{verbatim}
363\end{methoddesc}
364
365\class{Message} objects can also optionally contain two instance
366attributes, which can be used when generating the plain text of a MIME
367message.
368
369\begin{datadesc}{preamble}
370The format of a MIME document allows for some text between the blank
371line following the headers, and the first multipart boundary string.
372Normally, this text is never visible in a MIME-aware mail reader
373because it falls outside the standard MIME armor. However, when
374viewing the raw text of the message, or when viewing the message in a
375non-MIME aware reader, this text can become visible.
376
377The \var{preamble} attribute contains this leading extra-armor text
378for MIME documents. When the \class{Parser} discovers some text after
379the headers but before the first boundary string, it assigns this text
380to the message's \var{preamble} attribute. When the \class{Generator}
381is writing out the plain text representation of a MIME message, and it
382finds the message has a \var{preamble} attribute, it will write this
383text in the area between the headers and the first boundary.
384
385Note that if the message object has no preamble, the
386\var{preamble} attribute will be \code{None}.
387\end{datadesc}
388
389\begin{datadesc}{epilogue}
390The \var{epilogue} attribute acts the same way as the \var{preamble}
391attribute, except that it contains text that appears between the last
392boundary and the end of the message.
Barry Warsawe736d932001-10-19 04:34:42 +0000393
394One note: when generating the flat text for a \mimetype{multipart}
395message that has no \var{epilogue} (using the standard
396\class{Generator} class), no newline is added after the closing
397boundary line. If the message object has an \var{epilogue} and its
398value does not start with a newline, a newline is printed after the
399closing boundary. This seems a little clumsy, but it makes the most
400practical sense. The upshot is that if you want to ensure that a
401newline get printed after your closing \mimetype{multipart} boundary,
402set the \var{epilogue} to the empty string.
Barry Warsaw5e634632001-09-26 05:23:47 +0000403\end{datadesc}