17db96d56Sopenharmony_ci:mod:`email` --- An email and MIME handling package 27db96d56Sopenharmony_ci=================================================== 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: email 57db96d56Sopenharmony_ci :synopsis: Package supporting the parsing, manipulating, and generating 67db96d56Sopenharmony_ci email messages. 77db96d56Sopenharmony_ci.. moduleauthor:: Barry A. Warsaw <barry@python.org>, 87db96d56Sopenharmony_ci R. David Murray <rdmurray@bitdance.com> 97db96d56Sopenharmony_ci.. sectionauthor:: R. David Murray <rdmurray@bitdance.com> 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ci**Source code:** :source:`Lib/email/__init__.py` 127db96d56Sopenharmony_ci 137db96d56Sopenharmony_ci-------------- 147db96d56Sopenharmony_ci 157db96d56Sopenharmony_ciThe :mod:`email` package is a library for managing email messages. It is 167db96d56Sopenharmony_cispecifically *not* designed to do any sending of email messages to SMTP 177db96d56Sopenharmony_ci(:rfc:`2821`), NNTP, or other servers; those are functions of modules such as 187db96d56Sopenharmony_ci:mod:`smtplib` and :mod:`nntplib`. The :mod:`email` package attempts to be as 197db96d56Sopenharmony_ciRFC-compliant as possible, supporting :rfc:`5322` and :rfc:`6532`, as well as 207db96d56Sopenharmony_cisuch MIME-related RFCs as :rfc:`2045`, :rfc:`2046`, :rfc:`2047`, :rfc:`2183`, 217db96d56Sopenharmony_ciand :rfc:`2231`. 227db96d56Sopenharmony_ci 237db96d56Sopenharmony_ciThe overall structure of the email package can be divided into three major 247db96d56Sopenharmony_cicomponents, plus a fourth component that controls the behavior of the other 257db96d56Sopenharmony_cicomponents. 267db96d56Sopenharmony_ci 277db96d56Sopenharmony_ciThe central component of the package is an "object model" that represents email 287db96d56Sopenharmony_cimessages. An application interacts with the package primarily through the 297db96d56Sopenharmony_ciobject model interface defined in the :mod:`~email.message` sub-module. The 307db96d56Sopenharmony_ciapplication can use this API to ask questions about an existing email, to 317db96d56Sopenharmony_ciconstruct a new email, or to add or remove email subcomponents that themselves 327db96d56Sopenharmony_ciuse the same object model interface. That is, following the nature of email 337db96d56Sopenharmony_cimessages and their MIME subcomponents, the email object model is a tree 347db96d56Sopenharmony_cistructure of objects that all provide the :class:`~email.message.EmailMessage` 357db96d56Sopenharmony_ciAPI. 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ciThe other two major components of the package are the :mod:`~email.parser` and 387db96d56Sopenharmony_cithe :mod:`~email.generator`. The parser takes the serialized version of an 397db96d56Sopenharmony_ciemail message (a stream of bytes) and converts it into a tree of 407db96d56Sopenharmony_ci:class:`~email.message.EmailMessage` objects. The generator takes an 417db96d56Sopenharmony_ci:class:`~email.message.EmailMessage` and turns it back into a serialized byte 427db96d56Sopenharmony_cistream. (The parser and generator also handle streams of text characters, but 437db96d56Sopenharmony_cithis usage is discouraged as it is too easy to end up with messages that are 447db96d56Sopenharmony_cinot valid in one way or another.) 457db96d56Sopenharmony_ci 467db96d56Sopenharmony_ciThe control component is the :mod:`~email.policy` module. Every 477db96d56Sopenharmony_ci:class:`~email.message.EmailMessage`, every :mod:`~email.generator`, and every 487db96d56Sopenharmony_ci:mod:`~email.parser` has an associated :mod:`~email.policy` object that 497db96d56Sopenharmony_cicontrols its behavior. Usually an application only needs to specify the policy 507db96d56Sopenharmony_ciwhen an :class:`~email.message.EmailMessage` is created, either by directly 517db96d56Sopenharmony_ciinstantiating an :class:`~email.message.EmailMessage` to create a new email, 527db96d56Sopenharmony_cior by parsing an input stream using a :mod:`~email.parser`. But the policy can 537db96d56Sopenharmony_cibe changed when the message is serialized using a :mod:`~email.generator`. 547db96d56Sopenharmony_ciThis allows, for example, a generic email message to be parsed from disk, but 557db96d56Sopenharmony_cito serialize it using standard SMTP settings when sending it to an email 567db96d56Sopenharmony_ciserver. 577db96d56Sopenharmony_ci 587db96d56Sopenharmony_ciThe email package does its best to hide the details of the various governing 597db96d56Sopenharmony_ciRFCs from the application. Conceptually the application should be able to 607db96d56Sopenharmony_citreat the email message as a structured tree of unicode text and binary 617db96d56Sopenharmony_ciattachments, without having to worry about how these are represented when 627db96d56Sopenharmony_ciserialized. In practice, however, it is often necessary to be aware of at 637db96d56Sopenharmony_cileast some of the rules governing MIME messages and their structure, 647db96d56Sopenharmony_cispecifically the names and nature of the MIME "content types" and how they 657db96d56Sopenharmony_ciidentify multipart documents. For the most part this knowledge should only be 667db96d56Sopenharmony_cirequired for more complex applications, and even then it should only be the 677db96d56Sopenharmony_cihigh level structure in question, and not the details of how those structures 687db96d56Sopenharmony_ciare represented. Since MIME content types are used widely in modern internet 697db96d56Sopenharmony_cisoftware (not just email), this will be a familiar concept to many programmers. 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ciThe following sections describe the functionality of the :mod:`email` package. 727db96d56Sopenharmony_ciWe start with the :mod:`~email.message` object model, which is the primary 737db96d56Sopenharmony_ciinterface an application will use, and follow that with the 747db96d56Sopenharmony_ci:mod:`~email.parser` and :mod:`~email.generator` components. Then we cover the 757db96d56Sopenharmony_ci:mod:`~email.policy` controls, which completes the treatment of the main 767db96d56Sopenharmony_cicomponents of the library. 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ciThe next three sections cover the exceptions the package may raise and the 797db96d56Sopenharmony_cidefects (non-compliance with the RFCs) that the :mod:`~email.parser` may 807db96d56Sopenharmony_cidetect. Then we cover the :mod:`~email.headerregistry` and the 817db96d56Sopenharmony_ci:mod:`~email.contentmanager` sub-components, which provide tools for doing more 827db96d56Sopenharmony_cidetailed manipulation of headers and payloads, respectively. Both of these 837db96d56Sopenharmony_cicomponents contain features relevant to consuming and producing non-trivial 847db96d56Sopenharmony_cimessages, but also document their extensibility APIs, which will be of interest 857db96d56Sopenharmony_cito advanced applications. 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ciFollowing those is a set of examples of using the fundamental parts of the APIs 887db96d56Sopenharmony_cicovered in the preceding sections. 897db96d56Sopenharmony_ci 907db96d56Sopenharmony_ciThe foregoing represent the modern (unicode friendly) API of the email package. 917db96d56Sopenharmony_ciThe remaining sections, starting with the :class:`~email.message.Message` 927db96d56Sopenharmony_ciclass, cover the legacy :data:`~email.policy.compat32` API that deals much more 937db96d56Sopenharmony_cidirectly with the details of how email messages are represented. The 947db96d56Sopenharmony_ci:data:`~email.policy.compat32` API does *not* hide the details of the RFCs from 957db96d56Sopenharmony_cithe application, but for applications that need to operate at that level, they 967db96d56Sopenharmony_cican be useful tools. This documentation is also relevant for applications that 977db96d56Sopenharmony_ciare still using the :mod:`~email.policy.compat32` API for backward 987db96d56Sopenharmony_cicompatibility reasons. 997db96d56Sopenharmony_ci 1007db96d56Sopenharmony_ci.. versionchanged:: 3.6 1017db96d56Sopenharmony_ci Docs reorganized and rewritten to promote the new 1027db96d56Sopenharmony_ci :class:`~email.message.EmailMessage`/:class:`~email.policy.EmailPolicy` 1037db96d56Sopenharmony_ci API. 1047db96d56Sopenharmony_ci 1057db96d56Sopenharmony_ciContents of the :mod:`email` package documentation: 1067db96d56Sopenharmony_ci 1077db96d56Sopenharmony_ci.. toctree:: 1087db96d56Sopenharmony_ci 1097db96d56Sopenharmony_ci email.message.rst 1107db96d56Sopenharmony_ci email.parser.rst 1117db96d56Sopenharmony_ci email.generator.rst 1127db96d56Sopenharmony_ci email.policy.rst 1137db96d56Sopenharmony_ci 1147db96d56Sopenharmony_ci email.errors.rst 1157db96d56Sopenharmony_ci email.headerregistry.rst 1167db96d56Sopenharmony_ci email.contentmanager.rst 1177db96d56Sopenharmony_ci 1187db96d56Sopenharmony_ci email.examples.rst 1197db96d56Sopenharmony_ci 1207db96d56Sopenharmony_ciLegacy API: 1217db96d56Sopenharmony_ci 1227db96d56Sopenharmony_ci.. toctree:: 1237db96d56Sopenharmony_ci 1247db96d56Sopenharmony_ci email.compat32-message.rst 1257db96d56Sopenharmony_ci email.mime.rst 1267db96d56Sopenharmony_ci email.header.rst 1277db96d56Sopenharmony_ci email.charset.rst 1287db96d56Sopenharmony_ci email.encoders.rst 1297db96d56Sopenharmony_ci email.utils.rst 1307db96d56Sopenharmony_ci email.iterators.rst 1317db96d56Sopenharmony_ci 1327db96d56Sopenharmony_ci 1337db96d56Sopenharmony_ci.. seealso:: 1347db96d56Sopenharmony_ci 1357db96d56Sopenharmony_ci Module :mod:`smtplib` 1367db96d56Sopenharmony_ci SMTP (Simple Mail Transport Protocol) client 1377db96d56Sopenharmony_ci 1387db96d56Sopenharmony_ci Module :mod:`poplib` 1397db96d56Sopenharmony_ci POP (Post Office Protocol) client 1407db96d56Sopenharmony_ci 1417db96d56Sopenharmony_ci Module :mod:`imaplib` 1427db96d56Sopenharmony_ci IMAP (Internet Message Access Protocol) client 1437db96d56Sopenharmony_ci 1447db96d56Sopenharmony_ci Module :mod:`nntplib` 1457db96d56Sopenharmony_ci NNTP (Net News Transport Protocol) client 1467db96d56Sopenharmony_ci 1477db96d56Sopenharmony_ci Module :mod:`mailbox` 1487db96d56Sopenharmony_ci Tools for creating, reading, and managing collections of messages on disk 1497db96d56Sopenharmony_ci using a variety standard formats. 1507db96d56Sopenharmony_ci 1517db96d56Sopenharmony_ci Module :mod:`smtpd` 1527db96d56Sopenharmony_ci SMTP server framework (primarily useful for testing) 153