blob: 56affa5369c1e0d0c1fe72ecfe4f4e95a0f54598 [file] [log] [blame]
Barry Warsawbb113862004-10-03 03:16:19 +00001% Copyright (C) 2001-2004 Python Software Foundation
2% Author: barry@python.org (Barry Warsaw)
Barry Warsaw5e634632001-09-26 05:23:47 +00003
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.}
Barry Warsawbb113862004-10-03 03:16:19 +000010\moduleauthor{Barry A. Warsaw}{barry@python.org}
11\sectionauthor{Barry A. Warsaw}{barry@python.org}
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
Barry Warsawbb113862004-10-03 03:16:19 +000025\rfc{2045}, \rfc{2046}, \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 Warsawbb113862004-10-03 03:16:19 +000082\subsection{Exception and Defect classes}
Barry Warsawc5f8fe32001-09-26 22:21:52 +000083\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 Warsawbb113862004-10-03 03:16:19 +000091\subsection{Package History}
Barry Warsaw5b9da892002-10-01 01:05:52 +000092
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
Barry Warsawbb113862004-10-03 03:16:19 +000096a separate distutils-based package, and is compatible back to Python 2.1.
97
98\module{email} version 3.0 was released with Python 2.4 and as a separate
99distutils-based package. It is compatible back to Python 2.3.
100
101Here are the differences between \module{email} version 3 and version 2:
102
103\begin{itemize}
104\item The \class{FeedParser} class was introduced, and the \class{Parser}
105 class was implemented in terms of the \class{FeedParser}. All parsing
106 there for is non-strict, and parsing will make a best effort never to
107 raise an exception. Problems found while parsing messages are stored in
108 the message's \var{defect} attribute.
109
110\item All aspects of the API which raised \exception{DeprecationWarning}s in
111 version 2 have been removed. These include the \var{_encoder} argument
112 to the \class{MIMEText} constructor, the \method{Message.add_payload()}
113 method, the \function{Utils.dump_address_pair()} function, and the
114 functions \function{Utils.decode()} and \function{Utils.encode()}.
115
116\item New \exception{DeprecationWarning}s have been added to:
117 \method{Generator.__call__()}, \method{Message.get_type()},
118 \method{Message.get_main_type()}, \method{Message.get_subtype()}, and
119 the \var{strict} argument to the \class{Parser} class. These are
120 expected to be removed in email 3.1.
121
122\item Support for Pythons earlier than 2.3 has been removed.
123\end{itemize}
124
125Here are the differences between \module{email} version 2 and version 1:
Barry Warsaw5b9da892002-10-01 01:05:52 +0000126
127\begin{itemize}
128\item The \module{email.Header} and \module{email.Charset} modules
129 have been added.
Barry Warsawdd868d32002-10-01 15:29:09 +0000130
Barry Warsaw5b9da892002-10-01 01:05:52 +0000131\item The pickle format for \class{Message} instances has changed.
132 Since this was never (and still isn't) formally defined, this
Barry Warsaw5db478f2002-10-01 04:33:16 +0000133 isn't considered a backward incompatibility. However if your
Barry Warsaw5b9da892002-10-01 01:05:52 +0000134 application pickles and unpickles \class{Message} instances, be
135 aware that in \module{email} version 2, \class{Message}
136 instances now have private variables \var{_charset} and
137 \var{_default_type}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000138
Barry Warsaw5b9da892002-10-01 01:05:52 +0000139\item Several methods in the \class{Message} class have been
Barry Warsaw5db478f2002-10-01 04:33:16 +0000140 deprecated, or their signatures changed. Also, many new methods
Barry Warsaw5b9da892002-10-01 01:05:52 +0000141 have been added. See the documentation for the \class{Message}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000142 class for details. The changes should be completely backward
Barry Warsaw5b9da892002-10-01 01:05:52 +0000143 compatible.
Barry Warsawdd868d32002-10-01 15:29:09 +0000144
Barry Warsaw5b9da892002-10-01 01:05:52 +0000145\item The object structure has changed in the face of
146 \mimetype{message/rfc822} content types. In \module{email}
147 version 1, such a type would be represented by a scalar payload,
148 i.e. the container message's \method{is_multipart()} returned
Barry Warsaw5db478f2002-10-01 04:33:16 +0000149 false, \method{get_payload()} was not a list object, but a single
150 \class{Message} instance.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000151
152 This structure was inconsistent with the rest of the package, so
153 the object representation for \mimetype{message/rfc822} content
Barry Warsaw5db478f2002-10-01 04:33:16 +0000154 types was changed. In \module{email} version 2, the container
Barry Warsaw5b9da892002-10-01 01:05:52 +0000155 \emph{does} return \code{True} from \method{is_multipart()}, and
156 \method{get_payload()} returns a list containing a single
157 \class{Message} item.
158
Barry Warsaw5db478f2002-10-01 04:33:16 +0000159 Note that this is one place that backward compatibility could
Barry Warsaw5b9da892002-10-01 01:05:52 +0000160 not be completely maintained. However, if you're already
161 testing the return type of \method{get_payload()}, you should be
162 fine. You just need to make sure your code doesn't do a
163 \method{set_payload()} with a \class{Message} instance on a
164 container with a content type of \mimetype{message/rfc822}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000165
Barry Warsaw5b9da892002-10-01 01:05:52 +0000166\item The \class{Parser} constructor's \var{strict} argument was
167 added, and its \method{parse()} and \method{parsestr()} methods
168 grew a \var{headersonly} argument. The \var{strict} flag was
169 also added to functions \function{email.message_from_file()}
170 and \function{email.message_from_string()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000171
Barry Warsaw5b9da892002-10-01 01:05:52 +0000172\item \method{Generator.__call__()} is deprecated; use
173 \method{Generator.flatten()} instead. The \class{Generator}
174 class has also grown the \method{clone()} method.
Barry Warsawdd868d32002-10-01 15:29:09 +0000175
Barry Warsaw5b9da892002-10-01 01:05:52 +0000176\item The \class{DecodedGenerator} class in the
177 \module{email.Generator} module was added.
Barry Warsawdd868d32002-10-01 15:29:09 +0000178
Barry Warsaw5b9da892002-10-01 01:05:52 +0000179\item The intermediate base classes \class{MIMENonMultipart} and
180 \class{MIMEMultipart} have been added, and interposed in the
Barry Warsaw5db478f2002-10-01 04:33:16 +0000181 class hierarchy for most of the other MIME-related derived
Barry Warsaw5b9da892002-10-01 01:05:52 +0000182 classes.
Barry Warsawdd868d32002-10-01 15:29:09 +0000183
Barry Warsaw5b9da892002-10-01 01:05:52 +0000184\item The \var{_encoder} argument to the \class{MIMEText} constructor
185 has been deprecated. Encoding now happens implicitly based
186 on the \var{_charset} argument.
Barry Warsawdd868d32002-10-01 15:29:09 +0000187
Barry Warsaw5b9da892002-10-01 01:05:52 +0000188\item The following functions in the \module{email.Utils} module have
189 been deprecated: \function{dump_address_pairs()},
190 \function{decode()}, and \function{encode()}. The following
191 functions have been added to the module:
192 \function{make_msgid()}, \function{decode_rfc2231()},
193 \function{encode_rfc2231()}, and \function{decode_params()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000194
Barry Warsaw5b9da892002-10-01 01:05:52 +0000195\item The non-public function \function{email.Iterators._structure()}
196 was added.
197\end{itemize}
198
Barry Warsaw5e634632001-09-26 05:23:47 +0000199\subsection{Differences from \module{mimelib}}
200
201The \module{email} package was originally prototyped as a separate
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000202library called
203\ulink{\module{mimelib}}{http://mimelib.sf.net/}.
204Changes have been made so that
Barry Warsaw5e634632001-09-26 05:23:47 +0000205method names are more consistent, and some methods or modules have
206either been added or removed. The semantics of some of the methods
207have also changed. For the most part, any functionality available in
Fred Drake90e68782001-09-27 20:09:39 +0000208\module{mimelib} is still available in the \refmodule{email} package,
Barry Warsaw5db478f2002-10-01 04:33:16 +0000209albeit often in a different way. Backward compatibility between
210the \module{mimelib} package and the \module{email} package was not a
211priority.
Barry Warsaw5e634632001-09-26 05:23:47 +0000212
213Here is a brief description of the differences between the
Fred Drake90e68782001-09-27 20:09:39 +0000214\module{mimelib} and the \refmodule{email} packages, along with hints on
Barry Warsaw5e634632001-09-26 05:23:47 +0000215how to port your applications.
216
217Of course, the most visible difference between the two packages is
Fred Drake90e68782001-09-27 20:09:39 +0000218that the package name has been changed to \refmodule{email}. In
Barry Warsaw5e634632001-09-26 05:23:47 +0000219addition, the top-level package has the following differences:
220
221\begin{itemize}
222\item \function{messageFromString()} has been renamed to
223 \function{message_from_string()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000224
Barry Warsaw5e634632001-09-26 05:23:47 +0000225\item \function{messageFromFile()} has been renamed to
226 \function{message_from_file()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000227
Barry Warsaw5e634632001-09-26 05:23:47 +0000228\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()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000234
Barry Warsaw5e634632001-09-26 05:23:47 +0000235\item The method \method{ismultipart()} was renamed to
236 \method{is_multipart()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000237
Barry Warsaw5e634632001-09-26 05:23:47 +0000238\item The \method{get_payload()} method has grown a \var{decode}
239 optional argument.
Barry Warsawdd868d32002-10-01 15:29:09 +0000240
Barry Warsaw5e634632001-09-26 05:23:47 +0000241\item The method \method{getall()} was renamed to \method{get_all()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000242
Barry Warsaw5e634632001-09-26 05:23:47 +0000243\item The method \method{addheader()} was renamed to \method{add_header()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000244
Barry Warsaw5e634632001-09-26 05:23:47 +0000245\item The method \method{gettype()} was renamed to \method{get_type()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000246
Barry Warsaw5e634632001-09-26 05:23:47 +0000247\item The method\method{getmaintype()} was renamed to
248 \method{get_main_type()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000249
Barry Warsaw5e634632001-09-26 05:23:47 +0000250\item The method \method{getsubtype()} was renamed to
251 \method{get_subtype()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000252
Barry Warsaw5e634632001-09-26 05:23:47 +0000253\item The method \method{getparams()} was renamed to
254 \method{get_params()}.
255 Also, whereas \method{getparams()} returned a list of strings,
256 \method{get_params()} returns a list of 2-tuples, effectively
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000257 the key/value pairs of the parameters, split on the \character{=}
Barry Warsaw5e634632001-09-26 05:23:47 +0000258 sign.
Barry Warsawdd868d32002-10-01 15:29:09 +0000259
Barry Warsaw5e634632001-09-26 05:23:47 +0000260\item The method \method{getparam()} was renamed to \method{get_param()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000261
Barry Warsaw5e634632001-09-26 05:23:47 +0000262\item The method \method{getcharsets()} was renamed to
263 \method{get_charsets()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000264
Barry Warsaw5e634632001-09-26 05:23:47 +0000265\item The method \method{getfilename()} was renamed to
266 \method{get_filename()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000267
Barry Warsaw5e634632001-09-26 05:23:47 +0000268\item The method \method{getboundary()} was renamed to
269 \method{get_boundary()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000270
Barry Warsaw5e634632001-09-26 05:23:47 +0000271\item The method \method{setboundary()} was renamed to
272 \method{set_boundary()}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000273
Barry Warsaw5e634632001-09-26 05:23:47 +0000274\item The method \method{getdecodedpayload()} was removed. To get
275 similar functionality, pass the value 1 to the \var{decode} flag
276 of the {get_payload()} method.
Barry Warsawdd868d32002-10-01 15:29:09 +0000277
Barry Warsaw5e634632001-09-26 05:23:47 +0000278\item The method \method{getpayloadastext()} was removed. Similar
279 functionality
280 is supported by the \class{DecodedGenerator} class in the
281 \refmodule{email.Generator} module.
Barry Warsawdd868d32002-10-01 15:29:09 +0000282
Barry Warsaw5e634632001-09-26 05:23:47 +0000283\item The method \method{getbodyastext()} was removed. You can get
284 similar functionality by creating an iterator with
285 \function{typed_subpart_iterator()} in the
286 \refmodule{email.Iterators} module.
287\end{itemize}
288
289The \class{Parser} class has no differences in its public interface.
290It does have some additional smarts to recognize
Fred Drakea6a885b2001-09-26 16:52:18 +0000291\mimetype{message/delivery-status} type messages, which it represents as
Barry Warsaw5e634632001-09-26 05:23:47 +0000292a \class{Message} instance containing separate \class{Message}
293subparts for each header block in the delivery status
294notification\footnote{Delivery Status Notifications (DSN) are defined
Fred Drake90e68782001-09-27 20:09:39 +0000295in \rfc{1894}.}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000296
297The \class{Generator} class has no differences in its public
298interface. There is a new class in the \refmodule{email.Generator}
299module though, called \class{DecodedGenerator} which provides most of
300the functionality previously available in the
301\method{Message.getpayloadastext()} method.
302
303The following modules and classes have been changed:
304
305\begin{itemize}
306\item The \class{MIMEBase} class constructor arguments \var{_major}
307 and \var{_minor} have changed to \var{_maintype} and
308 \var{_subtype} respectively.
Barry Warsawdd868d32002-10-01 15:29:09 +0000309
Barry Warsaw5e634632001-09-26 05:23:47 +0000310\item The \code{Image} class/module has been renamed to
311 \code{MIMEImage}. The \var{_minor} argument has been renamed to
312 \var{_subtype}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000313
Barry Warsaw5e634632001-09-26 05:23:47 +0000314\item The \code{Text} class/module has been renamed to
315 \code{MIMEText}. The \var{_minor} argument has been renamed to
316 \var{_subtype}.
Barry Warsawdd868d32002-10-01 15:29:09 +0000317
Barry Warsaw5e634632001-09-26 05:23:47 +0000318\item The \code{MessageRFC822} class/module has been renamed to
319 \code{MIMEMessage}. Note that an earlier version of
320 \module{mimelib} called this class/module \code{RFC822}, but
321 that clashed with the Python standard library module
322 \refmodule{rfc822} on some case-insensitive file systems.
323
324 Also, the \class{MIMEMessage} class now represents any kind of
Fred Drakea6a885b2001-09-26 16:52:18 +0000325 MIME message with main type \mimetype{message}. It takes an
Barry Warsaw5e634632001-09-26 05:23:47 +0000326 optional argument \var{_subtype} which is used to set the MIME
Fred Drakea6a885b2001-09-26 16:52:18 +0000327 subtype. \var{_subtype} defaults to \mimetype{rfc822}.
Barry Warsaw5e634632001-09-26 05:23:47 +0000328\end{itemize}
329
330\module{mimelib} provided some utility functions in its
331\module{address} and \module{date} modules. All of these functions
332have been moved to the \refmodule{email.Utils} module.
333
334The \code{MsgReader} class/module has been removed. Its functionality
335is most closely supported in the \function{body_line_iterator()}
336function in the \refmodule{email.Iterators} module.
337
338\subsection{Examples}
339
Barry Warsaw2bb077f2001-11-05 17:50:53 +0000340Here are a few examples of how to use the \module{email} package to
341read, write, and send simple email messages, as well as more complex
342MIME messages.
343
344First, let's see how to create and send a simple text message:
345
Fred Drakefcc31b42002-10-01 14:17:10 +0000346\verbatiminput{email-simple.py}
Barry Warsaw2bb077f2001-11-05 17:50:53 +0000347
348Here's an example of how to send a MIME message containing a bunch of
Barry Warsawea66abc2002-10-01 04:48:06 +0000349family pictures that may be residing in a directory:
Barry Warsaw2bb077f2001-11-05 17:50:53 +0000350
Fred Drakefcc31b42002-10-01 14:17:10 +0000351\verbatiminput{email-mime.py}
Barry Warsaw2bb077f2001-11-05 17:50:53 +0000352
Fred Drake7934bc22003-01-29 05:10:27 +0000353Here's an example of how to send the entire contents of a directory as
354an email message:
355\footnote{Thanks to Matthew Dixon Cowles for the original inspiration
356 and examples.}
Barry Warsaw2bb077f2001-11-05 17:50:53 +0000357
Fred Drakefcc31b42002-10-01 14:17:10 +0000358\verbatiminput{email-dir.py}
Barry Warsaw2bb077f2001-11-05 17:50:53 +0000359
360And finally, here's an example of how to unpack a MIME message like
361the one above, into a directory of files:
362
Fred Drakefcc31b42002-10-01 14:17:10 +0000363\verbatiminput{email-unpack.py}