blob: 331d2ef0e4b2d6f7262ab5a3eb3380d27507082c [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`email` --- An email and MIME handling package
2===================================================
3
4.. module:: email
Georg Brandl3f076d82009-05-17 11:28:33 +00005 :synopsis: Package supporting the parsing, manipulating, and generating
6 email messages, including MIME documents.
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Barry A. Warsaw <barry@python.org>
8.. sectionauthor:: Barry A. Warsaw <barry@python.org>
R. David Murray96fd54e2010-10-08 15:55:28 +00009.. Copyright (C) 2001-2010 Python Software Foundation
Georg Brandl116aa622007-08-15 14:28:22 +000010
11
Georg Brandl116aa622007-08-15 14:28:22 +000012The :mod:`email` package is a library for managing email messages, including
Georg Brandl83e9f4c2008-06-12 18:52:31 +000013MIME and other :rfc:`2822`\ -based message documents. It is specifically *not*
14designed to do any sending of email messages to SMTP (:rfc:`2821`), NNTP, or
15other servers; those are functions of modules such as :mod:`smtplib` and
16:mod:`nntplib`. The :mod:`email` package attempts to be as RFC-compliant as
17possible, supporting in addition to :rfc:`2822`, such MIME-related RFCs as
18:rfc:`2045`, :rfc:`2046`, :rfc:`2047`, and :rfc:`2231`.
Georg Brandl116aa622007-08-15 14:28:22 +000019
20The primary distinguishing feature of the :mod:`email` package is that it splits
21the parsing and generating of email messages from the internal *object model*
22representation of email. Applications using the :mod:`email` package deal
23primarily with objects; you can add sub-objects to messages, remove sub-objects
24from messages, completely re-arrange the contents, etc. There is a separate
25parser and a separate generator which handles the transformation from flat text
26to the object model, and then back to flat text again. There are also handy
27subclasses for some common MIME object types, and a few miscellaneous utilities
28that help with such common tasks as extracting and parsing message field values,
29creating RFC-compliant dates, etc.
30
31The following sections describe the functionality of the :mod:`email` package.
32The ordering follows a progression that should be common in applications: an
33email message is read as flat text from a file or other source, the text is
34parsed to produce the object structure of the email message, this structure is
35manipulated, and finally, the object tree is rendered back into flat text.
36
37It is perfectly feasible to create the object structure out of whole cloth ---
38i.e. completely from scratch. From there, a similar progression can be taken as
39above.
40
41Also included are detailed specifications of all the classes and modules that
42the :mod:`email` package provides, the exception classes you might encounter
43while using the :mod:`email` package, some auxiliary utilities, and a few
44examples. For users of the older :mod:`mimelib` package, or previous versions
45of the :mod:`email` package, a section on differences and porting is provided.
46
47Contents of the :mod:`email` package documentation:
48
49.. toctree::
50
51 email.message.rst
52 email.parser.rst
53 email.generator.rst
Georg Brandle8d2d2d2011-04-19 09:21:00 +020054 email.policy.rst
R David Murray79cf3ba2012-05-27 17:10:36 -040055 email.headerregistry.rst
R David Murray3da240f2013-10-16 22:48:40 -040056 email.contentmanager.rst
Georg Brandl116aa622007-08-15 14:28:22 +000057 email.mime.rst
58 email.header.rst
59 email.charset.rst
60 email.encoders.rst
61 email.errors.rst
62 email.util.rst
63 email.iterators.rst
64 email-examples.rst
65
66
67.. seealso::
68
69 Module :mod:`smtplib`
70 SMTP protocol client
71
72 Module :mod:`nntplib`
73 NNTP protocol client
74
75
76.. _email-pkg-history:
77
78Package History
79---------------
80
81This table describes the release history of the email package, corresponding to
82the version of Python that the package was released with. For purposes of this
83document, when you see a note about change or added versions, these refer to the
84Python version the change was made in, *not* the email package version. This
85table also describes the Python compatibility of each version of the package.
86
87+---------------+------------------------------+-----------------------+
88| email version | distributed with | compatible with |
89+===============+==============================+=======================+
90| :const:`1.x` | Python 2.2.0 to Python 2.2.1 | *no longer supported* |
91+---------------+------------------------------+-----------------------+
92| :const:`2.5` | Python 2.2.2+ and Python 2.3 | Python 2.1 to 2.5 |
93+---------------+------------------------------+-----------------------+
94| :const:`3.0` | Python 2.4 | Python 2.3 to 2.5 |
95+---------------+------------------------------+-----------------------+
96| :const:`4.0` | Python 2.5 | Python 2.3 to 2.5 |
97+---------------+------------------------------+-----------------------+
R. David Murray96fd54e2010-10-08 15:55:28 +000098| :const:`5.0` | Python 3.0 and Python 3.1 | Python 3.0 to 3.2 |
99+---------------+------------------------------+-----------------------+
100| :const:`5.1` | Python 3.2 | Python 3.0 to 3.2 |
101+---------------+------------------------------+-----------------------+
102
103Here are the major differences between :mod:`email` version 5.1 and
104version 5.0:
105
106* It is once again possible to parse messages containing non-ASCII bytes,
107 and to reproduce such messages if the data containing the non-ASCII
108 bytes is not modified.
109
110* New functions :func:`message_from_bytes` and :func:`message_from_binary_file`,
111 and new classes :class:`~email.parser.BytesFeedParser` and
112 :class:`~email.parser.BytesParser` allow binary message data to be parsed
113 into model objects.
114
115* Given bytes input to the model, :meth:`~email.message.Message.get_payload`
116 will by default decode a message body that has a
Senthil Kumaran916bd382010-10-15 12:55:19 +0000117 :mailheader:`Content-Transfer-Encoding` of ``8bit`` using the charset
118 specified in the MIME headers and return the resulting string.
R. David Murray96fd54e2010-10-08 15:55:28 +0000119
120* Given bytes input to the model, :class:`~email.generator.Generator` will
121 convert message bodies that have a :mailheader:`Content-Transfer-Encoding` of
122 8bit to instead have a 7bit Content-Transfer-Encoding.
123
R. David Murray7c0a2272010-10-08 21:37:39 +0000124* New class :class:`~email.generator.BytesGenerator` produces bytes
R. David Murray96fd54e2010-10-08 15:55:28 +0000125 as output, preserving any unchanged non-ASCII data that was
126 present in the input used to build the model, including message bodies
127 with a :mailheader:`Content-Transfer-Encoding` of 8bit.
128
129Here are the major differences between :mod:`email` version 5.0 and version 4:
130
131* All operations are on unicode strings. Text inputs must be strings,
132 text outputs are strings. Outputs are limited to the ASCII character
133 set and so can be encoded to ASCII for transmission. Inputs are also
134 limited to ASCII; this is an acknowledged limitation of email 5.0 and
135 means it can only be used to parse email that is 7bit clean.
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137Here are the major differences between :mod:`email` version 4 and version 3:
138
139* All modules have been renamed according to :pep:`8` standards. For example,
140 the version 3 module :mod:`email.Message` was renamed to :mod:`email.message` in
141 version 4.
142
143* A new subpackage :mod:`email.mime` was added and all the version 3
144 :mod:`email.MIME\*` modules were renamed and situated into the :mod:`email.mime`
145 subpackage. For example, the version 3 module :mod:`email.MIMEText` was renamed
146 to :mod:`email.mime.text`.
147
148 *Note that the version 3 names will continue to work until Python 2.6*.
149
150* The :mod:`email.mime.application` module was added, which contains the
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300151 :class:`~email.mime.application.MIMEApplication` class.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153* Methods that were deprecated in version 3 have been removed. These include
154 :meth:`Generator.__call__`, :meth:`Message.get_type`,
155 :meth:`Message.get_main_type`, :meth:`Message.get_subtype`.
156
157* Fixes have been added for :rfc:`2231` support which can change some of the
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300158 return types for :func:`Message.get_param <email.message.Message.get_param>`
159 and friends. Under some
Georg Brandl116aa622007-08-15 14:28:22 +0000160 circumstances, values which used to return a 3-tuple now return simple strings
161 (specifically, if all extended parameter segments were unencoded, there is no
162 language and charset designation expected, so the return type is now a simple
163 string). Also, %-decoding used to be done for both encoded and unencoded
164 segments; this decoding is now done only for encoded segments.
165
166Here are the major differences between :mod:`email` version 3 and version 2:
167
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300168* The :class:`~email.parser.FeedParser` class was introduced, and the
169 :class:`~email.parser.Parser` class was implemented in terms of the
170 :class:`~email.parser.FeedParser`. All parsing therefore is
Georg Brandl116aa622007-08-15 14:28:22 +0000171 non-strict, and parsing will make a best effort never to raise an exception.
172 Problems found while parsing messages are stored in the message's *defect*
173 attribute.
174
175* All aspects of the API which raised :exc:`DeprecationWarning`\ s in version 2
176 have been removed. These include the *_encoder* argument to the
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300177 :class:`~email.mime.text.MIMEText` constructor, the
178 :meth:`Message.add_payload` method, the :func:`Utils.dump_address_pair`
179 function, and the functions :func:`Utils.decode` and :func:`Utils.encode`.
Georg Brandl116aa622007-08-15 14:28:22 +0000180
181* New :exc:`DeprecationWarning`\ s have been added to:
182 :meth:`Generator.__call__`, :meth:`Message.get_type`,
183 :meth:`Message.get_main_type`, :meth:`Message.get_subtype`, and the *strict*
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300184 argument to the :class:`~email.parser.Parser` class. These are expected to
185 be removed in future versions.
Georg Brandl116aa622007-08-15 14:28:22 +0000186
187* Support for Pythons earlier than 2.3 has been removed.
188
189Here are the differences between :mod:`email` version 2 and version 1:
190
191* The :mod:`email.Header` and :mod:`email.Charset` modules have been added.
192
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300193* The pickle format for :class:`~email.message.Message` instances has changed.
194 Since this was never (and still isn't) formally defined, this isn't
195 considered a backward incompatibility. However if your application pickles
196 and unpickles :class:`~email.message.Message` instances, be aware that in
197 :mod:`email` version 2, :class:`~email.message.Message` instances now have
198 private variables *_charset* and *_default_type*.
Georg Brandl116aa622007-08-15 14:28:22 +0000199
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300200* Several methods in the :class:`~email.message.Message` class have been
201 deprecated, or their signatures changed. Also, many new methods have been
202 added. See the documentation for the :class:`~email.message.Message` class
203 for details. The changes should be completely backward compatible.
Georg Brandl116aa622007-08-15 14:28:22 +0000204
205* The object structure has changed in the face of :mimetype:`message/rfc822`
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300206 content types. In :mod:`email` version 1, such a type would be represented
207 by a scalar payload, i.e. the container message's
208 :meth:`~email.message.Message.is_multipart` returned false,
209 :meth:`~email.message.Message.get_payload` was not a list object, but a
210 single :class:`~email.message.Message` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000211
212 This structure was inconsistent with the rest of the package, so the object
213 representation for :mimetype:`message/rfc822` content types was changed. In
214 :mod:`email` version 2, the container *does* return ``True`` from
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300215 :meth:`~email.message.Message.is_multipart`, and
216 :meth:`~email.message.Message.get_payload` returns a list containing a single
217 :class:`~email.message.Message` item.
Georg Brandl116aa622007-08-15 14:28:22 +0000218
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300219 Note that this is one place that backward compatibility could not be
220 completely maintained. However, if you're already testing the return type of
221 :meth:`~email.message.Message.get_payload`, you should be fine. You just need
222 to make sure your code doesn't do a :meth:`~email.message.Message.set_payload`
223 with a :class:`~email.message.Message` instance on a container with a content
224 type of :mimetype:`message/rfc822`.
Georg Brandl116aa622007-08-15 14:28:22 +0000225
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300226* The :class:`~email.parser.Parser` constructor's *strict* argument was added,
227 and its :meth:`~email.parser.Parser.parse` and
228 :meth:`~email.parser.Parser.parsestr` methods grew a *headersonly* argument.
229 The *strict* flag was also added to functions :func:`email.message_from_file`
230 and :func:`email.message_from_string`.
Georg Brandl116aa622007-08-15 14:28:22 +0000231
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300232* :meth:`Generator.__call__` is deprecated; use :meth:`Generator.flatten
233 <email.generator.Generator.flatten>` instead. The
234 :class:`~email.generator.Generator` class has also grown the
235 :meth:`~email.generator.Generator.clone` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000236
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300237* The :class:`~email.generator.DecodedGenerator` class in the
238 :mod:`email.generator` module was added.
Georg Brandl116aa622007-08-15 14:28:22 +0000239
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300240* The intermediate base classes
241 :class:`~email.mime.nonmultipart.MIMENonMultipart` and
242 :class:`~email.mime.multipart.MIMEMultipart` have been added, and interposed
243 in the class hierarchy for most of the other MIME-related derived classes.
Georg Brandl116aa622007-08-15 14:28:22 +0000244
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300245* The *_encoder* argument to the :class:`~email.mime.text.MIMEText` constructor
246 has been deprecated. Encoding now happens implicitly based on the
247 *_charset* argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000248
249* The following functions in the :mod:`email.Utils` module have been deprecated:
250 :func:`dump_address_pairs`, :func:`decode`, and :func:`encode`. The following
251 functions have been added to the module: :func:`make_msgid`,
252 :func:`decode_rfc2231`, :func:`encode_rfc2231`, and :func:`decode_params`.
253
254* The non-public function :func:`email.Iterators._structure` was added.
255
256
257Differences from :mod:`mimelib`
258-------------------------------
259
260The :mod:`email` package was originally prototyped as a separate library called
261`mimelib <http://mimelib.sf.net/>`_. Changes have been made so that method names
262are more consistent, and some methods or modules have either been added or
263removed. The semantics of some of the methods have also changed. For the most
264part, any functionality available in :mod:`mimelib` is still available in the
265:mod:`email` package, albeit often in a different way. Backward compatibility
266between the :mod:`mimelib` package and the :mod:`email` package was not a
267priority.
268
269Here is a brief description of the differences between the :mod:`mimelib` and
270the :mod:`email` packages, along with hints on how to port your applications.
271
272Of course, the most visible difference between the two packages is that the
273package name has been changed to :mod:`email`. In addition, the top-level
274package has the following differences:
275
276* :func:`messageFromString` has been renamed to :func:`message_from_string`.
277
278* :func:`messageFromFile` has been renamed to :func:`message_from_file`.
279
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300280The :class:`~email.message.Message` class has the following differences:
Georg Brandl116aa622007-08-15 14:28:22 +0000281
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300282* The method :meth:`asString` was renamed to
283 :meth:`~email.message.Message.as_string`.
Georg Brandl116aa622007-08-15 14:28:22 +0000284
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300285* The method :meth:`ismultipart` was renamed to
286 :meth:`~email.message.Message.is_multipart`.
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300288* The :meth:`~email.message.Message.get_payload` method has grown a *decode*
289 optional argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000290
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300291* The method :meth:`getall` was renamed to
292 :meth:`~email.message.Message.get_all`.
Georg Brandl116aa622007-08-15 14:28:22 +0000293
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300294* The method :meth:`addheader` was renamed to
295 :meth:`~email.message.Message.add_header`.
Georg Brandl116aa622007-08-15 14:28:22 +0000296
297* The method :meth:`gettype` was renamed to :meth:`get_type`.
298
299* The method :meth:`getmaintype` was renamed to :meth:`get_main_type`.
300
301* The method :meth:`getsubtype` was renamed to :meth:`get_subtype`.
302
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300303* The method :meth:`getparams` was renamed to
304 :meth:`~email.message.Message.get_params`. Also, whereas :meth:`getparams`
305 returned a list of strings, :meth:`~email.message.Message.get_params` returns
306 a list of 2-tuples, effectively the key/value pairs of the parameters, split
307 on the ``'='`` sign.
Georg Brandl116aa622007-08-15 14:28:22 +0000308
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300309* The method :meth:`getparam` was renamed to
310 :meth:`~email.message.Message.get_param`.
Georg Brandl116aa622007-08-15 14:28:22 +0000311
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300312* The method :meth:`getcharsets` was renamed to
313 :meth:`~email.message.Message.get_charsets`.
Georg Brandl116aa622007-08-15 14:28:22 +0000314
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300315* The method :meth:`getfilename` was renamed to
316 :meth:`~email.message.Message.get_filename`.
Georg Brandl116aa622007-08-15 14:28:22 +0000317
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300318* The method :meth:`getboundary` was renamed to
319 :meth:`~email.message.Message.get_boundary`.
Georg Brandl116aa622007-08-15 14:28:22 +0000320
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300321* The method :meth:`setboundary` was renamed to
322 :meth:`~email.message.Message.set_boundary`.
Georg Brandl116aa622007-08-15 14:28:22 +0000323
324* The method :meth:`getdecodedpayload` was removed. To get similar
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300325 functionality, pass the value 1 to the *decode* flag of the
326 :meth:`~email.message.Message.get_payload` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000327
328* The method :meth:`getpayloadastext` was removed. Similar functionality is
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300329 supported by the :class:`~email.generator.DecodedGenerator` class in the
330 :mod:`email.generator` module.
Georg Brandl116aa622007-08-15 14:28:22 +0000331
332* The method :meth:`getbodyastext` was removed. You can get similar
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300333 functionality by creating an iterator with
334 :func:`~email.iterators.typed_subpart_iterator` in the :mod:`email.iterators`
335 module.
Georg Brandl116aa622007-08-15 14:28:22 +0000336
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300337The :class:`~email.parser.Parser` class has no differences in its public
338interface. It does have some additional smarts to recognize
339:mimetype:`message/delivery-status` type messages, which it represents as a
340:class:`~email.message.Message` instance containing separate
341:class:`~email.message.Message` subparts for each header block in the delivery
342status notification [#]_.
Georg Brandl116aa622007-08-15 14:28:22 +0000343
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300344The :class:`~email.generator.Generator` class has no differences in its public
345interface. There is a new class in the :mod:`email.generator` module though,
346called :class:`~email.generator.DecodedGenerator` which provides most of the
347functionality previously available in the :meth:`Message.getpayloadastext`
348method.
Georg Brandl116aa622007-08-15 14:28:22 +0000349
350The following modules and classes have been changed:
351
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300352* The :class:`~email.mime.base.MIMEBase` class constructor arguments *_major*
353 and *_minor* have changed to *_maintype* and *_subtype* respectively.
Georg Brandl116aa622007-08-15 14:28:22 +0000354
355* The ``Image`` class/module has been renamed to ``MIMEImage``. The *_minor*
356 argument has been renamed to *_subtype*.
357
358* The ``Text`` class/module has been renamed to ``MIMEText``. The *_minor*
359 argument has been renamed to *_subtype*.
360
361* The ``MessageRFC822`` class/module has been renamed to ``MIMEMessage``. Note
362 that an earlier version of :mod:`mimelib` called this class/module ``RFC822``,
363 but that clashed with the Python standard library module :mod:`rfc822` on some
364 case-insensitive file systems.
365
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300366 Also, the :class:`~email.mime.message.MIMEMessage` class now represents any
367 kind of MIME message
Georg Brandl116aa622007-08-15 14:28:22 +0000368 with main type :mimetype:`message`. It takes an optional argument *_subtype*
369 which is used to set the MIME subtype. *_subtype* defaults to
370 :mimetype:`rfc822`.
371
372:mod:`mimelib` provided some utility functions in its :mod:`address` and
373:mod:`date` modules. All of these functions have been moved to the
374:mod:`email.utils` module.
375
376The ``MsgReader`` class/module has been removed. Its functionality is most
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300377closely supported in the :func:`~email.iterators.body_line_iterator` function
378in the :mod:`email.iterators` module.
Georg Brandl116aa622007-08-15 14:28:22 +0000379
380.. rubric:: Footnotes
381
382.. [#] Delivery Status Notifications (DSN) are defined in :rfc:`1894`.