blob: 338de75734fb0e2a5a927eb41b71b71b0c5a3c44 [file] [log] [blame]
Barry Warsaw5e634632001-09-26 05:23:47 +00001% Copyright (C) 2001 Python Software Foundation
2% Author: barry@zope.com (Barry Warsaw)
3
Fred Drakea6a885b2001-09-26 16:52:18 +00004\section{\module{email} ---
Barry Warsaw5e634632001-09-26 05:23:47 +00005 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 Drake90e68782001-09-27 20:09:39 +000011\sectionauthor{Barry A. Warsaw}{barry@zope.com}
Barry Warsaw5e634632001-09-26 05:23:47 +000012
13\versionadded{2.2}
14
15The \module{email} package is a library for managing email messages,
16including MIME and other \rfc{2822}-based message documents. It
17subsumes most of the functionality in several older standard modules
Barry Warsawc5f8fe32001-09-26 22:21:52 +000018such as \refmodule{rfc822}, \refmodule{mimetools},
19\refmodule{multifile}, and other non-standard packages such as
20\module{mimecntl}.
Barry Warsaw5e634632001-09-26 05:23:47 +000021
22The primary distinguishing feature of the \module{email} package is
23that it splits the parsing and generating of email messages from the
24internal \emph{object model} representation of email. Applications
25using the \module{email} package deal primarily with objects; you can
26add sub-objects to messages, remove sub-objects from messages,
27completely re-arrange the contents, etc. There is a separate parser
28and a separate generator which handles the transformation from flat
29text to the object module, and then back to flat text again. There
30are also handy subclasses for some common MIME object types, and a few
31miscellaneous utilities that help with such common tasks as extracting
32and parsing message field values, creating RFC-compliant dates, etc.
33
34The following sections describe the functionality of the
35\module{email} package. The ordering follows a progression that
36should be common in applications: an email message is read as flat
37text from a file or other source, the text is parsed to produce an
38object model representation of the email message, this model is
39manipulated, and finally the model is rendered back into
40flat text.
41
42It is perfectly feasible to create the object model out of whole cloth
Barry Warsawc5f8fe32001-09-26 22:21:52 +000043--- i.e. completely from scratch. From there, a similar progression
44can be taken as above.
Barry Warsaw5e634632001-09-26 05:23:47 +000045
46Also included are detailed specifications of all the classes and
47modules that the \module{email} package provides, the exception
48classes you might encounter while using the \module{email} package,
49some auxiliary utilities, and a few examples. For users of the older
50\module{mimelib} package, from which the \module{email} package is
51descendent, a section on differences and porting is provided.
52
53\subsection{Representing an email message}
Barry Warsawc5f8fe32001-09-26 22:21:52 +000054\input{emailmessage}
Barry Warsaw5e634632001-09-26 05:23:47 +000055
56\subsection{Parsing email messages}
Barry Warsawc5f8fe32001-09-26 22:21:52 +000057\input{emailparser}
Barry Warsaw5e634632001-09-26 05:23:47 +000058
59\subsection{Generating MIME documents}
Barry Warsawc5f8fe32001-09-26 22:21:52 +000060\input{emailgenerator}
Barry Warsaw5e634632001-09-26 05:23:47 +000061
62\subsection{Creating email and MIME objects from scratch}
63
64Ordinarily, you get a message object tree by passing some text to a
65parser, which parses the text and returns the root of the message
66object tree. However you can also build a complete object tree from
67scratch, or even individual \class{Message} objects by hand. In fact,
68you can also take an existing tree and add new \class{Message}
69objects, move them around, etc. This makes a very convenient
70interface for slicing-and-dicing MIME messages.
71
72You can create a new object tree by creating \class{Message}
73instances, adding payloads and all the appropriate headers manually.
74For MIME messages though, the \module{email} package provides some
75convenient classes to make things easier. Each of these classes
76should be imported from a module with the same name as the class, from
77within the \module{email} package. E.g.:
78
79\begin{verbatim}
80import email.MIMEImage.MIMEImage
81\end{verbatim}
82
83or
84
85\begin{verbatim}
86from email.MIMEText import MIMEText
87\end{verbatim}
88
89Here are the classes:
90
91\begin{classdesc}{MIMEBase}{_maintype, _subtype, **_params}
92This is the base class for all the MIME-specific subclasses of
93\class{Message}. Ordinarily you won't create instances specifically
94of \class{MIMEBase}, although you could. \class{MIMEBase} is provided
95primarily as a convenient base class for more specific MIME-aware
96subclasses.
97
Barry Warsawc5f8fe32001-09-26 22:21:52 +000098\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 Warsaw5e634632001-09-26 05:23:47 +0000102key/value dictionary and is passed directly to
103\method{Message.add_header()}.
104
Fred Drakea6a885b2001-09-26 16:52:18 +0000105The \class{MIMEBase} class always adds a \mailheader{Content-Type} header
Barry Warsaw5e634632001-09-26 05:23:47 +0000106(based on \var{_maintype}, \var{_subtype}, and \var{_params}), and a
Fred Drakea6a885b2001-09-26 16:52:18 +0000107\mailheader{MIME-Version} header (always set to \code{1.0}).
Barry Warsaw5e634632001-09-26 05:23:47 +0000108\end{classdesc}
109
110\begin{classdesc}{MIMEImage}{_imagedata\optional{, _subtype\optional{,
111 _encoder\optional{, **_params}}}}
112
113A subclass of \class{MIMEBase}, the \class{MIMEImage} class is used to
Fred Drakea6a885b2001-09-26 16:52:18 +0000114create MIME message objects of major type \mimetype{image}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000115\var{_imagedata} is a string containing the raw image data. If this
116data can be decoded by the standard Python module \refmodule{imghdr},
117then the subtype will be automatically included in the
Fred Drakea6a885b2001-09-26 16:52:18 +0000118\mailheader{Content-Type} header. Otherwise you can explicitly specify the
Barry Warsaw5e634632001-09-26 05:23:47 +0000119image subtype via the \var{_subtype} parameter. If the minor type could
Fred Drakea6a885b2001-09-26 16:52:18 +0000120not be guessed and \var{_subtype} was not given, then \exception{TypeError}
Barry Warsaw5e634632001-09-26 05:23:47 +0000121is raised.
122
123Optional \var{_encoder} is a callable (i.e. function) which will
124perform the actual encoding of the image data for transport. This
125callable takes one argument, which is the \class{MIMEImage} instance.
126It should use \method{get_payload()} and \method{set_payload()} to
127change the payload to encoded form. It should also add any
Fred Drakea6a885b2001-09-26 16:52:18 +0000128\mailheader{Content-Transfer-Encoding} or other headers to the message
Barry Warsaw5e634632001-09-26 05:23:47 +0000129object 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}
133constructor.
134\end{classdesc}
135
136\begin{classdesc}{MIMEText}{_text\optional{, _subtype\optional{,
137 _charset\optional{, _encoder}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000138
Barry Warsaw5e634632001-09-26 05:23:47 +0000139A subclass of \class{MIMEBase}, the \class{MIMEText} class is used to
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000140create MIME objects of major type \mimetype{text}. \var{_text} is the
141string for the payload. \var{_subtype} is the minor type and defaults
142to \mimetype{plain}. \var{_charset} is the character set of the text and is
Barry Warsaw5e634632001-09-26 05:23:47 +0000143passed as a parameter to the \class{MIMEBase} constructor; it defaults
144to \code{us-ascii}. No guessing or encoding is performed on the text
145data, but a newline is appended to \var{_text} if it doesn't already
146end with a newline.
147
148The \var{_encoding} argument is as with the \class{MIMEImage} class
149constructor, except that the default encoding for \class{MIMEText}
150objects is one that doesn't actually modify the payload, but does set
Fred Drakea6a885b2001-09-26 16:52:18 +0000151the \mailheader{Content-Transfer-Encoding} header to \code{7bit} or
Barry Warsaw5e634632001-09-26 05:23:47 +0000152\code{8bit} as appropriate.
153\end{classdesc}
154
155\begin{classdesc}{MIMEMessage}{_msg\optional{, _subtype}}
156A subclass of \class{MIMEBase}, the \class{MIMEMessage} class is used to
Fred Drakea6a885b2001-09-26 16:52:18 +0000157create MIME objects of main type \mimetype{message}. \var{_msg} is used as
Barry Warsaw5e634632001-09-26 05:23:47 +0000158the payload, and must be an instance of class \class{Message} (or a
159subclass thereof), otherwise a \exception{TypeError} is raised.
160
161Optional \var{_subtype} sets the subtype of the message; it defaults
Fred Drakea6a885b2001-09-26 16:52:18 +0000162to \mimetype{rfc822}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000163\end{classdesc}
164
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000165\subsection{Encoders}
166\input{emailencoders}
Barry Warsaw5e634632001-09-26 05:23:47 +0000167
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000168\subsection{Exception classes}
169\input{emailexc}
Barry Warsaw5e634632001-09-26 05:23:47 +0000170
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000171\subsection{Miscellaneous utilities}
172\input{emailutil}
Barry Warsaw5e634632001-09-26 05:23:47 +0000173
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000174\subsection{Iterators}
175\input{emailiter}
Barry Warsaw5e634632001-09-26 05:23:47 +0000176
177\subsection{Differences from \module{mimelib}}
178
179The \module{email} package was originally prototyped as a separate
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000180library called
181\ulink{\module{mimelib}}{http://mimelib.sf.net/}.
182Changes have been made so that
Barry Warsaw5e634632001-09-26 05:23:47 +0000183method names are more consistent, and some methods or modules have
184either been added or removed. The semantics of some of the methods
185have also changed. For the most part, any functionality available in
Fred Drake90e68782001-09-27 20:09:39 +0000186\module{mimelib} is still available in the \refmodule{email} package,
Barry Warsaw5e634632001-09-26 05:23:47 +0000187albeit often in a different way.
188
189Here is a brief description of the differences between the
Fred Drake90e68782001-09-27 20:09:39 +0000190\module{mimelib} and the \refmodule{email} packages, along with hints on
Barry Warsaw5e634632001-09-26 05:23:47 +0000191how to port your applications.
192
193Of course, the most visible difference between the two packages is
Fred Drake90e68782001-09-27 20:09:39 +0000194that the package name has been changed to \refmodule{email}. In
Barry Warsaw5e634632001-09-26 05:23:47 +0000195addition, 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
204The \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 Warsawc5f8fe32001-09-26 22:21:52 +0000223 the key/value pairs of the parameters, split on the \character{=}
Barry Warsaw5e634632001-09-26 05:23:47 +0000224 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
247The \class{Parser} class has no differences in its public interface.
248It does have some additional smarts to recognize
Fred Drakea6a885b2001-09-26 16:52:18 +0000249\mimetype{message/delivery-status} type messages, which it represents as
Barry Warsaw5e634632001-09-26 05:23:47 +0000250a \class{Message} instance containing separate \class{Message}
251subparts for each header block in the delivery status
252notification\footnote{Delivery Status Notifications (DSN) are defined
Fred Drake90e68782001-09-27 20:09:39 +0000253in \rfc{1894}.}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000254
255The \class{Generator} class has no differences in its public
256interface. There is a new class in the \refmodule{email.Generator}
257module though, called \class{DecodedGenerator} which provides most of
258the functionality previously available in the
259\method{Message.getpayloadastext()} method.
260
261The 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 Drakea6a885b2001-09-26 16:52:18 +0000280 MIME message with main type \mimetype{message}. It takes an
Barry Warsaw5e634632001-09-26 05:23:47 +0000281 optional argument \var{_subtype} which is used to set the MIME
Fred Drakea6a885b2001-09-26 16:52:18 +0000282 subtype. \var{_subtype} defaults to \mimetype{rfc822}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000283\end{itemize}
284
285\module{mimelib} provided some utility functions in its
286\module{address} and \module{date} modules. All of these functions
287have been moved to the \refmodule{email.Utils} module.
288
289The \code{MsgReader} class/module has been removed. Its functionality
290is most closely supported in the \function{body_line_iterator()}
291function in the \refmodule{email.Iterators} module.
292
293\subsection{Examples}
294
295Coming soon...