Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 1 | % Copyright (C) 2001 Python Software Foundation |
| 2 | % Author: barry@zope.com (Barry Warsaw) |
| 3 | |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 4 | \section{\module{email} --- |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 5 | An email and MIME handling package} |
| 6 | |
| 7 | \declaremodule{standard}{email} |
| 8 | \modulesynopsis{Package supporting the parsing, manipulating, and |
| 9 | generating email messages, including MIME documents.} |
| 10 | \moduleauthor{Barry A. Warsaw}{barry@zope.com} |
Fred Drake | 90e6878 | 2001-09-27 20:09:39 +0000 | [diff] [blame] | 11 | \sectionauthor{Barry A. Warsaw}{barry@zope.com} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 12 | |
| 13 | \versionadded{2.2} |
| 14 | |
| 15 | The \module{email} package is a library for managing email messages, |
| 16 | including MIME and other \rfc{2822}-based message documents. It |
| 17 | subsumes most of the functionality in several older standard modules |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 18 | such as \refmodule{rfc822}, \refmodule{mimetools}, |
| 19 | \refmodule{multifile}, and other non-standard packages such as |
| 20 | \module{mimecntl}. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 21 | |
| 22 | The primary distinguishing feature of the \module{email} package is |
| 23 | that it splits the parsing and generating of email messages from the |
| 24 | internal \emph{object model} representation of email. Applications |
| 25 | using the \module{email} package deal primarily with objects; you can |
| 26 | add sub-objects to messages, remove sub-objects from messages, |
| 27 | completely re-arrange the contents, etc. There is a separate parser |
| 28 | and a separate generator which handles the transformation from flat |
| 29 | text to the object module, and then back to flat text again. There |
| 30 | are also handy subclasses for some common MIME object types, and a few |
| 31 | miscellaneous utilities that help with such common tasks as extracting |
| 32 | and parsing message field values, creating RFC-compliant dates, etc. |
| 33 | |
| 34 | The following sections describe the functionality of the |
| 35 | \module{email} package. The ordering follows a progression that |
| 36 | should be common in applications: an email message is read as flat |
| 37 | text from a file or other source, the text is parsed to produce an |
| 38 | object model representation of the email message, this model is |
| 39 | manipulated, and finally the model is rendered back into |
| 40 | flat text. |
| 41 | |
| 42 | It is perfectly feasible to create the object model out of whole cloth |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 43 | --- i.e. completely from scratch. From there, a similar progression |
| 44 | can be taken as above. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 45 | |
| 46 | Also included are detailed specifications of all the classes and |
| 47 | modules that the \module{email} package provides, the exception |
| 48 | classes you might encounter while using the \module{email} package, |
| 49 | some auxiliary utilities, and a few examples. For users of the older |
| 50 | \module{mimelib} package, from which the \module{email} package is |
| 51 | descendent, a section on differences and porting is provided. |
| 52 | |
| 53 | \subsection{Representing an email message} |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 54 | \input{emailmessage} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 55 | |
| 56 | \subsection{Parsing email messages} |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 57 | \input{emailparser} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 58 | |
| 59 | \subsection{Generating MIME documents} |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 60 | \input{emailgenerator} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 61 | |
| 62 | \subsection{Creating email and MIME objects from scratch} |
| 63 | |
| 64 | Ordinarily, you get a message object tree by passing some text to a |
| 65 | parser, which parses the text and returns the root of the message |
| 66 | object tree. However you can also build a complete object tree from |
| 67 | scratch, or even individual \class{Message} objects by hand. In fact, |
| 68 | you can also take an existing tree and add new \class{Message} |
| 69 | objects, move them around, etc. This makes a very convenient |
| 70 | interface for slicing-and-dicing MIME messages. |
| 71 | |
| 72 | You can create a new object tree by creating \class{Message} |
| 73 | instances, adding payloads and all the appropriate headers manually. |
| 74 | For MIME messages though, the \module{email} package provides some |
| 75 | convenient classes to make things easier. Each of these classes |
| 76 | should be imported from a module with the same name as the class, from |
| 77 | within the \module{email} package. E.g.: |
| 78 | |
| 79 | \begin{verbatim} |
| 80 | import email.MIMEImage.MIMEImage |
| 81 | \end{verbatim} |
| 82 | |
| 83 | or |
| 84 | |
| 85 | \begin{verbatim} |
| 86 | from email.MIMEText import MIMEText |
| 87 | \end{verbatim} |
| 88 | |
| 89 | Here are the classes: |
| 90 | |
| 91 | \begin{classdesc}{MIMEBase}{_maintype, _subtype, **_params} |
| 92 | This is the base class for all the MIME-specific subclasses of |
| 93 | \class{Message}. Ordinarily you won't create instances specifically |
| 94 | of \class{MIMEBase}, although you could. \class{MIMEBase} is provided |
| 95 | primarily as a convenient base class for more specific MIME-aware |
| 96 | subclasses. |
| 97 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 98 | \var{_maintype} is the \mailheader{Content-Type} major type |
| 99 | (e.g. \mimetype{text} or \mimetype{image}), and \var{_subtype} is the |
| 100 | \mailheader{Content-Type} minor type |
| 101 | (e.g. \mimetype{plain} or \mimetype{gif}). \var{_params} is a parameter |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 102 | key/value dictionary and is passed directly to |
| 103 | \method{Message.add_header()}. |
| 104 | |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 105 | The \class{MIMEBase} class always adds a \mailheader{Content-Type} header |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 106 | (based on \var{_maintype}, \var{_subtype}, and \var{_params}), and a |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 107 | \mailheader{MIME-Version} header (always set to \code{1.0}). |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 108 | \end{classdesc} |
| 109 | |
| 110 | \begin{classdesc}{MIMEImage}{_imagedata\optional{, _subtype\optional{, |
| 111 | _encoder\optional{, **_params}}}} |
| 112 | |
| 113 | A subclass of \class{MIMEBase}, the \class{MIMEImage} class is used to |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 114 | create MIME message objects of major type \mimetype{image}. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 115 | \var{_imagedata} is a string containing the raw image data. If this |
| 116 | data can be decoded by the standard Python module \refmodule{imghdr}, |
| 117 | then the subtype will be automatically included in the |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 118 | \mailheader{Content-Type} header. Otherwise you can explicitly specify the |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 119 | image subtype via the \var{_subtype} parameter. If the minor type could |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 120 | not be guessed and \var{_subtype} was not given, then \exception{TypeError} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 121 | is raised. |
| 122 | |
| 123 | Optional \var{_encoder} is a callable (i.e. function) which will |
| 124 | perform the actual encoding of the image data for transport. This |
| 125 | callable takes one argument, which is the \class{MIMEImage} instance. |
| 126 | It should use \method{get_payload()} and \method{set_payload()} to |
| 127 | change the payload to encoded form. It should also add any |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 128 | \mailheader{Content-Transfer-Encoding} or other headers to the message |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 129 | object as necessary. The default encoding is \emph{Base64}. See the |
| 130 | \refmodule{email.Encoders} module for a list of the built-in encoders. |
| 131 | |
| 132 | \var{_params} are passed straight through to the \class{MIMEBase} |
| 133 | constructor. |
| 134 | \end{classdesc} |
| 135 | |
| 136 | \begin{classdesc}{MIMEText}{_text\optional{, _subtype\optional{, |
| 137 | _charset\optional{, _encoder}}}} |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 138 | |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 139 | A subclass of \class{MIMEBase}, the \class{MIMEText} class is used to |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 140 | create MIME objects of major type \mimetype{text}. \var{_text} is the |
| 141 | string for the payload. \var{_subtype} is the minor type and defaults |
| 142 | to \mimetype{plain}. \var{_charset} is the character set of the text and is |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 143 | passed as a parameter to the \class{MIMEBase} constructor; it defaults |
| 144 | to \code{us-ascii}. No guessing or encoding is performed on the text |
| 145 | data, but a newline is appended to \var{_text} if it doesn't already |
| 146 | end with a newline. |
| 147 | |
| 148 | The \var{_encoding} argument is as with the \class{MIMEImage} class |
| 149 | constructor, except that the default encoding for \class{MIMEText} |
| 150 | objects is one that doesn't actually modify the payload, but does set |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 151 | the \mailheader{Content-Transfer-Encoding} header to \code{7bit} or |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 152 | \code{8bit} as appropriate. |
| 153 | \end{classdesc} |
| 154 | |
| 155 | \begin{classdesc}{MIMEMessage}{_msg\optional{, _subtype}} |
| 156 | A subclass of \class{MIMEBase}, the \class{MIMEMessage} class is used to |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 157 | create MIME objects of main type \mimetype{message}. \var{_msg} is used as |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 158 | the payload, and must be an instance of class \class{Message} (or a |
| 159 | subclass thereof), otherwise a \exception{TypeError} is raised. |
| 160 | |
| 161 | Optional \var{_subtype} sets the subtype of the message; it defaults |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 162 | to \mimetype{rfc822}. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 163 | \end{classdesc} |
| 164 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 165 | \subsection{Encoders} |
| 166 | \input{emailencoders} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 167 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 168 | \subsection{Exception classes} |
| 169 | \input{emailexc} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 170 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 171 | \subsection{Miscellaneous utilities} |
| 172 | \input{emailutil} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 173 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 174 | \subsection{Iterators} |
| 175 | \input{emailiter} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 176 | |
| 177 | \subsection{Differences from \module{mimelib}} |
| 178 | |
| 179 | The \module{email} package was originally prototyped as a separate |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 180 | library called |
| 181 | \ulink{\module{mimelib}}{http://mimelib.sf.net/}. |
| 182 | Changes have been made so that |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 183 | method names are more consistent, and some methods or modules have |
| 184 | either been added or removed. The semantics of some of the methods |
| 185 | have also changed. For the most part, any functionality available in |
Fred Drake | 90e6878 | 2001-09-27 20:09:39 +0000 | [diff] [blame] | 186 | \module{mimelib} is still available in the \refmodule{email} package, |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 187 | albeit often in a different way. |
| 188 | |
| 189 | Here is a brief description of the differences between the |
Fred Drake | 90e6878 | 2001-09-27 20:09:39 +0000 | [diff] [blame] | 190 | \module{mimelib} and the \refmodule{email} packages, along with hints on |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 191 | how to port your applications. |
| 192 | |
| 193 | Of course, the most visible difference between the two packages is |
Fred Drake | 90e6878 | 2001-09-27 20:09:39 +0000 | [diff] [blame] | 194 | that the package name has been changed to \refmodule{email}. In |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 195 | addition, the top-level package has the following differences: |
| 196 | |
| 197 | \begin{itemize} |
| 198 | \item \function{messageFromString()} has been renamed to |
| 199 | \function{message_from_string()}. |
| 200 | \item \function{messageFromFile()} has been renamed to |
| 201 | \function{message_from_file()}. |
| 202 | \end{itemize} |
| 203 | |
| 204 | The \class{Message} class has the following differences: |
| 205 | |
| 206 | \begin{itemize} |
| 207 | \item The method \method{asString()} was renamed to \method{as_string()}. |
| 208 | \item The method \method{ismultipart()} was renamed to |
| 209 | \method{is_multipart()}. |
| 210 | \item The \method{get_payload()} method has grown a \var{decode} |
| 211 | optional argument. |
| 212 | \item The method \method{getall()} was renamed to \method{get_all()}. |
| 213 | \item The method \method{addheader()} was renamed to \method{add_header()}. |
| 214 | \item The method \method{gettype()} was renamed to \method{get_type()}. |
| 215 | \item The method\method{getmaintype()} was renamed to |
| 216 | \method{get_main_type()}. |
| 217 | \item The method \method{getsubtype()} was renamed to |
| 218 | \method{get_subtype()}. |
| 219 | \item The method \method{getparams()} was renamed to |
| 220 | \method{get_params()}. |
| 221 | Also, whereas \method{getparams()} returned a list of strings, |
| 222 | \method{get_params()} returns a list of 2-tuples, effectively |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 223 | the key/value pairs of the parameters, split on the \character{=} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 224 | sign. |
| 225 | \item The method \method{getparam()} was renamed to \method{get_param()}. |
| 226 | \item The method \method{getcharsets()} was renamed to |
| 227 | \method{get_charsets()}. |
| 228 | \item The method \method{getfilename()} was renamed to |
| 229 | \method{get_filename()}. |
| 230 | \item The method \method{getboundary()} was renamed to |
| 231 | \method{get_boundary()}. |
| 232 | \item The method \method{setboundary()} was renamed to |
| 233 | \method{set_boundary()}. |
| 234 | \item The method \method{getdecodedpayload()} was removed. To get |
| 235 | similar functionality, pass the value 1 to the \var{decode} flag |
| 236 | of the {get_payload()} method. |
| 237 | \item The method \method{getpayloadastext()} was removed. Similar |
| 238 | functionality |
| 239 | is supported by the \class{DecodedGenerator} class in the |
| 240 | \refmodule{email.Generator} module. |
| 241 | \item The method \method{getbodyastext()} was removed. You can get |
| 242 | similar functionality by creating an iterator with |
| 243 | \function{typed_subpart_iterator()} in the |
| 244 | \refmodule{email.Iterators} module. |
| 245 | \end{itemize} |
| 246 | |
| 247 | The \class{Parser} class has no differences in its public interface. |
| 248 | It does have some additional smarts to recognize |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 249 | \mimetype{message/delivery-status} type messages, which it represents as |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 250 | a \class{Message} instance containing separate \class{Message} |
| 251 | subparts for each header block in the delivery status |
| 252 | notification\footnote{Delivery Status Notifications (DSN) are defined |
Fred Drake | 90e6878 | 2001-09-27 20:09:39 +0000 | [diff] [blame] | 253 | in \rfc{1894}.}. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 254 | |
| 255 | The \class{Generator} class has no differences in its public |
| 256 | interface. There is a new class in the \refmodule{email.Generator} |
| 257 | module though, called \class{DecodedGenerator} which provides most of |
| 258 | the functionality previously available in the |
| 259 | \method{Message.getpayloadastext()} method. |
| 260 | |
| 261 | The following modules and classes have been changed: |
| 262 | |
| 263 | \begin{itemize} |
| 264 | \item The \class{MIMEBase} class constructor arguments \var{_major} |
| 265 | and \var{_minor} have changed to \var{_maintype} and |
| 266 | \var{_subtype} respectively. |
| 267 | \item The \code{Image} class/module has been renamed to |
| 268 | \code{MIMEImage}. The \var{_minor} argument has been renamed to |
| 269 | \var{_subtype}. |
| 270 | \item The \code{Text} class/module has been renamed to |
| 271 | \code{MIMEText}. The \var{_minor} argument has been renamed to |
| 272 | \var{_subtype}. |
| 273 | \item The \code{MessageRFC822} class/module has been renamed to |
| 274 | \code{MIMEMessage}. Note that an earlier version of |
| 275 | \module{mimelib} called this class/module \code{RFC822}, but |
| 276 | that clashed with the Python standard library module |
| 277 | \refmodule{rfc822} on some case-insensitive file systems. |
| 278 | |
| 279 | Also, the \class{MIMEMessage} class now represents any kind of |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 280 | MIME message with main type \mimetype{message}. It takes an |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 281 | optional argument \var{_subtype} which is used to set the MIME |
Fred Drake | a6a885b | 2001-09-26 16:52:18 +0000 | [diff] [blame] | 282 | subtype. \var{_subtype} defaults to \mimetype{rfc822}. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 283 | \end{itemize} |
| 284 | |
| 285 | \module{mimelib} provided some utility functions in its |
| 286 | \module{address} and \module{date} modules. All of these functions |
| 287 | have been moved to the \refmodule{email.Utils} module. |
| 288 | |
| 289 | The \code{MsgReader} class/module has been removed. Its functionality |
| 290 | is most closely supported in the \function{body_line_iterator()} |
| 291 | function in the \refmodule{email.Iterators} module. |
| 292 | |
| 293 | \subsection{Examples} |
| 294 | |
| 295 | Coming soon... |