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