blob: a6ad567df0f6c84777956a65cefc41e05a401bdc [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
Barry Warsawa55d1322001-10-09 19:14:17 +0000110\begin{classdesc}{MIMEAudio}{_audiodata\optional{, _subtype\optional{,
111 _encoder\optional{, **_params}}}}
112
113A subclass of \class{MIMEBase}, the \class{MIMEAudio} class is used to
114create MIME message objects of major type \mimetype{audio}.
Barry Warsawdca93982001-10-09 19:37:51 +0000115\var{_audiodata} is a string containing the raw audio data. If this
Barry Warsawa55d1322001-10-09 19:14:17 +0000116data can be decoded by the standard Python module \refmodule{sndhdr},
117then the subtype will be automatically included in the
118\mailheader{Content-Type} header. Otherwise you can explicitly specify the
119audio subtype via the \var{_subtype} parameter. If the minor type could
120not be guessed and \var{_subtype} was not given, then \exception{TypeError}
121is raised.
122
123Optional \var{_encoder} is a callable (i.e. function) which will
124perform the actual encoding of the audio data for transport. This
125callable takes one argument, which is the \class{MIMEAudio} instance.
126It should use \method{get_payload()} and \method{set_payload()} to
127change the payload to encoded form. It should also add any
128\mailheader{Content-Transfer-Encoding} or other headers to the message
129object 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
Barry Warsaw5e634632001-09-26 05:23:47 +0000136\begin{classdesc}{MIMEImage}{_imagedata\optional{, _subtype\optional{,
137 _encoder\optional{, **_params}}}}
138
139A subclass of \class{MIMEBase}, the \class{MIMEImage} class is used to
Fred Drakea6a885b2001-09-26 16:52:18 +0000140create MIME message objects of major type \mimetype{image}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000141\var{_imagedata} is a string containing the raw image data. If this
142data can be decoded by the standard Python module \refmodule{imghdr},
143then the subtype will be automatically included in the
Fred Drakea6a885b2001-09-26 16:52:18 +0000144\mailheader{Content-Type} header. Otherwise you can explicitly specify the
Barry Warsaw5e634632001-09-26 05:23:47 +0000145image subtype via the \var{_subtype} parameter. If the minor type could
Fred Drakea6a885b2001-09-26 16:52:18 +0000146not be guessed and \var{_subtype} was not given, then \exception{TypeError}
Barry Warsaw5e634632001-09-26 05:23:47 +0000147is raised.
148
149Optional \var{_encoder} is a callable (i.e. function) which will
150perform the actual encoding of the image data for transport. This
151callable takes one argument, which is the \class{MIMEImage} instance.
152It should use \method{get_payload()} and \method{set_payload()} to
153change the payload to encoded form. It should also add any
Fred Drakea6a885b2001-09-26 16:52:18 +0000154\mailheader{Content-Transfer-Encoding} or other headers to the message
Barry Warsaw5e634632001-09-26 05:23:47 +0000155object as necessary. The default encoding is \emph{Base64}. See the
156\refmodule{email.Encoders} module for a list of the built-in encoders.
157
158\var{_params} are passed straight through to the \class{MIMEBase}
159constructor.
160\end{classdesc}
161
162\begin{classdesc}{MIMEText}{_text\optional{, _subtype\optional{,
163 _charset\optional{, _encoder}}}}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000164
Barry Warsaw5e634632001-09-26 05:23:47 +0000165A subclass of \class{MIMEBase}, the \class{MIMEText} class is used to
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000166create MIME objects of major type \mimetype{text}. \var{_text} is the
167string for the payload. \var{_subtype} is the minor type and defaults
168to \mimetype{plain}. \var{_charset} is the character set of the text and is
Barry Warsaw5e634632001-09-26 05:23:47 +0000169passed as a parameter to the \class{MIMEBase} constructor; it defaults
170to \code{us-ascii}. No guessing or encoding is performed on the text
171data, but a newline is appended to \var{_text} if it doesn't already
172end with a newline.
173
174The \var{_encoding} argument is as with the \class{MIMEImage} class
175constructor, except that the default encoding for \class{MIMEText}
176objects is one that doesn't actually modify the payload, but does set
Fred Drakea6a885b2001-09-26 16:52:18 +0000177the \mailheader{Content-Transfer-Encoding} header to \code{7bit} or
Barry Warsaw5e634632001-09-26 05:23:47 +0000178\code{8bit} as appropriate.
179\end{classdesc}
180
181\begin{classdesc}{MIMEMessage}{_msg\optional{, _subtype}}
182A subclass of \class{MIMEBase}, the \class{MIMEMessage} class is used to
Fred Drakea6a885b2001-09-26 16:52:18 +0000183create MIME objects of main type \mimetype{message}. \var{_msg} is used as
Barry Warsaw5e634632001-09-26 05:23:47 +0000184the payload, and must be an instance of class \class{Message} (or a
185subclass thereof), otherwise a \exception{TypeError} is raised.
186
187Optional \var{_subtype} sets the subtype of the message; it defaults
Fred Drakea6a885b2001-09-26 16:52:18 +0000188to \mimetype{rfc822}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000189\end{classdesc}
190
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000191\subsection{Encoders}
192\input{emailencoders}
Barry Warsaw5e634632001-09-26 05:23:47 +0000193
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000194\subsection{Exception classes}
195\input{emailexc}
Barry Warsaw5e634632001-09-26 05:23:47 +0000196
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000197\subsection{Miscellaneous utilities}
198\input{emailutil}
Barry Warsaw5e634632001-09-26 05:23:47 +0000199
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000200\subsection{Iterators}
201\input{emailiter}
Barry Warsaw5e634632001-09-26 05:23:47 +0000202
203\subsection{Differences from \module{mimelib}}
204
205The \module{email} package was originally prototyped as a separate
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000206library called
207\ulink{\module{mimelib}}{http://mimelib.sf.net/}.
208Changes have been made so that
Barry Warsaw5e634632001-09-26 05:23:47 +0000209method names are more consistent, and some methods or modules have
210either been added or removed. The semantics of some of the methods
211have also changed. For the most part, any functionality available in
Fred Drake90e68782001-09-27 20:09:39 +0000212\module{mimelib} is still available in the \refmodule{email} package,
Barry Warsaw5e634632001-09-26 05:23:47 +0000213albeit often in a different way.
214
215Here is a brief description of the differences between the
Fred Drake90e68782001-09-27 20:09:39 +0000216\module{mimelib} and the \refmodule{email} packages, along with hints on
Barry Warsaw5e634632001-09-26 05:23:47 +0000217how to port your applications.
218
219Of course, the most visible difference between the two packages is
Fred Drake90e68782001-09-27 20:09:39 +0000220that the package name has been changed to \refmodule{email}. In
Barry Warsaw5e634632001-09-26 05:23:47 +0000221addition, the top-level package has the following differences:
222
223\begin{itemize}
224\item \function{messageFromString()} has been renamed to
225 \function{message_from_string()}.
226\item \function{messageFromFile()} has been renamed to
227 \function{message_from_file()}.
228\end{itemize}
229
230The \class{Message} class has the following differences:
231
232\begin{itemize}
233\item The method \method{asString()} was renamed to \method{as_string()}.
234\item The method \method{ismultipart()} was renamed to
235 \method{is_multipart()}.
236\item The \method{get_payload()} method has grown a \var{decode}
237 optional argument.
238\item The method \method{getall()} was renamed to \method{get_all()}.
239\item The method \method{addheader()} was renamed to \method{add_header()}.
240\item The method \method{gettype()} was renamed to \method{get_type()}.
241\item The method\method{getmaintype()} was renamed to
242 \method{get_main_type()}.
243\item The method \method{getsubtype()} was renamed to
244 \method{get_subtype()}.
245\item The method \method{getparams()} was renamed to
246 \method{get_params()}.
247 Also, whereas \method{getparams()} returned a list of strings,
248 \method{get_params()} returns a list of 2-tuples, effectively
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000249 the key/value pairs of the parameters, split on the \character{=}
Barry Warsaw5e634632001-09-26 05:23:47 +0000250 sign.
251\item The method \method{getparam()} was renamed to \method{get_param()}.
252\item The method \method{getcharsets()} was renamed to
253 \method{get_charsets()}.
254\item The method \method{getfilename()} was renamed to
255 \method{get_filename()}.
256\item The method \method{getboundary()} was renamed to
257 \method{get_boundary()}.
258\item The method \method{setboundary()} was renamed to
259 \method{set_boundary()}.
260\item The method \method{getdecodedpayload()} was removed. To get
261 similar functionality, pass the value 1 to the \var{decode} flag
262 of the {get_payload()} method.
263\item The method \method{getpayloadastext()} was removed. Similar
264 functionality
265 is supported by the \class{DecodedGenerator} class in the
266 \refmodule{email.Generator} module.
267\item The method \method{getbodyastext()} was removed. You can get
268 similar functionality by creating an iterator with
269 \function{typed_subpart_iterator()} in the
270 \refmodule{email.Iterators} module.
271\end{itemize}
272
273The \class{Parser} class has no differences in its public interface.
274It does have some additional smarts to recognize
Fred Drakea6a885b2001-09-26 16:52:18 +0000275\mimetype{message/delivery-status} type messages, which it represents as
Barry Warsaw5e634632001-09-26 05:23:47 +0000276a \class{Message} instance containing separate \class{Message}
277subparts for each header block in the delivery status
278notification\footnote{Delivery Status Notifications (DSN) are defined
Fred Drake90e68782001-09-27 20:09:39 +0000279in \rfc{1894}.}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000280
281The \class{Generator} class has no differences in its public
282interface. There is a new class in the \refmodule{email.Generator}
283module though, called \class{DecodedGenerator} which provides most of
284the functionality previously available in the
285\method{Message.getpayloadastext()} method.
286
287The following modules and classes have been changed:
288
289\begin{itemize}
290\item The \class{MIMEBase} class constructor arguments \var{_major}
291 and \var{_minor} have changed to \var{_maintype} and
292 \var{_subtype} respectively.
293\item The \code{Image} class/module has been renamed to
294 \code{MIMEImage}. The \var{_minor} argument has been renamed to
295 \var{_subtype}.
296\item The \code{Text} class/module has been renamed to
297 \code{MIMEText}. The \var{_minor} argument has been renamed to
298 \var{_subtype}.
299\item The \code{MessageRFC822} class/module has been renamed to
300 \code{MIMEMessage}. Note that an earlier version of
301 \module{mimelib} called this class/module \code{RFC822}, but
302 that clashed with the Python standard library module
303 \refmodule{rfc822} on some case-insensitive file systems.
304
305 Also, the \class{MIMEMessage} class now represents any kind of
Fred Drakea6a885b2001-09-26 16:52:18 +0000306 MIME message with main type \mimetype{message}. It takes an
Barry Warsaw5e634632001-09-26 05:23:47 +0000307 optional argument \var{_subtype} which is used to set the MIME
Fred Drakea6a885b2001-09-26 16:52:18 +0000308 subtype. \var{_subtype} defaults to \mimetype{rfc822}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000309\end{itemize}
310
311\module{mimelib} provided some utility functions in its
312\module{address} and \module{date} modules. All of these functions
313have been moved to the \refmodule{email.Utils} module.
314
315The \code{MsgReader} class/module has been removed. Its functionality
316is most closely supported in the \function{body_line_iterator()}
317function in the \refmodule{email.Iterators} module.
318
319\subsection{Examples}
320
321Coming soon...