blob: 47bbf5be6da612890f2c230aa030547c69bdce4d [file] [log] [blame]
Barry Warsaw5b9da892002-10-01 01:05:52 +00001% Copyright (C) 2001,2002 Python Software Foundation
Barry Warsaw5e634632001-09-26 05:23:47 +00002% 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
Barry Warsaw5e17d202001-11-16 22:16:04 +000020\module{mimecntl}. It is specifically \emph{not} designed to do any
21sending of email messages to SMTP (\rfc{2821}) servers; that is the
Barry Warsaw5b9da892002-10-01 01:05:52 +000022function of the \refmodule{smtplib} module. The \module{email}
23package attempts to be as RFC-compliant as possible, supporting in
24addition to \rfc{2822}, such MIME-related RFCs as
25\rfc{2045}-\rfc{2047}, and \rfc{2231}.
Barry Warsaw5e634632001-09-26 05:23:47 +000026
27The primary distinguishing feature of the \module{email} package is
28that it splits the parsing and generating of email messages from the
29internal \emph{object model} representation of email. Applications
30using the \module{email} package deal primarily with objects; you can
31add sub-objects to messages, remove sub-objects from messages,
32completely re-arrange the contents, etc. There is a separate parser
33and a separate generator which handles the transformation from flat
Andrew M. Kuchling43dc1fc2001-11-05 01:55:03 +000034text to the object model, and then back to flat text again. There
Barry Warsaw5e634632001-09-26 05:23:47 +000035are also handy subclasses for some common MIME object types, and a few
36miscellaneous utilities that help with such common tasks as extracting
37and parsing message field values, creating RFC-compliant dates, etc.
38
39The following sections describe the functionality of the
40\module{email} package. The ordering follows a progression that
41should be common in applications: an email message is read as flat
Barry Warsaw5db478f2002-10-01 04:33:16 +000042text from a file or other source, the text is parsed to produce the
43object structure of the email message, this structure is manipulated,
44and finally rendered back into flat text.
Barry Warsaw5e634632001-09-26 05:23:47 +000045
Barry Warsaw5db478f2002-10-01 04:33:16 +000046It is perfectly feasible to create the object structure out of whole
47cloth --- i.e. completely from scratch. From there, a similar
48progression can be taken as above.
Barry Warsaw5e634632001-09-26 05:23:47 +000049
50Also included are detailed specifications of all the classes and
51modules that the \module{email} package provides, the exception
52classes you might encounter while using the \module{email} package,
53some auxiliary utilities, and a few examples. For users of the older
Barry Warsaw5b9da892002-10-01 01:05:52 +000054\module{mimelib} package, or previous versions of the \module{email}
55package, a section on differences and porting is provided.
Barry Warsaw5e634632001-09-26 05:23:47 +000056
Barry Warsaw5e17d202001-11-16 22:16:04 +000057\begin{seealso}
58 \seemodule{smtplib}{SMTP protocol client}
59\end{seealso}
60
Barry Warsaw5e634632001-09-26 05:23:47 +000061\subsection{Representing an email message}
Barry Warsawc5f8fe32001-09-26 22:21:52 +000062\input{emailmessage}
Barry Warsaw5e634632001-09-26 05:23:47 +000063
64\subsection{Parsing email messages}
Barry Warsawc5f8fe32001-09-26 22:21:52 +000065\input{emailparser}
Barry Warsaw5e634632001-09-26 05:23:47 +000066
67\subsection{Generating MIME documents}
Barry Warsawc5f8fe32001-09-26 22:21:52 +000068\input{emailgenerator}
Barry Warsaw5e634632001-09-26 05:23:47 +000069
70\subsection{Creating email and MIME objects from scratch}
Barry Warsaw5b9da892002-10-01 01:05:52 +000071\input{emailmimebase}
Barry Warsaw5e634632001-09-26 05:23:47 +000072
Barry Warsaw5db478f2002-10-01 04:33:16 +000073\subsection{Internationalized headers}
Barry Warsaw5b9da892002-10-01 01:05:52 +000074\input{emailheaders}
Barry Warsaw5e634632001-09-26 05:23:47 +000075
Barry Warsaw5db478f2002-10-01 04:33:16 +000076\subsection{Representing character sets}
77\input{emailcharsets}
78
Barry Warsawc5f8fe32001-09-26 22:21:52 +000079\subsection{Encoders}
80\input{emailencoders}
Barry Warsaw5e634632001-09-26 05:23:47 +000081
Barry Warsawc5f8fe32001-09-26 22:21:52 +000082\subsection{Exception classes}
83\input{emailexc}
Barry Warsaw5e634632001-09-26 05:23:47 +000084
Barry Warsawc5f8fe32001-09-26 22:21:52 +000085\subsection{Miscellaneous utilities}
86\input{emailutil}
Barry Warsaw5e634632001-09-26 05:23:47 +000087
Barry Warsawc5f8fe32001-09-26 22:21:52 +000088\subsection{Iterators}
89\input{emailiter}
Barry Warsaw5e634632001-09-26 05:23:47 +000090
Barry Warsaw5b9da892002-10-01 01:05:52 +000091\subsection{Differences from \module{email} v1 (up to Python 2.2.1)}
92
93Version 1 of the \module{email} package was bundled with Python
94releases up to Python 2.2.1. Version 2 was developed for the Python
952.3 release, and backported to Python 2.2.2. It was also available as
96a separate distutils based package. \module{email} version 2 is
Barry Warsaw5db478f2002-10-01 04:33:16 +000097almost entirely backward compatible with version 1, with the
Barry Warsaw5b9da892002-10-01 01:05:52 +000098following differences:
99
100\begin{itemize}
101\item The \module{email.Header} and \module{email.Charset} modules
102 have been added.
Barry Warsawdd868d32002-10-01 15:29:09 +0000103
Barry Warsaw5b9da892002-10-01 01:05:52 +0000104\item The pickle format for \class{Message} instances has changed.
105 Since this was never (and still isn't) formally defined, this
Barry Warsaw5db478f2002-10-01 04:33:16 +0000106 isn't considered a backward incompatibility. However if your
Barry Warsaw5b9da892002-10-01 01:05:52 +0000107 application pickles and unpickles \class{Message} instances, be
108 aware that in \module{email} version 2, \class{Message}
109 instances now have private variables \var{_charset} and
110 \var{_default_type}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000111
Barry Warsaw5b9da892002-10-01 01:05:52 +0000112\item Several methods in the \class{Message} class have been
Barry Warsaw5db478f2002-10-01 04:33:16 +0000113 deprecated, or their signatures changed. Also, many new methods
Barry Warsaw5b9da892002-10-01 01:05:52 +0000114 have been added. See the documentation for the \class{Message}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000115 class for details. The changes should be completely backward
Barry Warsaw5b9da892002-10-01 01:05:52 +0000116 compatible.
Barry Warsawdd868d32002-10-01 15:29:09 +0000117
Barry Warsaw5b9da892002-10-01 01:05:52 +0000118\item The object structure has changed in the face of
119 \mimetype{message/rfc822} content types. In \module{email}
120 version 1, such a type would be represented by a scalar payload,
121 i.e. the container message's \method{is_multipart()} returned
Barry Warsaw5db478f2002-10-01 04:33:16 +0000122 false, \method{get_payload()} was not a list object, but a single
123 \class{Message} instance.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000124
125 This structure was inconsistent with the rest of the package, so
126 the object representation for \mimetype{message/rfc822} content
Barry Warsaw5db478f2002-10-01 04:33:16 +0000127 types was changed. In \module{email} version 2, the container
Barry Warsaw5b9da892002-10-01 01:05:52 +0000128 \emph{does} return \code{True} from \method{is_multipart()}, and
129 \method{get_payload()} returns a list containing a single
130 \class{Message} item.
131
Barry Warsaw5db478f2002-10-01 04:33:16 +0000132 Note that this is one place that backward compatibility could
Barry Warsaw5b9da892002-10-01 01:05:52 +0000133 not be completely maintained. However, if you're already
134 testing the return type of \method{get_payload()}, you should be
135 fine. You just need to make sure your code doesn't do a
136 \method{set_payload()} with a \class{Message} instance on a
137 container with a content type of \mimetype{message/rfc822}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000138
Barry Warsaw5b9da892002-10-01 01:05:52 +0000139\item The \class{Parser} constructor's \var{strict} argument was
140 added, and its \method{parse()} and \method{parsestr()} methods
141 grew a \var{headersonly} argument. The \var{strict} flag was
142 also added to functions \function{email.message_from_file()}
143 and \function{email.message_from_string()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000144
Barry Warsaw5b9da892002-10-01 01:05:52 +0000145\item \method{Generator.__call__()} is deprecated; use
146 \method{Generator.flatten()} instead. The \class{Generator}
147 class has also grown the \method{clone()} method.
Barry Warsawdd868d32002-10-01 15:29:09 +0000148
Barry Warsaw5b9da892002-10-01 01:05:52 +0000149\item The \class{DecodedGenerator} class in the
150 \module{email.Generator} module was added.
Barry Warsawdd868d32002-10-01 15:29:09 +0000151
Barry Warsaw5b9da892002-10-01 01:05:52 +0000152\item The intermediate base classes \class{MIMENonMultipart} and
153 \class{MIMEMultipart} have been added, and interposed in the
Barry Warsaw5db478f2002-10-01 04:33:16 +0000154 class hierarchy for most of the other MIME-related derived
Barry Warsaw5b9da892002-10-01 01:05:52 +0000155 classes.
Barry Warsawdd868d32002-10-01 15:29:09 +0000156
Barry Warsaw5b9da892002-10-01 01:05:52 +0000157\item The \var{_encoder} argument to the \class{MIMEText} constructor
158 has been deprecated. Encoding now happens implicitly based
159 on the \var{_charset} argument.
Barry Warsawdd868d32002-10-01 15:29:09 +0000160
Barry Warsaw5b9da892002-10-01 01:05:52 +0000161\item The following functions in the \module{email.Utils} module have
162 been deprecated: \function{dump_address_pairs()},
163 \function{decode()}, and \function{encode()}. The following
164 functions have been added to the module:
165 \function{make_msgid()}, \function{decode_rfc2231()},
166 \function{encode_rfc2231()}, and \function{decode_params()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000167
Barry Warsaw5b9da892002-10-01 01:05:52 +0000168\item The non-public function \function{email.Iterators._structure()}
169 was added.
170\end{itemize}
171
Barry Warsaw5e634632001-09-26 05:23:47 +0000172\subsection{Differences from \module{mimelib}}
173
174The \module{email} package was originally prototyped as a separate
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000175library called
176\ulink{\module{mimelib}}{http://mimelib.sf.net/}.
177Changes have been made so that
Barry Warsaw5e634632001-09-26 05:23:47 +0000178method names are more consistent, and some methods or modules have
179either been added or removed. The semantics of some of the methods
180have also changed. For the most part, any functionality available in
Fred Drake90e68782001-09-27 20:09:39 +0000181\module{mimelib} is still available in the \refmodule{email} package,
Barry Warsaw5db478f2002-10-01 04:33:16 +0000182albeit often in a different way. Backward compatibility between
183the \module{mimelib} package and the \module{email} package was not a
184priority.
Barry Warsaw5e634632001-09-26 05:23:47 +0000185
186Here is a brief description of the differences between the
Fred Drake90e68782001-09-27 20:09:39 +0000187\module{mimelib} and the \refmodule{email} packages, along with hints on
Barry Warsaw5e634632001-09-26 05:23:47 +0000188how to port your applications.
189
190Of course, the most visible difference between the two packages is
Fred Drake90e68782001-09-27 20:09:39 +0000191that the package name has been changed to \refmodule{email}. In
Barry Warsaw5e634632001-09-26 05:23:47 +0000192addition, the top-level package has the following differences:
193
194\begin{itemize}
195\item \function{messageFromString()} has been renamed to
196 \function{message_from_string()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000197
Barry Warsaw5e634632001-09-26 05:23:47 +0000198\item \function{messageFromFile()} has been renamed to
199 \function{message_from_file()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000200
Barry Warsaw5e634632001-09-26 05:23:47 +0000201\end{itemize}
202
203The \class{Message} class has the following differences:
204
205\begin{itemize}
206\item The method \method{asString()} was renamed to \method{as_string()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000207
Barry Warsaw5e634632001-09-26 05:23:47 +0000208\item The method \method{ismultipart()} was renamed to
209 \method{is_multipart()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000210
Barry Warsaw5e634632001-09-26 05:23:47 +0000211\item The \method{get_payload()} method has grown a \var{decode}
212 optional argument.
Barry Warsawdd868d32002-10-01 15:29:09 +0000213
Barry Warsaw5e634632001-09-26 05:23:47 +0000214\item The method \method{getall()} was renamed to \method{get_all()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000215
Barry Warsaw5e634632001-09-26 05:23:47 +0000216\item The method \method{addheader()} was renamed to \method{add_header()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000217
Barry Warsaw5e634632001-09-26 05:23:47 +0000218\item The method \method{gettype()} was renamed to \method{get_type()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000219
Barry Warsaw5e634632001-09-26 05:23:47 +0000220\item The method\method{getmaintype()} was renamed to
221 \method{get_main_type()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000222
Barry Warsaw5e634632001-09-26 05:23:47 +0000223\item The method \method{getsubtype()} was renamed to
224 \method{get_subtype()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000225
Barry Warsaw5e634632001-09-26 05:23:47 +0000226\item The method \method{getparams()} was renamed to
227 \method{get_params()}.
228 Also, whereas \method{getparams()} returned a list of strings,
229 \method{get_params()} returns a list of 2-tuples, effectively
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000230 the key/value pairs of the parameters, split on the \character{=}
Barry Warsaw5e634632001-09-26 05:23:47 +0000231 sign.
Barry Warsawdd868d32002-10-01 15:29:09 +0000232
Barry Warsaw5e634632001-09-26 05:23:47 +0000233\item The method \method{getparam()} was renamed to \method{get_param()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000234
Barry Warsaw5e634632001-09-26 05:23:47 +0000235\item The method \method{getcharsets()} was renamed to
236 \method{get_charsets()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000237
Barry Warsaw5e634632001-09-26 05:23:47 +0000238\item The method \method{getfilename()} was renamed to
239 \method{get_filename()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000240
Barry Warsaw5e634632001-09-26 05:23:47 +0000241\item The method \method{getboundary()} was renamed to
242 \method{get_boundary()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000243
Barry Warsaw5e634632001-09-26 05:23:47 +0000244\item The method \method{setboundary()} was renamed to
245 \method{set_boundary()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000246
Barry Warsaw5e634632001-09-26 05:23:47 +0000247\item The method \method{getdecodedpayload()} was removed. To get
248 similar functionality, pass the value 1 to the \var{decode} flag
249 of the {get_payload()} method.
Barry Warsawdd868d32002-10-01 15:29:09 +0000250
Barry Warsaw5e634632001-09-26 05:23:47 +0000251\item The method \method{getpayloadastext()} was removed. Similar
252 functionality
253 is supported by the \class{DecodedGenerator} class in the
254 \refmodule{email.Generator} module.
Barry Warsawdd868d32002-10-01 15:29:09 +0000255
Barry Warsaw5e634632001-09-26 05:23:47 +0000256\item The method \method{getbodyastext()} was removed. You can get
257 similar functionality by creating an iterator with
258 \function{typed_subpart_iterator()} in the
259 \refmodule{email.Iterators} module.
260\end{itemize}
261
262The \class{Parser} class has no differences in its public interface.
263It does have some additional smarts to recognize
Fred Drakea6a885b2001-09-26 16:52:18 +0000264\mimetype{message/delivery-status} type messages, which it represents as
Barry Warsaw5e634632001-09-26 05:23:47 +0000265a \class{Message} instance containing separate \class{Message}
266subparts for each header block in the delivery status
267notification\footnote{Delivery Status Notifications (DSN) are defined
Fred Drake90e68782001-09-27 20:09:39 +0000268in \rfc{1894}.}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000269
270The \class{Generator} class has no differences in its public
271interface. There is a new class in the \refmodule{email.Generator}
272module though, called \class{DecodedGenerator} which provides most of
273the functionality previously available in the
274\method{Message.getpayloadastext()} method.
275
276The following modules and classes have been changed:
277
278\begin{itemize}
279\item The \class{MIMEBase} class constructor arguments \var{_major}
280 and \var{_minor} have changed to \var{_maintype} and
281 \var{_subtype} respectively.
Barry Warsawdd868d32002-10-01 15:29:09 +0000282
Barry Warsaw5e634632001-09-26 05:23:47 +0000283\item The \code{Image} class/module has been renamed to
284 \code{MIMEImage}. The \var{_minor} argument has been renamed to
285 \var{_subtype}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000286
Barry Warsaw5e634632001-09-26 05:23:47 +0000287\item The \code{Text} class/module has been renamed to
288 \code{MIMEText}. The \var{_minor} argument has been renamed to
289 \var{_subtype}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000290
Barry Warsaw5e634632001-09-26 05:23:47 +0000291\item The \code{MessageRFC822} class/module has been renamed to
292 \code{MIMEMessage}. Note that an earlier version of
293 \module{mimelib} called this class/module \code{RFC822}, but
294 that clashed with the Python standard library module
295 \refmodule{rfc822} on some case-insensitive file systems.
296
297 Also, the \class{MIMEMessage} class now represents any kind of
Fred Drakea6a885b2001-09-26 16:52:18 +0000298 MIME message with main type \mimetype{message}. It takes an
Barry Warsaw5e634632001-09-26 05:23:47 +0000299 optional argument \var{_subtype} which is used to set the MIME
Fred Drakea6a885b2001-09-26 16:52:18 +0000300 subtype. \var{_subtype} defaults to \mimetype{rfc822}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000301\end{itemize}
302
303\module{mimelib} provided some utility functions in its
304\module{address} and \module{date} modules. All of these functions
305have been moved to the \refmodule{email.Utils} module.
306
307The \code{MsgReader} class/module has been removed. Its functionality
308is most closely supported in the \function{body_line_iterator()}
309function in the \refmodule{email.Iterators} module.
310
311\subsection{Examples}
312
Barry Warsaw2bb077f2001-11-05 17:50:53 +0000313Here are a few examples of how to use the \module{email} package to
314read, write, and send simple email messages, as well as more complex
315MIME messages.
316
317First, let's see how to create and send a simple text message:
318
Fred Drakefcc31b42002-10-01 14:17:10 +0000319\verbatiminput{email-simple.py}
Barry Warsaw2bb077f2001-11-05 17:50:53 +0000320
321Here's an example of how to send a MIME message containing a bunch of
Barry Warsawea66abc2002-10-01 04:48:06 +0000322family pictures that may be residing in a directory:
Barry Warsaw2bb077f2001-11-05 17:50:53 +0000323
Fred Drakefcc31b42002-10-01 14:17:10 +0000324\verbatiminput{email-mime.py}
Barry Warsaw2bb077f2001-11-05 17:50:53 +0000325
326Here's an example\footnote{Thanks to Matthew Dixon Cowles for the
327original inspiration and examples.} of how to send the entire contents
328of a directory as an email message:
329
Fred Drakefcc31b42002-10-01 14:17:10 +0000330\verbatiminput{email-dir.py}
Barry Warsaw2bb077f2001-11-05 17:50:53 +0000331
332And finally, here's an example of how to unpack a MIME message like
333the one above, into a directory of files:
334
Fred Drakefcc31b42002-10-01 14:17:10 +0000335\verbatiminput{email-unpack.py}