blob: 212c3215d68f368846513c9400d4700380f5bd7c [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. % Copyright (C) 2001-2007 Python Software Foundation
2.. % Author: barry@python.org (Barry Warsaw)
3
4
5:mod:`email` --- An email and MIME handling package
6===================================================
7
8.. module:: email
9 :synopsis: Package supporting the parsing, manipulating, and generating email messages,
10 including MIME documents.
11.. moduleauthor:: Barry A. Warsaw <barry@python.org>
12.. sectionauthor:: Barry A. Warsaw <barry@python.org>
13
14
15.. versionadded:: 2.2
16
17The :mod:`email` package is a library for managing email messages, including
18MIME and other :rfc:`2822`\ -based message documents. It subsumes most of the
19functionality in several older standard modules such as :mod:`rfc822`,
20:mod:`mimetools`, :mod:`multifile`, and other non-standard packages such as
21:mod:`mimecntl`. It is specifically *not* designed to do any sending of email
22messages to SMTP (:rfc:`2821`), NNTP, or other servers; those are functions of
23modules such as :mod:`smtplib` and :mod:`nntplib`. The :mod:`email` package
24attempts to be as RFC-compliant as possible, supporting in addition to
25:rfc:`2822`, such MIME-related RFCs as :rfc:`2045`, :rfc:`2046`, :rfc:`2047`,
26and :rfc:`2231`.
27
28The primary distinguishing feature of the :mod:`email` package is that it splits
29the parsing and generating of email messages from the internal *object model*
30representation of email. Applications using the :mod:`email` package deal
31primarily with objects; you can add sub-objects to messages, remove sub-objects
32from messages, completely re-arrange the contents, etc. There is a separate
33parser and a separate generator which handles the transformation from flat text
34to the object model, and then back to flat text again. There are also handy
35subclasses for some common MIME object types, and a few miscellaneous utilities
36that help with such common tasks as extracting and parsing message field values,
37creating RFC-compliant dates, etc.
38
39The following sections describe the functionality of the :mod:`email` package.
40The ordering follows a progression that should be common in applications: an
41email message is read as flat text from a file or other source, the text is
42parsed to produce the object structure of the email message, this structure is
43manipulated, and finally, the object tree is rendered back into flat text.
44
45It is perfectly feasible to create the object structure out of whole cloth ---
46i.e. completely from scratch. From there, a similar progression can be taken as
47above.
48
49Also included are detailed specifications of all the classes and modules that
50the :mod:`email` package provides, the exception classes you might encounter
51while using the :mod:`email` package, some auxiliary utilities, and a few
52examples. For users of the older :mod:`mimelib` package, or previous versions
53of the :mod:`email` package, a section on differences and porting is provided.
54
55Contents of the :mod:`email` package documentation:
56
57.. toctree::
58
59 email.message.rst
60 email.parser.rst
61 email.generator.rst
62 email.mime.rst
63 email.header.rst
64 email.charset.rst
65 email.encoders.rst
66 email.errors.rst
67 email.util.rst
68 email.iterators.rst
69 email-examples.rst
70
71
72.. seealso::
73
74 Module :mod:`smtplib`
75 SMTP protocol client
76
77 Module :mod:`nntplib`
78 NNTP protocol client
79
80
81.. _email-pkg-history:
82
83Package History
84---------------
85
86This table describes the release history of the email package, corresponding to
87the version of Python that the package was released with. For purposes of this
88document, when you see a note about change or added versions, these refer to the
89Python version the change was made in, *not* the email package version. This
90table also describes the Python compatibility of each version of the package.
91
92+---------------+------------------------------+-----------------------+
93| email version | distributed with | compatible with |
94+===============+==============================+=======================+
95| :const:`1.x` | Python 2.2.0 to Python 2.2.1 | *no longer supported* |
96+---------------+------------------------------+-----------------------+
97| :const:`2.5` | Python 2.2.2+ and Python 2.3 | Python 2.1 to 2.5 |
98+---------------+------------------------------+-----------------------+
99| :const:`3.0` | Python 2.4 | Python 2.3 to 2.5 |
100+---------------+------------------------------+-----------------------+
101| :const:`4.0` | Python 2.5 | Python 2.3 to 2.5 |
102+---------------+------------------------------+-----------------------+
103
104Here are the major differences between :mod:`email` version 4 and version 3:
105
106* All modules have been renamed according to :pep:`8` standards. For example,
107 the version 3 module :mod:`email.Message` was renamed to :mod:`email.message` in
108 version 4.
109
110* A new subpackage :mod:`email.mime` was added and all the version 3
111 :mod:`email.MIME\*` modules were renamed and situated into the :mod:`email.mime`
112 subpackage. For example, the version 3 module :mod:`email.MIMEText` was renamed
113 to :mod:`email.mime.text`.
114
115 *Note that the version 3 names will continue to work until Python 2.6*.
116
117* The :mod:`email.mime.application` module was added, which contains the
118 :class:`MIMEApplication` class.
119
120* Methods that were deprecated in version 3 have been removed. These include
121 :meth:`Generator.__call__`, :meth:`Message.get_type`,
122 :meth:`Message.get_main_type`, :meth:`Message.get_subtype`.
123
124* Fixes have been added for :rfc:`2231` support which can change some of the
125 return types for :func:`Message.get_param` and friends. Under some
126 circumstances, values which used to return a 3-tuple now return simple strings
127 (specifically, if all extended parameter segments were unencoded, there is no
128 language and charset designation expected, so the return type is now a simple
129 string). Also, %-decoding used to be done for both encoded and unencoded
130 segments; this decoding is now done only for encoded segments.
131
132Here are the major differences between :mod:`email` version 3 and version 2:
133
134* The :class:`FeedParser` class was introduced, and the :class:`Parser` class
135 was implemented in terms of the :class:`FeedParser`. All parsing therefore is
136 non-strict, and parsing will make a best effort never to raise an exception.
137 Problems found while parsing messages are stored in the message's *defect*
138 attribute.
139
140* All aspects of the API which raised :exc:`DeprecationWarning`\ s in version 2
141 have been removed. These include the *_encoder* argument to the
142 :class:`MIMEText` constructor, the :meth:`Message.add_payload` method, the
143 :func:`Utils.dump_address_pair` function, and the functions :func:`Utils.decode`
144 and :func:`Utils.encode`.
145
146* New :exc:`DeprecationWarning`\ s have been added to:
147 :meth:`Generator.__call__`, :meth:`Message.get_type`,
148 :meth:`Message.get_main_type`, :meth:`Message.get_subtype`, and the *strict*
149 argument to the :class:`Parser` class. These are expected to be removed in
150 future versions.
151
152* Support for Pythons earlier than 2.3 has been removed.
153
154Here are the differences between :mod:`email` version 2 and version 1:
155
156* The :mod:`email.Header` and :mod:`email.Charset` modules have been added.
157
158* The pickle format for :class:`Message` instances has changed. Since this was
159 never (and still isn't) formally defined, this isn't considered a backward
160 incompatibility. However if your application pickles and unpickles
161 :class:`Message` instances, be aware that in :mod:`email` version 2,
162 :class:`Message` instances now have private variables *_charset* and
163 *_default_type*.
164
165* Several methods in the :class:`Message` class have been deprecated, or their
166 signatures changed. Also, many new methods have been added. See the
167 documentation for the :class:`Message` class for details. The changes should be
168 completely backward compatible.
169
170* The object structure has changed in the face of :mimetype:`message/rfc822`
171 content types. In :mod:`email` version 1, such a type would be represented by a
172 scalar payload, i.e. the container message's :meth:`is_multipart` returned
173 false, :meth:`get_payload` was not a list object, but a single :class:`Message`
174 instance.
175
176 This structure was inconsistent with the rest of the package, so the object
177 representation for :mimetype:`message/rfc822` content types was changed. In
178 :mod:`email` version 2, the container *does* return ``True`` from
179 :meth:`is_multipart`, and :meth:`get_payload` returns a list containing a single
180 :class:`Message` item.
181
182 Note that this is one place that backward compatibility could not be completely
183 maintained. However, if you're already testing the return type of
184 :meth:`get_payload`, you should be fine. You just need to make sure your code
185 doesn't do a :meth:`set_payload` with a :class:`Message` instance on a container
186 with a content type of :mimetype:`message/rfc822`.
187
188* The :class:`Parser` constructor's *strict* argument was added, and its
189 :meth:`parse` and :meth:`parsestr` methods grew a *headersonly* argument. The
190 *strict* flag was also added to functions :func:`email.message_from_file` and
191 :func:`email.message_from_string`.
192
193* :meth:`Generator.__call__` is deprecated; use :meth:`Generator.flatten`
194 instead. The :class:`Generator` class has also grown the :meth:`clone` method.
195
196* The :class:`DecodedGenerator` class in the :mod:`email.Generator` module was
197 added.
198
199* The intermediate base classes :class:`MIMENonMultipart` and
200 :class:`MIMEMultipart` have been added, and interposed in the class hierarchy
201 for most of the other MIME-related derived classes.
202
203* The *_encoder* argument to the :class:`MIMEText` constructor has been
204 deprecated. Encoding now happens implicitly based on the *_charset* argument.
205
206* The following functions in the :mod:`email.Utils` module have been deprecated:
207 :func:`dump_address_pairs`, :func:`decode`, and :func:`encode`. The following
208 functions have been added to the module: :func:`make_msgid`,
209 :func:`decode_rfc2231`, :func:`encode_rfc2231`, and :func:`decode_params`.
210
211* The non-public function :func:`email.Iterators._structure` was added.
212
213
214Differences from :mod:`mimelib`
215-------------------------------
216
217The :mod:`email` package was originally prototyped as a separate library called
218`mimelib <http://mimelib.sf.net/>`_. Changes have been made so that method names
219are more consistent, and some methods or modules have either been added or
220removed. The semantics of some of the methods have also changed. For the most
221part, any functionality available in :mod:`mimelib` is still available in the
222:mod:`email` package, albeit often in a different way. Backward compatibility
223between the :mod:`mimelib` package and the :mod:`email` package was not a
224priority.
225
226Here is a brief description of the differences between the :mod:`mimelib` and
227the :mod:`email` packages, along with hints on how to port your applications.
228
229Of course, the most visible difference between the two packages is that the
230package name has been changed to :mod:`email`. In addition, the top-level
231package has the following differences:
232
233* :func:`messageFromString` has been renamed to :func:`message_from_string`.
234
235* :func:`messageFromFile` has been renamed to :func:`message_from_file`.
236
237The :class:`Message` class has the following differences:
238
239* The method :meth:`asString` was renamed to :meth:`as_string`.
240
241* The method :meth:`ismultipart` was renamed to :meth:`is_multipart`.
242
243* The :meth:`get_payload` method has grown a *decode* optional argument.
244
245* The method :meth:`getall` was renamed to :meth:`get_all`.
246
247* The method :meth:`addheader` was renamed to :meth:`add_header`.
248
249* The method :meth:`gettype` was renamed to :meth:`get_type`.
250
251* The method :meth:`getmaintype` was renamed to :meth:`get_main_type`.
252
253* The method :meth:`getsubtype` was renamed to :meth:`get_subtype`.
254
255* The method :meth:`getparams` was renamed to :meth:`get_params`. Also, whereas
256 :meth:`getparams` returned a list of strings, :meth:`get_params` returns a list
257 of 2-tuples, effectively the key/value pairs of the parameters, split on the
258 ``'='`` sign.
259
260* The method :meth:`getparam` was renamed to :meth:`get_param`.
261
262* The method :meth:`getcharsets` was renamed to :meth:`get_charsets`.
263
264* The method :meth:`getfilename` was renamed to :meth:`get_filename`.
265
266* The method :meth:`getboundary` was renamed to :meth:`get_boundary`.
267
268* The method :meth:`setboundary` was renamed to :meth:`set_boundary`.
269
270* The method :meth:`getdecodedpayload` was removed. To get similar
271 functionality, pass the value 1 to the *decode* flag of the get_payload()
272 method.
273
274* The method :meth:`getpayloadastext` was removed. Similar functionality is
275 supported by the :class:`DecodedGenerator` class in the :mod:`email.generator`
276 module.
277
278* The method :meth:`getbodyastext` was removed. You can get similar
279 functionality by creating an iterator with :func:`typed_subpart_iterator` in the
280 :mod:`email.iterators` module.
281
282The :class:`Parser` class has no differences in its public interface. It does
283have some additional smarts to recognize :mimetype:`message/delivery-status`
284type messages, which it represents as a :class:`Message` instance containing
285separate :class:`Message` subparts for each header block in the delivery status
286notification [#]_.
287
288The :class:`Generator` class has no differences in its public interface. There
289is a new class in the :mod:`email.generator` module though, called
290:class:`DecodedGenerator` which provides most of the functionality previously
291available in the :meth:`Message.getpayloadastext` method.
292
293The following modules and classes have been changed:
294
295* The :class:`MIMEBase` class constructor arguments *_major* and *_minor* have
296 changed to *_maintype* and *_subtype* respectively.
297
298* The ``Image`` class/module has been renamed to ``MIMEImage``. The *_minor*
299 argument has been renamed to *_subtype*.
300
301* The ``Text`` class/module has been renamed to ``MIMEText``. The *_minor*
302 argument has been renamed to *_subtype*.
303
304* The ``MessageRFC822`` class/module has been renamed to ``MIMEMessage``. Note
305 that an earlier version of :mod:`mimelib` called this class/module ``RFC822``,
306 but that clashed with the Python standard library module :mod:`rfc822` on some
307 case-insensitive file systems.
308
309 Also, the :class:`MIMEMessage` class now represents any kind of MIME message
310 with main type :mimetype:`message`. It takes an optional argument *_subtype*
311 which is used to set the MIME subtype. *_subtype* defaults to
312 :mimetype:`rfc822`.
313
314:mod:`mimelib` provided some utility functions in its :mod:`address` and
315:mod:`date` modules. All of these functions have been moved to the
316:mod:`email.utils` module.
317
318The ``MsgReader`` class/module has been removed. Its functionality is most
319closely supported in the :func:`body_line_iterator` function in the
320:mod:`email.iterators` module.
321
322.. rubric:: Footnotes
323
324.. [#] Delivery Status Notifications (DSN) are defined in :rfc:`1894`.