17db96d56Sopenharmony_ci:mod:`email.contentmanager`: Managing MIME Content 27db96d56Sopenharmony_ci-------------------------------------------------- 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: email.contentmanager 57db96d56Sopenharmony_ci :synopsis: Storing and Retrieving Content from MIME Parts 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. moduleauthor:: R. David Murray <rdmurray@bitdance.com> 87db96d56Sopenharmony_ci.. sectionauthor:: R. David Murray <rdmurray@bitdance.com> 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ci**Source code:** :source:`Lib/email/contentmanager.py` 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci------------ 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ci.. versionadded:: 3.6 [1]_ 157db96d56Sopenharmony_ci 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ci.. class:: ContentManager() 187db96d56Sopenharmony_ci 197db96d56Sopenharmony_ci Base class for content managers. Provides the standard registry mechanisms 207db96d56Sopenharmony_ci to register converters between MIME content and other representations, as 217db96d56Sopenharmony_ci well as the ``get_content`` and ``set_content`` dispatch methods. 227db96d56Sopenharmony_ci 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ci .. method:: get_content(msg, *args, **kw) 257db96d56Sopenharmony_ci 267db96d56Sopenharmony_ci Look up a handler function based on the ``mimetype`` of *msg* (see next 277db96d56Sopenharmony_ci paragraph), call it, passing through all arguments, and return the result 287db96d56Sopenharmony_ci of the call. The expectation is that the handler will extract the 297db96d56Sopenharmony_ci payload from *msg* and return an object that encodes information about 307db96d56Sopenharmony_ci the extracted data. 317db96d56Sopenharmony_ci 327db96d56Sopenharmony_ci To find the handler, look for the following keys in the registry, 337db96d56Sopenharmony_ci stopping with the first one found: 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ci * the string representing the full MIME type (``maintype/subtype``) 367db96d56Sopenharmony_ci * the string representing the ``maintype`` 377db96d56Sopenharmony_ci * the empty string 387db96d56Sopenharmony_ci 397db96d56Sopenharmony_ci If none of these keys produce a handler, raise a :exc:`KeyError` for the 407db96d56Sopenharmony_ci full MIME type. 417db96d56Sopenharmony_ci 427db96d56Sopenharmony_ci 437db96d56Sopenharmony_ci .. method:: set_content(msg, obj, *args, **kw) 447db96d56Sopenharmony_ci 457db96d56Sopenharmony_ci If the ``maintype`` is ``multipart``, raise a :exc:`TypeError`; otherwise 467db96d56Sopenharmony_ci look up a handler function based on the type of *obj* (see next 477db96d56Sopenharmony_ci paragraph), call :meth:`~email.message.EmailMessage.clear_content` on the 487db96d56Sopenharmony_ci *msg*, and call the handler function, passing through all arguments. The 497db96d56Sopenharmony_ci expectation is that the handler will transform and store *obj* into 507db96d56Sopenharmony_ci *msg*, possibly making other changes to *msg* as well, such as adding 517db96d56Sopenharmony_ci various MIME headers to encode information needed to interpret the stored 527db96d56Sopenharmony_ci data. 537db96d56Sopenharmony_ci 547db96d56Sopenharmony_ci To find the handler, obtain the type of *obj* (``typ = type(obj)``), and 557db96d56Sopenharmony_ci look for the following keys in the registry, stopping with the first one 567db96d56Sopenharmony_ci found: 577db96d56Sopenharmony_ci 587db96d56Sopenharmony_ci * the type itself (``typ``) 597db96d56Sopenharmony_ci * the type's fully qualified name (``typ.__module__ + '.' + 607db96d56Sopenharmony_ci typ.__qualname__``). 617db96d56Sopenharmony_ci * the type's qualname (``typ.__qualname__``) 627db96d56Sopenharmony_ci * the type's name (``typ.__name__``). 637db96d56Sopenharmony_ci 647db96d56Sopenharmony_ci If none of the above match, repeat all of the checks above for each of 657db96d56Sopenharmony_ci the types in the :term:`MRO` (``typ.__mro__``). Finally, if no other key 667db96d56Sopenharmony_ci yields a handler, check for a handler for the key ``None``. If there is 677db96d56Sopenharmony_ci no handler for ``None``, raise a :exc:`KeyError` for the fully 687db96d56Sopenharmony_ci qualified name of the type. 697db96d56Sopenharmony_ci 707db96d56Sopenharmony_ci Also add a :mailheader:`MIME-Version` header if one is not present (see 717db96d56Sopenharmony_ci also :class:`.MIMEPart`). 727db96d56Sopenharmony_ci 737db96d56Sopenharmony_ci 747db96d56Sopenharmony_ci .. method:: add_get_handler(key, handler) 757db96d56Sopenharmony_ci 767db96d56Sopenharmony_ci Record the function *handler* as the handler for *key*. For the possible 777db96d56Sopenharmony_ci values of *key*, see :meth:`get_content`. 787db96d56Sopenharmony_ci 797db96d56Sopenharmony_ci 807db96d56Sopenharmony_ci .. method:: add_set_handler(typekey, handler) 817db96d56Sopenharmony_ci 827db96d56Sopenharmony_ci Record *handler* as the function to call when an object of a type 837db96d56Sopenharmony_ci matching *typekey* is passed to :meth:`set_content`. For the possible 847db96d56Sopenharmony_ci values of *typekey*, see :meth:`set_content`. 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ciContent Manager Instances 887db96d56Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~ 897db96d56Sopenharmony_ci 907db96d56Sopenharmony_ciCurrently the email package provides only one concrete content manager, 917db96d56Sopenharmony_ci:data:`raw_data_manager`, although more may be added in the future. 927db96d56Sopenharmony_ci:data:`raw_data_manager` is the 937db96d56Sopenharmony_ci:attr:`~email.policy.EmailPolicy.content_manager` provided by 947db96d56Sopenharmony_ci:attr:`~email.policy.EmailPolicy` and its derivatives. 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ci 977db96d56Sopenharmony_ci.. data:: raw_data_manager 987db96d56Sopenharmony_ci 997db96d56Sopenharmony_ci This content manager provides only a minimum interface beyond that provided 1007db96d56Sopenharmony_ci by :class:`~email.message.Message` itself: it deals only with text, raw 1017db96d56Sopenharmony_ci byte strings, and :class:`~email.message.Message` objects. Nevertheless, it 1027db96d56Sopenharmony_ci provides significant advantages compared to the base API: ``get_content`` on 1037db96d56Sopenharmony_ci a text part will return a unicode string without the application needing to 1047db96d56Sopenharmony_ci manually decode it, ``set_content`` provides a rich set of options for 1057db96d56Sopenharmony_ci controlling the headers added to a part and controlling the content transfer 1067db96d56Sopenharmony_ci encoding, and it enables the use of the various ``add_`` methods, thereby 1077db96d56Sopenharmony_ci simplifying the creation of multipart messages. 1087db96d56Sopenharmony_ci 1097db96d56Sopenharmony_ci .. method:: get_content(msg, errors='replace') 1107db96d56Sopenharmony_ci 1117db96d56Sopenharmony_ci Return the payload of the part as either a string (for ``text`` parts), an 1127db96d56Sopenharmony_ci :class:`~email.message.EmailMessage` object (for ``message/rfc822`` 1137db96d56Sopenharmony_ci parts), or a ``bytes`` object (for all other non-multipart types). Raise 1147db96d56Sopenharmony_ci a :exc:`KeyError` if called on a ``multipart``. If the part is a 1157db96d56Sopenharmony_ci ``text`` part and *errors* is specified, use it as the error handler when 1167db96d56Sopenharmony_ci decoding the payload to unicode. The default error handler is 1177db96d56Sopenharmony_ci ``replace``. 1187db96d56Sopenharmony_ci 1197db96d56Sopenharmony_ci .. method:: set_content(msg, <'str'>, subtype="plain", charset='utf-8', \ 1207db96d56Sopenharmony_ci cte=None, \ 1217db96d56Sopenharmony_ci disposition=None, filename=None, cid=None, \ 1227db96d56Sopenharmony_ci params=None, headers=None) 1237db96d56Sopenharmony_ci set_content(msg, <'bytes'>, maintype, subtype, cte="base64", \ 1247db96d56Sopenharmony_ci disposition=None, filename=None, cid=None, \ 1257db96d56Sopenharmony_ci params=None, headers=None) 1267db96d56Sopenharmony_ci set_content(msg, <'EmailMessage'>, cte=None, \ 1277db96d56Sopenharmony_ci disposition=None, filename=None, cid=None, \ 1287db96d56Sopenharmony_ci params=None, headers=None) 1297db96d56Sopenharmony_ci 1307db96d56Sopenharmony_ci Add headers and payload to *msg*: 1317db96d56Sopenharmony_ci 1327db96d56Sopenharmony_ci Add a :mailheader:`Content-Type` header with a ``maintype/subtype`` 1337db96d56Sopenharmony_ci value. 1347db96d56Sopenharmony_ci 1357db96d56Sopenharmony_ci * For ``str``, set the MIME ``maintype`` to ``text``, and set the 1367db96d56Sopenharmony_ci subtype to *subtype* if it is specified, or ``plain`` if it is not. 1377db96d56Sopenharmony_ci * For ``bytes``, use the specified *maintype* and *subtype*, or 1387db96d56Sopenharmony_ci raise a :exc:`TypeError` if they are not specified. 1397db96d56Sopenharmony_ci * For :class:`~email.message.EmailMessage` objects, set the maintype 1407db96d56Sopenharmony_ci to ``message``, and set the subtype to *subtype* if it is 1417db96d56Sopenharmony_ci specified or ``rfc822`` if it is not. If *subtype* is 1427db96d56Sopenharmony_ci ``partial``, raise an error (``bytes`` objects must be used to 1437db96d56Sopenharmony_ci construct ``message/partial`` parts). 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ci If *charset* is provided (which is valid only for ``str``), encode the 1467db96d56Sopenharmony_ci string to bytes using the specified character set. The default is 1477db96d56Sopenharmony_ci ``utf-8``. If the specified *charset* is a known alias for a standard 1487db96d56Sopenharmony_ci MIME charset name, use the standard charset instead. 1497db96d56Sopenharmony_ci 1507db96d56Sopenharmony_ci If *cte* is set, encode the payload using the specified content transfer 1517db96d56Sopenharmony_ci encoding, and set the :mailheader:`Content-Transfer-Encoding` header to 1527db96d56Sopenharmony_ci that value. Possible values for *cte* are ``quoted-printable``, 1537db96d56Sopenharmony_ci ``base64``, ``7bit``, ``8bit``, and ``binary``. If the input cannot be 1547db96d56Sopenharmony_ci encoded in the specified encoding (for example, specifying a *cte* of 1557db96d56Sopenharmony_ci ``7bit`` for an input that contains non-ASCII values), raise a 1567db96d56Sopenharmony_ci :exc:`ValueError`. 1577db96d56Sopenharmony_ci 1587db96d56Sopenharmony_ci * For ``str`` objects, if *cte* is not set use heuristics to 1597db96d56Sopenharmony_ci determine the most compact encoding. 1607db96d56Sopenharmony_ci * For :class:`~email.message.EmailMessage`, per :rfc:`2046`, raise 1617db96d56Sopenharmony_ci an error if a *cte* of ``quoted-printable`` or ``base64`` is 1627db96d56Sopenharmony_ci requested for *subtype* ``rfc822``, and for any *cte* other than 1637db96d56Sopenharmony_ci ``7bit`` for *subtype* ``external-body``. For 1647db96d56Sopenharmony_ci ``message/rfc822``, use ``8bit`` if *cte* is not specified. For 1657db96d56Sopenharmony_ci all other values of *subtype*, use ``7bit``. 1667db96d56Sopenharmony_ci 1677db96d56Sopenharmony_ci .. note:: A *cte* of ``binary`` does not actually work correctly yet. 1687db96d56Sopenharmony_ci The ``EmailMessage`` object as modified by ``set_content`` is 1697db96d56Sopenharmony_ci correct, but :class:`~email.generator.BytesGenerator` does not 1707db96d56Sopenharmony_ci serialize it correctly. 1717db96d56Sopenharmony_ci 1727db96d56Sopenharmony_ci If *disposition* is set, use it as the value of the 1737db96d56Sopenharmony_ci :mailheader:`Content-Disposition` header. If not specified, and 1747db96d56Sopenharmony_ci *filename* is specified, add the header with the value ``attachment``. 1757db96d56Sopenharmony_ci If *disposition* is not specified and *filename* is also not specified, 1767db96d56Sopenharmony_ci do not add the header. The only valid values for *disposition* are 1777db96d56Sopenharmony_ci ``attachment`` and ``inline``. 1787db96d56Sopenharmony_ci 1797db96d56Sopenharmony_ci If *filename* is specified, use it as the value of the ``filename`` 1807db96d56Sopenharmony_ci parameter of the :mailheader:`Content-Disposition` header. 1817db96d56Sopenharmony_ci 1827db96d56Sopenharmony_ci If *cid* is specified, add a :mailheader:`Content-ID` header with 1837db96d56Sopenharmony_ci *cid* as its value. 1847db96d56Sopenharmony_ci 1857db96d56Sopenharmony_ci If *params* is specified, iterate its ``items`` method and use the 1867db96d56Sopenharmony_ci resulting ``(key, value)`` pairs to set additional parameters on the 1877db96d56Sopenharmony_ci :mailheader:`Content-Type` header. 1887db96d56Sopenharmony_ci 1897db96d56Sopenharmony_ci If *headers* is specified and is a list of strings of the form 1907db96d56Sopenharmony_ci ``headername: headervalue`` or a list of ``header`` objects 1917db96d56Sopenharmony_ci (distinguished from strings by having a ``name`` attribute), add the 1927db96d56Sopenharmony_ci headers to *msg*. 1937db96d56Sopenharmony_ci 1947db96d56Sopenharmony_ci 1957db96d56Sopenharmony_ci.. rubric:: Footnotes 1967db96d56Sopenharmony_ci 1977db96d56Sopenharmony_ci.. [1] Originally added in 3.4 as a :term:`provisional module <provisional 1987db96d56Sopenharmony_ci package>` 199