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