Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 1 | \declaremodule{standard}{email.Message} |
| 2 | \modulesynopsis{The base class representing email messages.} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 3 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 4 | The central class in the \module{email} package is the |
| 5 | \class{Message} class; it is the base class for the \module{email} |
| 6 | object model. \class{Message} provides the core functionality for |
| 7 | setting and querying header fields, and for accessing message bodies. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 8 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 9 | Conceptually, a \class{Message} object consists of \emph{headers} and |
| 10 | \emph{payloads}. Headers are \rfc{2822} style field names and |
| 11 | values where the field name and value are separated by a colon. The |
| 12 | colon is not part of either the field name or the field value. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 13 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 14 | Headers are stored and returned in case-preserving form but are |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 15 | matched case-insensitively. There may also be a single envelope |
| 16 | header, also known as the \emph{Unix-From} header or the |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 17 | \code{From_} header. The payload is either a string in the case of |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 18 | simple message objects or a list of \class{Message} objects for |
| 19 | MIME container documents (e.g. \mimetype{multipart/*} and |
| 20 | \mimetype{message/rfc822}). |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 21 | |
| 22 | \class{Message} objects provide a mapping style interface for |
| 23 | accessing the message headers, and an explicit interface for accessing |
| 24 | both the headers and the payload. It provides convenience methods for |
| 25 | generating a flat text representation of the message object tree, for |
| 26 | accessing commonly used header parameters, and for recursively walking |
| 27 | over the object tree. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 28 | |
| 29 | Here are the methods of the \class{Message} class: |
| 30 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 31 | \begin{classdesc}{Message}{} |
| 32 | The constructor takes no arguments. |
| 33 | \end{classdesc} |
| 34 | |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 35 | \begin{methoddesc}[Message]{as_string}{\optional{unixfrom}} |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 36 | Return the entire message flatten as a string. When optional |
| 37 | \var{unixfrom} is \code{True}, the envelope header is included in the |
| 38 | returned string. \var{unixfrom} defaults to \code{False}. |
Barry Warsaw | b05df57 | 2003-04-18 23:03:53 +0000 | [diff] [blame] | 39 | |
Barry Warsaw | 2ec4854 | 2004-10-03 03:39:47 +0000 | [diff] [blame] | 40 | Note that this method is provided as a convenience and may not always format |
Michael W. Hudson | bfe5684 | 2004-10-03 09:41:26 +0000 | [diff] [blame] | 41 | the message the way you want. For example, by default it mangles lines that |
Barry Warsaw | 2ec4854 | 2004-10-03 03:39:47 +0000 | [diff] [blame] | 42 | begin with \code{From }. For more flexibility, instantiate a |
| 43 | \class{Generator} instance and use its |
Barry Warsaw | b05df57 | 2003-04-18 23:03:53 +0000 | [diff] [blame] | 44 | \method{flatten()} method directly. For example: |
| 45 | |
| 46 | \begin{verbatim} |
| 47 | from cStringIO import StringIO |
| 48 | from email.Generator import Generator |
| 49 | fp = StringIO() |
Andrew M. Kuchling | b4b9ced | 2003-10-31 19:52:30 +0000 | [diff] [blame] | 50 | g = Generator(fp, mangle_from_=False, maxheaderlen=60) |
Barry Warsaw | b05df57 | 2003-04-18 23:03:53 +0000 | [diff] [blame] | 51 | g.flatten(msg) |
| 52 | text = fp.getvalue() |
| 53 | \end{verbatim} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 54 | \end{methoddesc} |
| 55 | |
Fred Drake | 7779b20 | 2002-05-22 20:44:03 +0000 | [diff] [blame] | 56 | \begin{methoddesc}[Message]{__str__}{} |
Fred Drake | 6516e14 | 2002-10-01 14:29:58 +0000 | [diff] [blame] | 57 | Equivalent to \method{as_string(unixfrom=True)}. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 58 | \end{methoddesc} |
| 59 | |
| 60 | \begin{methoddesc}[Message]{is_multipart}{} |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 61 | Return \code{True} if the message's payload is a list of |
| 62 | sub-\class{Message} objects, otherwise return \code{False}. When |
| 63 | \method{is_multipart()} returns False, the payload should be a string |
| 64 | object. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 65 | \end{methoddesc} |
| 66 | |
| 67 | \begin{methoddesc}[Message]{set_unixfrom}{unixfrom} |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 68 | Set the message's envelope header to \var{unixfrom}, which should be a string. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 69 | \end{methoddesc} |
| 70 | |
| 71 | \begin{methoddesc}[Message]{get_unixfrom}{} |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 72 | Return the message's envelope header. Defaults to \code{None} if the |
| 73 | envelope header was never set. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 74 | \end{methoddesc} |
| 75 | |
| 76 | \begin{methoddesc}[Message]{attach}{payload} |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 77 | Add the given \var{payload} to the current payload, which must be |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 78 | \code{None} or a list of \class{Message} objects before the call. |
| 79 | After the call, the payload will always be a list of \class{Message} |
| 80 | objects. If you want to set the payload to a scalar object (e.g. a |
| 81 | string), use \method{set_payload()} instead. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 82 | \end{methoddesc} |
| 83 | |
| 84 | \begin{methoddesc}[Message]{get_payload}{\optional{i\optional{, decode}}} |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 85 | Return a reference the current payload, which will be a list of |
| 86 | \class{Message} objects when \method{is_multipart()} is \code{True}, or a |
| 87 | string when \method{is_multipart()} is \code{False}. If the |
| 88 | payload is a list and you mutate the list object, you modify the |
| 89 | message's payload in place. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 90 | |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 91 | With optional argument \var{i}, \method{get_payload()} will return the |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 92 | \var{i}-th element of the payload, counting from zero, if |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 93 | \method{is_multipart()} is \code{True}. An \exception{IndexError} |
| 94 | will be raised if \var{i} is less than 0 or greater than or equal to |
| 95 | the 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 Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 97 | \exception{TypeError} is raised. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 98 | |
| 99 | Optional \var{decode} is a flag indicating whether the payload should be |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 100 | decoded or not, according to the \mailheader{Content-Transfer-Encoding} header. |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 101 | When \code{True} and the message is not a multipart, the payload will be |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 102 | decoded if this header's value is \samp{quoted-printable} or |
| 103 | \samp{base64}. If some other encoding is used, or |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 104 | \mailheader{Content-Transfer-Encoding} header is |
Barry Warsaw | 20ebc37 | 2003-03-10 16:13:50 +0000 | [diff] [blame] | 105 | missing, or if the payload has bogus base64 data, the payload is |
| 106 | returned as-is (undecoded). If the message is a multipart and the |
| 107 | \var{decode} flag is \code{True}, then \code{None} is returned. The |
| 108 | default for \var{decode} is \code{False}. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 109 | \end{methoddesc} |
| 110 | |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 111 | \begin{methoddesc}[Message]{set_payload}{payload\optional{, charset}} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 112 | Set the entire message object's payload to \var{payload}. It is the |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 113 | client's responsibility to ensure the payload invariants. Optional |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 114 | \var{charset} sets the message's default character set; see |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 115 | \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} |
| 121 | Set the character set of the payload to \var{charset}, which can |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 122 | either be a \class{Charset} instance (see \refmodule{email.Charset}), a |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 123 | string naming a character set, |
| 124 | or \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 | |
| 130 | The 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} |
| 133 | and encoded properly, if needed, when generating the plain text |
| 134 | representation 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}{} |
| 142 | Return the \class{Charset} instance associated with the message's payload. |
| 143 | \versionadded{2.2.2} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 144 | \end{methoddesc} |
| 145 | |
| 146 | The following methods implement a mapping-like interface for accessing |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 147 | the message's \rfc{2822} headers. Note that there are some |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 148 | semantic differences between these methods and a normal mapping |
| 149 | (i.e. dictionary) interface. For example, in a dictionary there are |
| 150 | no duplicate keys, but here there may be duplicate message headers. Also, |
| 151 | in dictionaries there is no guaranteed order to the keys returned by |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 152 | \method{keys()}, but in a \class{Message} object, headers are always |
| 153 | returned in the order they appeared in the original message, or were |
| 154 | added to the message later. Any header deleted and then re-added are |
| 155 | always appended to the end of the header list. |
| 156 | |
| 157 | These semantic differences are intentional and are biased toward |
| 158 | maximal convenience. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 159 | |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 160 | Note that in all cases, any envelope header present in the message is |
| 161 | not included in the mapping interface. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 162 | |
| 163 | \begin{methoddesc}[Message]{__len__}{} |
| 164 | Return the total number of headers, including duplicates. |
| 165 | \end{methoddesc} |
| 166 | |
| 167 | \begin{methoddesc}[Message]{__contains__}{name} |
| 168 | Return true if the message object has a field named \var{name}. |
Andrew M. Kuchling | 43dc1fc | 2001-11-05 01:55:03 +0000 | [diff] [blame] | 169 | Matching is done case-insensitively and \var{name} should not include the |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 170 | trailing colon. Used for the \code{in} operator, |
| 171 | e.g.: |
| 172 | |
| 173 | \begin{verbatim} |
| 174 | if 'message-id' in myMessage: |
| 175 | print 'Message-ID:', myMessage['message-id'] |
| 176 | \end{verbatim} |
| 177 | \end{methoddesc} |
| 178 | |
| 179 | \begin{methoddesc}[Message]{__getitem__}{name} |
| 180 | Return the value of the named header field. \var{name} should not |
| 181 | include the colon field separator. If the header is missing, |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 182 | \code{None} is returned; a \exception{KeyError} is never raised. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 183 | |
| 184 | Note that if the named field appears more than once in the message's |
| 185 | headers, exactly which of those field values will be returned is |
| 186 | undefined. Use the \method{get_all()} method to get the values of all |
| 187 | the extant named headers. |
| 188 | \end{methoddesc} |
| 189 | |
| 190 | \begin{methoddesc}[Message]{__setitem__}{name, val} |
| 191 | Add 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 |
| 193 | fields. |
| 194 | |
| 195 | Note that this does \emph{not} overwrite or delete any existing header |
| 196 | with the same name. If you want to ensure that the new header is the |
| 197 | only one present in the message with field name |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 198 | \var{name}, delete the field first, e.g.: |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 199 | |
| 200 | \begin{verbatim} |
| 201 | del msg['subject'] |
| 202 | msg['subject'] = 'Python roolz!' |
| 203 | \end{verbatim} |
| 204 | \end{methoddesc} |
| 205 | |
| 206 | \begin{methoddesc}[Message]{__delitem__}{name} |
| 207 | Delete all occurrences of the field with name \var{name} from the |
| 208 | message's headers. No exception is raised if the named field isn't |
| 209 | present in the headers. |
| 210 | \end{methoddesc} |
| 211 | |
| 212 | \begin{methoddesc}[Message]{has_key}{name} |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 213 | Return true if the message contains a header field named \var{name}, |
| 214 | otherwise return false. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 215 | \end{methoddesc} |
| 216 | |
| 217 | \begin{methoddesc}[Message]{keys}{} |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 218 | Return a list of all the message's header field names. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 219 | \end{methoddesc} |
| 220 | |
| 221 | \begin{methoddesc}[Message]{values}{} |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 222 | Return a list of all the message's field values. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 223 | \end{methoddesc} |
| 224 | |
| 225 | \begin{methoddesc}[Message]{items}{} |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 226 | Return a list of 2-tuples containing all the message's field headers |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 227 | and values. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 228 | \end{methoddesc} |
| 229 | |
| 230 | \begin{methoddesc}[Message]{get}{name\optional{, failobj}} |
| 231 | Return the value of the named header field. This is identical to |
| 232 | \method{__getitem__()} except that optional \var{failobj} is returned |
| 233 | if the named header is missing (defaults to \code{None}). |
| 234 | \end{methoddesc} |
| 235 | |
| 236 | Here are some additional useful methods: |
| 237 | |
| 238 | \begin{methoddesc}[Message]{get_all}{name\optional{, failobj}} |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 239 | Return a list of all the values for the field named \var{name}. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 240 | If there are no such named headers in the message, \var{failobj} is |
| 241 | returned (defaults to \code{None}). |
| 242 | \end{methoddesc} |
| 243 | |
| 244 | \begin{methoddesc}[Message]{add_header}{_name, _value, **_params} |
| 245 | Extended header setting. This method is similar to |
| 246 | \method{__setitem__()} except that additional header parameters can be |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 247 | provided as keyword arguments. \var{_name} is the header field to add |
| 248 | and \var{_value} is the \emph{primary} value for the header. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 249 | |
| 250 | For each item in the keyword argument dictionary \var{_params}, the |
| 251 | key is taken as the parameter name, with underscores converted to |
| 252 | dashes (since dashes are illegal in Python identifiers). Normally, |
| 253 | the 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 | |
| 256 | Here's an example: |
| 257 | |
| 258 | \begin{verbatim} |
| 259 | msg.add_header('Content-Disposition', 'attachment', filename='bud.gif') |
| 260 | \end{verbatim} |
| 261 | |
| 262 | This will add a header that looks like |
| 263 | |
| 264 | \begin{verbatim} |
| 265 | Content-Disposition: attachment; filename="bud.gif" |
| 266 | \end{verbatim} |
| 267 | \end{methoddesc} |
| 268 | |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 269 | \begin{methoddesc}[Message]{replace_header}{_name, _value} |
| 270 | Replace a header. Replace the first header found in the message that |
| 271 | matches \var{_name}, retaining header order and field name case. If |
| 272 | no 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}{} |
| 278 | Return the message's content type. The returned string is coerced to |
| 279 | lower case of the form \mimetype{maintype/subtype}. If there was no |
| 280 | \mailheader{Content-Type} header in the message the default type as |
| 281 | given by \method{get_default_type()} will be returned. Since |
| 282 | according 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 |
| 289 | has an invalid type specification, \rfc{2045} mandates that the |
| 290 | default type be \mimetype{text/plain}. |
| 291 | |
| 292 | \versionadded{2.2.2} |
| 293 | \end{methoddesc} |
| 294 | |
| 295 | \begin{methoddesc}[Message]{get_content_maintype}{} |
| 296 | Return 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}{} |
| 304 | Return the message's sub-content type. This is the \mimetype{subtype} |
| 305 | part 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}{} |
| 311 | Return the default content type. Most messages have a default content |
| 312 | type of \mimetype{text/plain}, except for messages that are subparts |
| 313 | of \mimetype{multipart/digest} containers. Such subparts have a |
| 314 | default content type of \mimetype{message/rfc822}. |
| 315 | |
| 316 | \versionadded{2.2.2} |
| 317 | \end{methoddesc} |
| 318 | |
| 319 | \begin{methoddesc}[Message]{set_default_type}{ctype} |
| 320 | Set the default content type. \var{ctype} should either be |
| 321 | \mimetype{text/plain} or \mimetype{message/rfc822}, although this is |
| 322 | not enforced. The default content type is not stored in the |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 323 | \mailheader{Content-Type} header. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 324 | |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 325 | \versionadded{2.2.2} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 326 | \end{methoddesc} |
| 327 | |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 328 | \begin{methoddesc}[Message]{get_params}{\optional{failobj\optional{, |
| 329 | header\optional{, unquote}}}} |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 330 | Return the message's \mailheader{Content-Type} parameters, as a list. The |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 331 | elements of the returned list are 2-tuples of key/value pairs, as |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 332 | split on the \character{=} sign. The left hand side of the |
| 333 | \character{=} is the key, while the right hand side is the value. If |
| 334 | there is no \character{=} sign in the parameter the value is the empty |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 335 | string, otherwise the value is as described in \method{get_param()} and is |
| 336 | unquoted if optional \var{unquote} is \code{True} (the default). |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 337 | |
| 338 | Optional \var{failobj} is the object to return if there is no |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 339 | \mailheader{Content-Type} header. Optional \var{header} is the header to |
| 340 | search instead of \mailheader{Content-Type}. |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 341 | |
| 342 | \versionchanged[\var{unquote} argument added]{2.2.2} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 343 | \end{methoddesc} |
| 344 | |
| 345 | \begin{methoddesc}[Message]{get_param}{param\optional{, |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 346 | failobj\optional{, header\optional{, unquote}}}} |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 347 | Return 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 Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 349 | header or if there is no such parameter, then \var{failobj} is |
| 350 | returned (defaults to \code{None}). |
| 351 | |
| 352 | Optional \var{header} if given, specifies the message header to use |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 353 | instead of \mailheader{Content-Type}. |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 354 | |
| 355 | Parameter keys are always compared case insensitively. The return |
| 356 | value 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 Warsaw | 463c5a8 | 2003-08-19 04:26:59 +0000 | [diff] [blame] | 358 | the 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 |
| 361 | usually ignore \code{LANGUAGE}. |
| 362 | |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 363 | If 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 |
| 366 | from \method{get_param()}. This will return a suitably decoded Unicode string |
| 367 | whn the value is a tuple, or the original string unquoted if it isn't. For |
| 368 | example: |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 369 | |
| 370 | \begin{verbatim} |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 371 | rawparam = msg.get_param('foo') |
| 372 | param = email.Utils.collapse_rfc2231_value(rawparam) |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 373 | \end{verbatim} |
| 374 | |
| 375 | In any case, the parameter value (either the returned string, or the |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 376 | \code{VALUE} item in the 3-tuple) is always unquoted, unless |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 377 | \var{unquote} is set to \code{False}. |
| 378 | |
| 379 | \versionchanged[\var{unquote} argument added, and 3-tuple return value |
| 380 | possible]{2.2.2} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 381 | \end{methoddesc} |
| 382 | |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 383 | \begin{methoddesc}[Message]{set_param}{param, value\optional{, |
| 384 | header\optional{, requote\optional{, charset\optional{, language}}}}} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 385 | |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 386 | Set a parameter in the \mailheader{Content-Type} header. If the |
| 387 | parameter already exists in the header, its value will be replaced |
| 388 | with \var{value}. If the \mailheader{Content-Type} header as not yet |
| 389 | been defined for this message, it will be set to \mimetype{text/plain} |
| 390 | and the new parameter value will be appended as per \rfc{2045}. |
| 391 | |
| 392 | Optional \var{header} specifies an alternative header to |
| 393 | \mailheader{Content-Type}, and all parameters will be quoted as |
| 394 | necessary unless optional \var{requote} is \code{False} (the default |
| 395 | is \code{True}). |
| 396 | |
| 397 | If optional \var{charset} is specified, the parameter will be encoded |
| 398 | according to \rfc{2231}. Optional \var{language} specifies the RFC |
| 399 | 2231 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}}} |
| 407 | Remove the given parameter completely from the |
| 408 | \mailheader{Content-Type} header. The header will be re-written in |
| 409 | place without the parameter or its value. All values will be quoted |
| 410 | as necessary unless \var{requote} is \code{False} (the default is |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 411 | \code{True}). Optional \var{header} specifies an alternative to |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 412 | \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}} |
| 419 | Set the main type and subtype for the \mailheader{Content-Type} |
| 420 | header. \var{type} must be a string in the form |
| 421 | \mimetype{maintype/subtype}, otherwise a \exception{ValueError} is |
| 422 | raised. |
| 423 | |
| 424 | This method replaces the \mailheader{Content-Type} header, keeping all |
| 425 | the parameters in place. If \var{requote} is \code{False}, this |
| 426 | leaves the existing header's quoting as is, otherwise the parameters |
| 427 | will be quoted (the default). |
| 428 | |
| 429 | An alternative header can be specified in the \var{header} argument. |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 430 | When the \mailheader{Content-Type} header is set a |
| 431 | \mailheader{MIME-Version} header is also added. |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 432 | |
| 433 | \versionadded{2.2.2} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 434 | \end{methoddesc} |
| 435 | |
| 436 | \begin{methoddesc}[Message]{get_filename}{\optional{failobj}} |
| 437 | Return the value of the \code{filename} parameter of the |
Barry Warsaw | 48653af7 | 2006-01-17 05:24:25 +0000 | [diff] [blame] | 438 | \mailheader{Content-Disposition} header of the message. If the header does |
| 439 | not have a \code{filename} parameter, this method falls back to looking for |
| 440 | the \code{name} parameter. If neither is found, or the header is missing, |
| 441 | then \var{failobj} is returned. The returned string will always be unquoted |
| 442 | as per \method{Utils.unquote()}. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 443 | \end{methoddesc} |
| 444 | |
| 445 | \begin{methoddesc}[Message]{get_boundary}{\optional{failobj}} |
| 446 | Return the value of the \code{boundary} parameter of the |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 447 | \mailheader{Content-Type} header of the message, or \var{failobj} if either |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 448 | the header is missing, or has no \code{boundary} parameter. The |
| 449 | returned string will always be unquoted as per |
| 450 | \method{Utils.unquote()}. |
| 451 | \end{methoddesc} |
| 452 | |
| 453 | \begin{methoddesc}[Message]{set_boundary}{boundary} |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 454 | Set the \code{boundary} parameter of the \mailheader{Content-Type} |
| 455 | header to \var{boundary}. \method{set_boundary()} will always quote |
| 456 | \var{boundary} if necessary. A \exception{HeaderParseError} is raised |
| 457 | if the message object has no \mailheader{Content-Type} header. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 458 | |
| 459 | Note that using this method is subtly different than deleting the old |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 460 | \mailheader{Content-Type} header and adding a new one with the new boundary |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 461 | via \method{add_header()}, because \method{set_boundary()} preserves the |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 462 | order of the \mailheader{Content-Type} header in the list of headers. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 463 | However, it does \emph{not} preserve any continuation lines which may |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 464 | have been present in the original \mailheader{Content-Type} header. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 465 | \end{methoddesc} |
| 466 | |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 467 | \begin{methoddesc}[Message]{get_content_charset}{\optional{failobj}} |
| 468 | Return the \code{charset} parameter of the \mailheader{Content-Type} |
Barry Warsaw | 57ce143 | 2002-10-10 15:22:16 +0000 | [diff] [blame] | 469 | header, coerced to lower case. If there is no |
| 470 | \mailheader{Content-Type} header, or if that header has no |
| 471 | \code{charset} parameter, \var{failobj} is returned. |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 472 | |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 473 | Note that this method differs from \method{get_charset()} which |
| 474 | returns the \class{Charset} instance for the default encoding of the |
| 475 | message body. |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 476 | |
| 477 | \versionadded{2.2.2} |
| 478 | \end{methoddesc} |
| 479 | |
| 480 | \begin{methoddesc}[Message]{get_charsets}{\optional{failobj}} |
| 481 | Return a list containing the character set names in the message. If |
| 482 | the message is a \mimetype{multipart}, then the list will contain one |
| 483 | element for each subpart in the payload, otherwise, it will be a list |
| 484 | of length 1. |
| 485 | |
| 486 | Each item in the list will be a string which is the value of the |
| 487 | \code{charset} parameter in the \mailheader{Content-Type} header for the |
| 488 | represented subpart. However, if the subpart has no |
| 489 | \mailheader{Content-Type} header, no \code{charset} parameter, or is not of |
| 490 | the \mimetype{text} main MIME type, then that item in the returned list |
| 491 | will be \var{failobj}. |
| 492 | \end{methoddesc} |
| 493 | |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 494 | \begin{methoddesc}[Message]{walk}{} |
| 495 | The \method{walk()} method is an all-purpose generator which can be |
| 496 | used to iterate over all the parts and subparts of a message object |
| 497 | tree, in depth-first traversal order. You will typically use |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 498 | \method{walk()} as the iterator in a \code{for} loop; each |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 499 | iteration returns the next subpart. |
| 500 | |
Barry Warsaw | 5db478f | 2002-10-01 04:33:16 +0000 | [diff] [blame] | 501 | Here's an example that prints the MIME type of every part of a |
| 502 | multipart message structure: |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 503 | |
| 504 | \begin{verbatim} |
| 505 | >>> for part in msg.walk(): |
Edward Loper | ad51226 | 2004-09-28 02:56:45 +0000 | [diff] [blame] | 506 | ... print part.get_content_type() |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 507 | multipart/report |
| 508 | text/plain |
| 509 | message/delivery-status |
| 510 | text/plain |
| 511 | text/plain |
| 512 | message/rfc822 |
| 513 | \end{verbatim} |
| 514 | \end{methoddesc} |
| 515 | |
| 516 | \class{Message} objects can also optionally contain two instance |
| 517 | attributes, which can be used when generating the plain text of a MIME |
| 518 | message. |
| 519 | |
| 520 | \begin{datadesc}{preamble} |
| 521 | The format of a MIME document allows for some text between the blank |
| 522 | line following the headers, and the first multipart boundary string. |
| 523 | Normally, this text is never visible in a MIME-aware mail reader |
| 524 | because it falls outside the standard MIME armor. However, when |
| 525 | viewing the raw text of the message, or when viewing the message in a |
| 526 | non-MIME aware reader, this text can become visible. |
| 527 | |
| 528 | The \var{preamble} attribute contains this leading extra-armor text |
| 529 | for MIME documents. When the \class{Parser} discovers some text after |
| 530 | the headers but before the first boundary string, it assigns this text |
| 531 | to the message's \var{preamble} attribute. When the \class{Generator} |
| 532 | is writing out the plain text representation of a MIME message, and it |
| 533 | finds the message has a \var{preamble} attribute, it will write this |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 534 | text in the area between the headers and the first boundary. See |
| 535 | \refmodule{email.Parser} and \refmodule{email.Generator} for details. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 536 | |
| 537 | Note that if the message object has no preamble, the |
| 538 | \var{preamble} attribute will be \code{None}. |
| 539 | \end{datadesc} |
| 540 | |
| 541 | \begin{datadesc}{epilogue} |
| 542 | The \var{epilogue} attribute acts the same way as the \var{preamble} |
| 543 | attribute, except that it contains text that appears between the last |
| 544 | boundary and the end of the message. |
Barry Warsaw | e736d93 | 2001-10-19 04:34:42 +0000 | [diff] [blame] | 545 | |
| 546 | One note: when generating the flat text for a \mimetype{multipart} |
| 547 | message that has no \var{epilogue} (using the standard |
| 548 | \class{Generator} class), no newline is added after the closing |
| 549 | boundary line. If the message object has an \var{epilogue} and its |
| 550 | value does not start with a newline, a newline is printed after the |
| 551 | closing boundary. This seems a little clumsy, but it makes the most |
| 552 | practical sense. The upshot is that if you want to ensure that a |
| 553 | newline get printed after your closing \mimetype{multipart} boundary, |
| 554 | set the \var{epilogue} to the empty string. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 555 | \end{datadesc} |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 556 | |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 557 | \begin{datadesc}{defects} |
| 558 | The \var{defects} attribute contains a list of all the problems found when |
| 559 | parsing this message. See \refmodule{email.Errors} for a detailed description |
| 560 | of the possible parsing defects. |
| 561 | |
| 562 | \versionadded{2.4} |
| 563 | \end{datadesc} |
| 564 | |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 565 | \subsubsection{Deprecated methods} |
| 566 | |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 567 | \versionchanged[The \method{add_payload()} method was removed; use the |
| 568 | \method{attach()} method instead]{2.4} |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 569 | |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 570 | The following methods are deprecated. They are documented here for |
| 571 | completeness. |
Barry Warsaw | 5b9da89 | 2002-10-01 01:05:52 +0000 | [diff] [blame] | 572 | |
| 573 | \begin{methoddesc}[Message]{get_type}{\optional{failobj}} |
| 574 | Return the message's content type, as a string of the form |
| 575 | \mimetype{maintype/subtype} as taken from the |
| 576 | \mailheader{Content-Type} header. |
| 577 | The returned string is coerced to lowercase. |
| 578 | |
| 579 | If there is no \mailheader{Content-Type} header in the message, |
| 580 | \var{failobj} is returned (defaults to \code{None}). |
| 581 | |
| 582 | \deprecated{2.2.2}{Use the \method{get_content_type()} method instead.} |
| 583 | \end{methoddesc} |
| 584 | |
| 585 | \begin{methoddesc}[Message]{get_main_type}{\optional{failobj}} |
| 586 | Return the message's \emph{main} content type. This essentially returns the |
| 587 | \var{maintype} part of the string returned by \method{get_type()}, with the |
| 588 | same semantics for \var{failobj}. |
| 589 | |
| 590 | \deprecated{2.2.2}{Use the \method{get_content_maintype()} method instead.} |
| 591 | \end{methoddesc} |
| 592 | |
| 593 | \begin{methoddesc}[Message]{get_subtype}{\optional{failobj}} |
| 594 | Return the message's sub-content type. This essentially returns the |
| 595 | \var{subtype} part of the string returned by \method{get_type()}, with the |
| 596 | same semantics for \var{failobj}. |
| 597 | |
| 598 | \deprecated{2.2.2}{Use the \method{get_content_subtype()} method instead.} |
| 599 | \end{methoddesc} |
| 600 | |