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