17db96d56Sopenharmony_ci:mod:`email.headerregistry`: Custom Header Objects
27db96d56Sopenharmony_ci--------------------------------------------------
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: email.headerregistry
57db96d56Sopenharmony_ci   :synopsis: Automatic Parsing of headers based on the field name
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/headerregistry.py`
117db96d56Sopenharmony_ci
127db96d56Sopenharmony_ci--------------
137db96d56Sopenharmony_ci
147db96d56Sopenharmony_ci.. versionadded:: 3.6 [1]_
157db96d56Sopenharmony_ci
167db96d56Sopenharmony_ciHeaders are represented by customized subclasses of :class:`str`.  The
177db96d56Sopenharmony_ciparticular class used to represent a given header is determined by the
187db96d56Sopenharmony_ci:attr:`~email.policy.EmailPolicy.header_factory` of the :mod:`~email.policy` in
197db96d56Sopenharmony_cieffect when the headers are created.  This section documents the particular
207db96d56Sopenharmony_ci``header_factory`` implemented by the email package for handling :RFC:`5322`
217db96d56Sopenharmony_cicompliant email messages, which not only provides customized header objects for
227db96d56Sopenharmony_civarious header types, but also provides an extension mechanism for applications
237db96d56Sopenharmony_cito add their own custom header types.
247db96d56Sopenharmony_ci
257db96d56Sopenharmony_ciWhen using any of the policy objects derived from
267db96d56Sopenharmony_ci:data:`~email.policy.EmailPolicy`, all headers are produced by
277db96d56Sopenharmony_ci:class:`.HeaderRegistry` and have :class:`.BaseHeader` as their last base
287db96d56Sopenharmony_ciclass.  Each header class has an additional base class that is determined by
297db96d56Sopenharmony_cithe type of the header.  For example, many headers have the class
307db96d56Sopenharmony_ci:class:`.UnstructuredHeader` as their other base class.  The specialized second
317db96d56Sopenharmony_ciclass for a header is determined by the name of the header, using a lookup
327db96d56Sopenharmony_citable stored in the :class:`.HeaderRegistry`.  All of this is managed
337db96d56Sopenharmony_citransparently for the typical application program, but interfaces are provided
347db96d56Sopenharmony_cifor modifying the default behavior for use by more complex applications.
357db96d56Sopenharmony_ci
367db96d56Sopenharmony_ciThe sections below first document the header base classes and their attributes,
377db96d56Sopenharmony_cifollowed by the API for modifying the behavior of :class:`.HeaderRegistry`, and
387db96d56Sopenharmony_cifinally the support classes used to represent the data parsed from structured
397db96d56Sopenharmony_ciheaders.
407db96d56Sopenharmony_ci
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci.. class:: BaseHeader(name, value)
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci   *name* and *value* are passed to ``BaseHeader`` from the
457db96d56Sopenharmony_ci   :attr:`~email.policy.EmailPolicy.header_factory` call.  The string value of
467db96d56Sopenharmony_ci   any header object is the *value* fully decoded to unicode.
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ci   This base class defines the following read-only properties:
497db96d56Sopenharmony_ci
507db96d56Sopenharmony_ci
517db96d56Sopenharmony_ci   .. attribute:: name
527db96d56Sopenharmony_ci
537db96d56Sopenharmony_ci      The name of the header (the portion of the field before the ':').  This
547db96d56Sopenharmony_ci      is exactly the value passed in the
557db96d56Sopenharmony_ci      :attr:`~email.policy.EmailPolicy.header_factory` call for *name*; that
567db96d56Sopenharmony_ci      is, case is preserved.
577db96d56Sopenharmony_ci
587db96d56Sopenharmony_ci
597db96d56Sopenharmony_ci   .. attribute:: defects
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ci      A tuple of :exc:`~email.errors.HeaderDefect` instances reporting any
627db96d56Sopenharmony_ci      RFC compliance problems found during parsing.  The email package tries to
637db96d56Sopenharmony_ci      be complete about detecting compliance issues.  See the :mod:`~email.errors`
647db96d56Sopenharmony_ci      module for a discussion of the types of defects that may be reported.
657db96d56Sopenharmony_ci
667db96d56Sopenharmony_ci
677db96d56Sopenharmony_ci   .. attribute:: max_count
687db96d56Sopenharmony_ci
697db96d56Sopenharmony_ci      The maximum number of headers of this type that can have the same
707db96d56Sopenharmony_ci      ``name``.  A value of ``None`` means unlimited.  The ``BaseHeader`` value
717db96d56Sopenharmony_ci      for this attribute is ``None``; it is expected that specialized header
727db96d56Sopenharmony_ci      classes will override this value as needed.
737db96d56Sopenharmony_ci
747db96d56Sopenharmony_ci   ``BaseHeader`` also provides the following method, which is called by the
757db96d56Sopenharmony_ci   email library code and should not in general be called by application
767db96d56Sopenharmony_ci   programs:
777db96d56Sopenharmony_ci
787db96d56Sopenharmony_ci   .. method:: fold(*, policy)
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci      Return a string containing :attr:`~email.policy.Policy.linesep`
817db96d56Sopenharmony_ci      characters as required to correctly fold the header according to
827db96d56Sopenharmony_ci      *policy*.  A :attr:`~email.policy.Policy.cte_type` of ``8bit`` will be
837db96d56Sopenharmony_ci      treated as if it were ``7bit``, since headers may not contain arbitrary
847db96d56Sopenharmony_ci      binary data.  If :attr:`~email.policy.EmailPolicy.utf8` is ``False``,
857db96d56Sopenharmony_ci      non-ASCII data will be :rfc:`2047` encoded.
867db96d56Sopenharmony_ci
877db96d56Sopenharmony_ci
887db96d56Sopenharmony_ci   ``BaseHeader`` by itself cannot be used to create a header object.  It
897db96d56Sopenharmony_ci   defines a protocol that each specialized header cooperates with in order to
907db96d56Sopenharmony_ci   produce the header object.  Specifically, ``BaseHeader`` requires that
917db96d56Sopenharmony_ci   the specialized class provide a :func:`classmethod` named ``parse``.  This
927db96d56Sopenharmony_ci   method is called as follows::
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ci       parse(string, kwds)
957db96d56Sopenharmony_ci
967db96d56Sopenharmony_ci   ``kwds`` is a dictionary containing one pre-initialized key, ``defects``.
977db96d56Sopenharmony_ci   ``defects`` is an empty list.  The parse method should append any detected
987db96d56Sopenharmony_ci   defects to this list.  On return, the ``kwds`` dictionary *must* contain
997db96d56Sopenharmony_ci   values for at least the keys ``decoded`` and ``defects``.  ``decoded``
1007db96d56Sopenharmony_ci   should be the string value for the header (that is, the header value fully
1017db96d56Sopenharmony_ci   decoded to unicode).  The parse method should assume that *string* may
1027db96d56Sopenharmony_ci   contain content-transfer-encoded parts, but should correctly handle all valid
1037db96d56Sopenharmony_ci   unicode characters as well so that it can parse un-encoded header values.
1047db96d56Sopenharmony_ci
1057db96d56Sopenharmony_ci   ``BaseHeader``'s ``__new__`` then creates the header instance, and calls its
1067db96d56Sopenharmony_ci   ``init`` method.  The specialized class only needs to provide an ``init``
1077db96d56Sopenharmony_ci   method if it wishes to set additional attributes beyond those provided by
1087db96d56Sopenharmony_ci   ``BaseHeader`` itself.  Such an ``init`` method should look like this::
1097db96d56Sopenharmony_ci
1107db96d56Sopenharmony_ci       def init(self, /, *args, **kw):
1117db96d56Sopenharmony_ci           self._myattr = kw.pop('myattr')
1127db96d56Sopenharmony_ci           super().init(*args, **kw)
1137db96d56Sopenharmony_ci
1147db96d56Sopenharmony_ci   That is, anything extra that the specialized class puts in to the ``kwds``
1157db96d56Sopenharmony_ci   dictionary should be removed and handled, and the remaining contents of
1167db96d56Sopenharmony_ci   ``kw`` (and ``args``) passed to the ``BaseHeader`` ``init`` method.
1177db96d56Sopenharmony_ci
1187db96d56Sopenharmony_ci
1197db96d56Sopenharmony_ci.. class:: UnstructuredHeader
1207db96d56Sopenharmony_ci
1217db96d56Sopenharmony_ci   An "unstructured" header is the default type of header in :rfc:`5322`.
1227db96d56Sopenharmony_ci   Any header that does not have a specified syntax is treated as
1237db96d56Sopenharmony_ci   unstructured.  The classic example of an unstructured header is the
1247db96d56Sopenharmony_ci   :mailheader:`Subject` header.
1257db96d56Sopenharmony_ci
1267db96d56Sopenharmony_ci   In :rfc:`5322`, an unstructured header is a run of arbitrary text in the
1277db96d56Sopenharmony_ci   ASCII character set.  :rfc:`2047`, however, has an :rfc:`5322` compatible
1287db96d56Sopenharmony_ci   mechanism for encoding non-ASCII text as ASCII characters within a header
1297db96d56Sopenharmony_ci   value.  When a *value* containing encoded words is passed to the
1307db96d56Sopenharmony_ci   constructor, the ``UnstructuredHeader`` parser converts such encoded words
1317db96d56Sopenharmony_ci   into unicode, following the :rfc:`2047` rules for unstructured text.  The
1327db96d56Sopenharmony_ci   parser uses heuristics to attempt to decode certain non-compliant encoded
1337db96d56Sopenharmony_ci   words.  Defects are registered in such cases, as well as defects for issues
1347db96d56Sopenharmony_ci   such as invalid characters within the encoded words or the non-encoded text.
1357db96d56Sopenharmony_ci
1367db96d56Sopenharmony_ci   This header type provides no additional attributes.
1377db96d56Sopenharmony_ci
1387db96d56Sopenharmony_ci
1397db96d56Sopenharmony_ci.. class:: DateHeader
1407db96d56Sopenharmony_ci
1417db96d56Sopenharmony_ci   :rfc:`5322` specifies a very specific format for dates within email headers.
1427db96d56Sopenharmony_ci   The ``DateHeader`` parser recognizes that date format, as well as
1437db96d56Sopenharmony_ci   recognizing a number of variant forms that are sometimes found "in the
1447db96d56Sopenharmony_ci   wild".
1457db96d56Sopenharmony_ci
1467db96d56Sopenharmony_ci   This header type provides the following additional attributes:
1477db96d56Sopenharmony_ci
1487db96d56Sopenharmony_ci   .. attribute:: datetime
1497db96d56Sopenharmony_ci
1507db96d56Sopenharmony_ci      If the header value can be recognized as a valid date of one form or
1517db96d56Sopenharmony_ci      another, this attribute will contain a :class:`~datetime.datetime`
1527db96d56Sopenharmony_ci      instance representing that date.  If the timezone of the input date is
1537db96d56Sopenharmony_ci      specified as ``-0000`` (indicating it is in UTC but contains no
1547db96d56Sopenharmony_ci      information about the source timezone), then :attr:`.datetime` will be a
1557db96d56Sopenharmony_ci      naive :class:`~datetime.datetime`.  If a specific timezone offset is
1567db96d56Sopenharmony_ci      found (including ``+0000``), then :attr:`.datetime` will contain an aware
1577db96d56Sopenharmony_ci      ``datetime`` that uses :class:`datetime.timezone` to record the timezone
1587db96d56Sopenharmony_ci      offset.
1597db96d56Sopenharmony_ci
1607db96d56Sopenharmony_ci   The ``decoded`` value of the header is determined by formatting the
1617db96d56Sopenharmony_ci   ``datetime`` according to the :rfc:`5322` rules; that is, it is set to::
1627db96d56Sopenharmony_ci
1637db96d56Sopenharmony_ci       email.utils.format_datetime(self.datetime)
1647db96d56Sopenharmony_ci
1657db96d56Sopenharmony_ci   When creating a ``DateHeader``, *value* may be
1667db96d56Sopenharmony_ci   :class:`~datetime.datetime` instance.  This means, for example, that
1677db96d56Sopenharmony_ci   the following code is valid and does what one would expect::
1687db96d56Sopenharmony_ci
1697db96d56Sopenharmony_ci       msg['Date'] = datetime(2011, 7, 15, 21)
1707db96d56Sopenharmony_ci
1717db96d56Sopenharmony_ci   Because this is a naive ``datetime`` it will be interpreted as a UTC
1727db96d56Sopenharmony_ci   timestamp, and the resulting value will have a timezone of ``-0000``.  Much
1737db96d56Sopenharmony_ci   more useful is to use the :func:`~email.utils.localtime` function from the
1747db96d56Sopenharmony_ci   :mod:`~email.utils` module::
1757db96d56Sopenharmony_ci
1767db96d56Sopenharmony_ci       msg['Date'] = utils.localtime()
1777db96d56Sopenharmony_ci
1787db96d56Sopenharmony_ci   This example sets the date header to the current time and date using
1797db96d56Sopenharmony_ci   the current timezone offset.
1807db96d56Sopenharmony_ci
1817db96d56Sopenharmony_ci
1827db96d56Sopenharmony_ci.. class:: AddressHeader
1837db96d56Sopenharmony_ci
1847db96d56Sopenharmony_ci   Address headers are one of the most complex structured header types.
1857db96d56Sopenharmony_ci   The ``AddressHeader`` class provides a generic interface to any address
1867db96d56Sopenharmony_ci   header.
1877db96d56Sopenharmony_ci
1887db96d56Sopenharmony_ci   This header type provides the following additional attributes:
1897db96d56Sopenharmony_ci
1907db96d56Sopenharmony_ci
1917db96d56Sopenharmony_ci   .. attribute:: groups
1927db96d56Sopenharmony_ci
1937db96d56Sopenharmony_ci      A tuple of :class:`.Group` objects encoding the
1947db96d56Sopenharmony_ci      addresses and groups found in the header value.  Addresses that are
1957db96d56Sopenharmony_ci      not part of a group are represented in this list as single-address
1967db96d56Sopenharmony_ci      ``Groups`` whose :attr:`~.Group.display_name` is ``None``.
1977db96d56Sopenharmony_ci
1987db96d56Sopenharmony_ci
1997db96d56Sopenharmony_ci   .. attribute:: addresses
2007db96d56Sopenharmony_ci
2017db96d56Sopenharmony_ci      A tuple of :class:`.Address` objects encoding all
2027db96d56Sopenharmony_ci      of the individual addresses from the header value.  If the header value
2037db96d56Sopenharmony_ci      contains any groups, the individual addresses from the group are included
2047db96d56Sopenharmony_ci      in the list at the point where the group occurs in the value (that is,
2057db96d56Sopenharmony_ci      the list of addresses is "flattened" into a one dimensional list).
2067db96d56Sopenharmony_ci
2077db96d56Sopenharmony_ci   The ``decoded`` value of the header will have all encoded words decoded to
2087db96d56Sopenharmony_ci   unicode.  :class:`~encodings.idna` encoded domain names are also decoded to
2097db96d56Sopenharmony_ci   unicode.  The ``decoded`` value is set by :ref:`joining <meth-str-join>` the
2107db96d56Sopenharmony_ci   :class:`str` value of the elements of the ``groups`` attribute with ``',
2117db96d56Sopenharmony_ci   '``.
2127db96d56Sopenharmony_ci
2137db96d56Sopenharmony_ci   A list of :class:`.Address` and :class:`.Group` objects in any combination
2147db96d56Sopenharmony_ci   may be used to set the value of an address header.  ``Group`` objects whose
2157db96d56Sopenharmony_ci   ``display_name`` is ``None`` will be interpreted as single addresses, which
2167db96d56Sopenharmony_ci   allows an address list to be copied with groups intact by using the list
2177db96d56Sopenharmony_ci   obtained from the ``groups`` attribute of the source header.
2187db96d56Sopenharmony_ci
2197db96d56Sopenharmony_ci
2207db96d56Sopenharmony_ci.. class:: SingleAddressHeader
2217db96d56Sopenharmony_ci
2227db96d56Sopenharmony_ci   A subclass of :class:`.AddressHeader` that adds one
2237db96d56Sopenharmony_ci   additional attribute:
2247db96d56Sopenharmony_ci
2257db96d56Sopenharmony_ci
2267db96d56Sopenharmony_ci   .. attribute:: address
2277db96d56Sopenharmony_ci
2287db96d56Sopenharmony_ci      The single address encoded by the header value.  If the header value
2297db96d56Sopenharmony_ci      actually contains more than one address (which would be a violation of
2307db96d56Sopenharmony_ci      the RFC under the default :mod:`~email.policy`), accessing this attribute
2317db96d56Sopenharmony_ci      will result in a :exc:`ValueError`.
2327db96d56Sopenharmony_ci
2337db96d56Sopenharmony_ci
2347db96d56Sopenharmony_ciMany of the above classes also have a ``Unique`` variant (for example,
2357db96d56Sopenharmony_ci``UniqueUnstructuredHeader``).  The only difference is that in the ``Unique``
2367db96d56Sopenharmony_civariant, :attr:`~.BaseHeader.max_count` is set to 1.
2377db96d56Sopenharmony_ci
2387db96d56Sopenharmony_ci
2397db96d56Sopenharmony_ci.. class:: MIMEVersionHeader
2407db96d56Sopenharmony_ci
2417db96d56Sopenharmony_ci   There is really only one valid value for the :mailheader:`MIME-Version`
2427db96d56Sopenharmony_ci   header, and that is ``1.0``.  For future proofing, this header class
2437db96d56Sopenharmony_ci   supports other valid version numbers.  If a version number has a valid value
2447db96d56Sopenharmony_ci   per :rfc:`2045`, then the header object will have non-``None`` values for
2457db96d56Sopenharmony_ci   the following attributes:
2467db96d56Sopenharmony_ci
2477db96d56Sopenharmony_ci   .. attribute:: version
2487db96d56Sopenharmony_ci
2497db96d56Sopenharmony_ci      The version number as a string, with any whitespace and/or comments
2507db96d56Sopenharmony_ci      removed.
2517db96d56Sopenharmony_ci
2527db96d56Sopenharmony_ci   .. attribute:: major
2537db96d56Sopenharmony_ci
2547db96d56Sopenharmony_ci      The major version number as an integer
2557db96d56Sopenharmony_ci
2567db96d56Sopenharmony_ci   .. attribute:: minor
2577db96d56Sopenharmony_ci
2587db96d56Sopenharmony_ci      The minor version number as an integer
2597db96d56Sopenharmony_ci
2607db96d56Sopenharmony_ci
2617db96d56Sopenharmony_ci.. class:: ParameterizedMIMEHeader
2627db96d56Sopenharmony_ci
2637db96d56Sopenharmony_ci    MIME headers all start with the prefix 'Content-'.  Each specific header has
2647db96d56Sopenharmony_ci    a certain value, described under the class for that header.  Some can
2657db96d56Sopenharmony_ci    also take a list of supplemental parameters, which have a common format.
2667db96d56Sopenharmony_ci    This class serves as a base for all the MIME headers that take parameters.
2677db96d56Sopenharmony_ci
2687db96d56Sopenharmony_ci    .. attribute:: params
2697db96d56Sopenharmony_ci
2707db96d56Sopenharmony_ci       A dictionary mapping parameter names to parameter values.
2717db96d56Sopenharmony_ci
2727db96d56Sopenharmony_ci
2737db96d56Sopenharmony_ci.. class:: ContentTypeHeader
2747db96d56Sopenharmony_ci
2757db96d56Sopenharmony_ci    A :class:`ParameterizedMIMEHeader` class that handles the
2767db96d56Sopenharmony_ci    :mailheader:`Content-Type` header.
2777db96d56Sopenharmony_ci
2787db96d56Sopenharmony_ci    .. attribute:: content_type
2797db96d56Sopenharmony_ci
2807db96d56Sopenharmony_ci       The content type string, in the form ``maintype/subtype``.
2817db96d56Sopenharmony_ci
2827db96d56Sopenharmony_ci    .. attribute:: maintype
2837db96d56Sopenharmony_ci
2847db96d56Sopenharmony_ci    .. attribute:: subtype
2857db96d56Sopenharmony_ci
2867db96d56Sopenharmony_ci
2877db96d56Sopenharmony_ci.. class:: ContentDispositionHeader
2887db96d56Sopenharmony_ci
2897db96d56Sopenharmony_ci    A :class:`ParameterizedMIMEHeader` class that handles the
2907db96d56Sopenharmony_ci    :mailheader:`Content-Disposition` header.
2917db96d56Sopenharmony_ci
2927db96d56Sopenharmony_ci    .. attribute:: content_disposition
2937db96d56Sopenharmony_ci
2947db96d56Sopenharmony_ci       ``inline`` and ``attachment`` are the only valid values in common use.
2957db96d56Sopenharmony_ci
2967db96d56Sopenharmony_ci
2977db96d56Sopenharmony_ci.. class:: ContentTransferEncoding
2987db96d56Sopenharmony_ci
2997db96d56Sopenharmony_ci   Handles the :mailheader:`Content-Transfer-Encoding` header.
3007db96d56Sopenharmony_ci
3017db96d56Sopenharmony_ci   .. attribute:: cte
3027db96d56Sopenharmony_ci
3037db96d56Sopenharmony_ci      Valid values are ``7bit``, ``8bit``, ``base64``, and
3047db96d56Sopenharmony_ci      ``quoted-printable``.  See :rfc:`2045` for more information.
3057db96d56Sopenharmony_ci
3067db96d56Sopenharmony_ci
3077db96d56Sopenharmony_ci
3087db96d56Sopenharmony_ci.. class:: HeaderRegistry(base_class=BaseHeader, \
3097db96d56Sopenharmony_ci                          default_class=UnstructuredHeader, \
3107db96d56Sopenharmony_ci                          use_default_map=True)
3117db96d56Sopenharmony_ci
3127db96d56Sopenharmony_ci    This is the factory used by :class:`~email.policy.EmailPolicy` by default.
3137db96d56Sopenharmony_ci    ``HeaderRegistry`` builds the class used to create a header instance
3147db96d56Sopenharmony_ci    dynamically, using *base_class* and a specialized class retrieved from a
3157db96d56Sopenharmony_ci    registry that it holds.  When a given header name does not appear in the
3167db96d56Sopenharmony_ci    registry, the class specified by *default_class* is used as the specialized
3177db96d56Sopenharmony_ci    class.  When *use_default_map* is ``True`` (the default), the standard
3187db96d56Sopenharmony_ci    mapping of header names to classes is copied in to the registry during
3197db96d56Sopenharmony_ci    initialization.  *base_class* is always the last class in the generated
3207db96d56Sopenharmony_ci    class's ``__bases__`` list.
3217db96d56Sopenharmony_ci
3227db96d56Sopenharmony_ci    The default mappings are:
3237db96d56Sopenharmony_ci
3247db96d56Sopenharmony_ci      :subject:                   UniqueUnstructuredHeader
3257db96d56Sopenharmony_ci      :date:                      UniqueDateHeader
3267db96d56Sopenharmony_ci      :resent-date:               DateHeader
3277db96d56Sopenharmony_ci      :orig-date:                 UniqueDateHeader
3287db96d56Sopenharmony_ci      :sender:                    UniqueSingleAddressHeader
3297db96d56Sopenharmony_ci      :resent-sender:             SingleAddressHeader
3307db96d56Sopenharmony_ci      :to:                        UniqueAddressHeader
3317db96d56Sopenharmony_ci      :resent-to:                 AddressHeader
3327db96d56Sopenharmony_ci      :cc:                        UniqueAddressHeader
3337db96d56Sopenharmony_ci      :resent-cc:                 AddressHeader
3347db96d56Sopenharmony_ci      :bcc:                       UniqueAddressHeader
3357db96d56Sopenharmony_ci      :resent-bcc:                AddressHeader
3367db96d56Sopenharmony_ci      :from:                      UniqueAddressHeader
3377db96d56Sopenharmony_ci      :resent-from:               AddressHeader
3387db96d56Sopenharmony_ci      :reply-to:                  UniqueAddressHeader
3397db96d56Sopenharmony_ci      :mime-version:              MIMEVersionHeader
3407db96d56Sopenharmony_ci      :content-type:              ContentTypeHeader
3417db96d56Sopenharmony_ci      :content-disposition:       ContentDispositionHeader
3427db96d56Sopenharmony_ci      :content-transfer-encoding: ContentTransferEncodingHeader
3437db96d56Sopenharmony_ci      :message-id:                MessageIDHeader
3447db96d56Sopenharmony_ci
3457db96d56Sopenharmony_ci    ``HeaderRegistry`` has the following methods:
3467db96d56Sopenharmony_ci
3477db96d56Sopenharmony_ci
3487db96d56Sopenharmony_ci    .. method:: map_to_type(self, name, cls)
3497db96d56Sopenharmony_ci
3507db96d56Sopenharmony_ci       *name* is the name of the header to be mapped.  It will be converted to
3517db96d56Sopenharmony_ci       lower case in the registry.  *cls* is the specialized class to be used,
3527db96d56Sopenharmony_ci       along with *base_class*, to create the class used to instantiate headers
3537db96d56Sopenharmony_ci       that match *name*.
3547db96d56Sopenharmony_ci
3557db96d56Sopenharmony_ci
3567db96d56Sopenharmony_ci    .. method:: __getitem__(name)
3577db96d56Sopenharmony_ci
3587db96d56Sopenharmony_ci       Construct and return a class to handle creating a *name* header.
3597db96d56Sopenharmony_ci
3607db96d56Sopenharmony_ci
3617db96d56Sopenharmony_ci    .. method:: __call__(name, value)
3627db96d56Sopenharmony_ci
3637db96d56Sopenharmony_ci       Retrieves the specialized header associated with *name* from the
3647db96d56Sopenharmony_ci       registry (using *default_class* if *name* does not appear in the
3657db96d56Sopenharmony_ci       registry) and composes it with *base_class* to produce a class,
3667db96d56Sopenharmony_ci       calls the constructed class's constructor, passing it the same
3677db96d56Sopenharmony_ci       argument list, and finally returns the class instance created thereby.
3687db96d56Sopenharmony_ci
3697db96d56Sopenharmony_ci
3707db96d56Sopenharmony_ciThe following classes are the classes used to represent data parsed from
3717db96d56Sopenharmony_cistructured headers and can, in general, be used by an application program to
3727db96d56Sopenharmony_ciconstruct structured values to assign to specific headers.
3737db96d56Sopenharmony_ci
3747db96d56Sopenharmony_ci
3757db96d56Sopenharmony_ci.. class:: Address(display_name='', username='', domain='', addr_spec=None)
3767db96d56Sopenharmony_ci
3777db96d56Sopenharmony_ci   The class used to represent an email address.  The general form of an
3787db96d56Sopenharmony_ci   address is::
3797db96d56Sopenharmony_ci
3807db96d56Sopenharmony_ci      [display_name] <username@domain>
3817db96d56Sopenharmony_ci
3827db96d56Sopenharmony_ci   or::
3837db96d56Sopenharmony_ci
3847db96d56Sopenharmony_ci      username@domain
3857db96d56Sopenharmony_ci
3867db96d56Sopenharmony_ci   where each part must conform to specific syntax rules spelled out in
3877db96d56Sopenharmony_ci   :rfc:`5322`.
3887db96d56Sopenharmony_ci
3897db96d56Sopenharmony_ci   As a convenience *addr_spec* can be specified instead of *username* and
3907db96d56Sopenharmony_ci   *domain*, in which case *username* and *domain* will be parsed from the
3917db96d56Sopenharmony_ci   *addr_spec*.  An *addr_spec* must be a properly RFC quoted string; if it is
3927db96d56Sopenharmony_ci   not ``Address`` will raise an error.  Unicode characters are allowed and
3937db96d56Sopenharmony_ci   will be property encoded when serialized.  However, per the RFCs, unicode is
3947db96d56Sopenharmony_ci   *not* allowed in the username portion of the address.
3957db96d56Sopenharmony_ci
3967db96d56Sopenharmony_ci   .. attribute:: display_name
3977db96d56Sopenharmony_ci
3987db96d56Sopenharmony_ci      The display name portion of the address, if any, with all quoting
3997db96d56Sopenharmony_ci      removed.  If the address does not have a display name, this attribute
4007db96d56Sopenharmony_ci      will be an empty string.
4017db96d56Sopenharmony_ci
4027db96d56Sopenharmony_ci   .. attribute:: username
4037db96d56Sopenharmony_ci
4047db96d56Sopenharmony_ci      The ``username`` portion of the address, with all quoting removed.
4057db96d56Sopenharmony_ci
4067db96d56Sopenharmony_ci   .. attribute:: domain
4077db96d56Sopenharmony_ci
4087db96d56Sopenharmony_ci      The ``domain`` portion of the address.
4097db96d56Sopenharmony_ci
4107db96d56Sopenharmony_ci   .. attribute:: addr_spec
4117db96d56Sopenharmony_ci
4127db96d56Sopenharmony_ci      The ``username@domain`` portion of the address, correctly quoted
4137db96d56Sopenharmony_ci      for use as a bare address (the second form shown above).  This
4147db96d56Sopenharmony_ci      attribute is not mutable.
4157db96d56Sopenharmony_ci
4167db96d56Sopenharmony_ci   .. method:: __str__()
4177db96d56Sopenharmony_ci
4187db96d56Sopenharmony_ci      The ``str`` value of the object is the address quoted according to
4197db96d56Sopenharmony_ci      :rfc:`5322` rules, but with no Content Transfer Encoding of any non-ASCII
4207db96d56Sopenharmony_ci      characters.
4217db96d56Sopenharmony_ci
4227db96d56Sopenharmony_ci   To support SMTP (:rfc:`5321`), ``Address`` handles one special case: if
4237db96d56Sopenharmony_ci   ``username`` and ``domain`` are both the empty string (or ``None``), then
4247db96d56Sopenharmony_ci   the string value of the ``Address`` is ``<>``.
4257db96d56Sopenharmony_ci
4267db96d56Sopenharmony_ci
4277db96d56Sopenharmony_ci.. class:: Group(display_name=None, addresses=None)
4287db96d56Sopenharmony_ci
4297db96d56Sopenharmony_ci   The class used to represent an address group.  The general form of an
4307db96d56Sopenharmony_ci   address group is::
4317db96d56Sopenharmony_ci
4327db96d56Sopenharmony_ci     display_name: [address-list];
4337db96d56Sopenharmony_ci
4347db96d56Sopenharmony_ci   As a convenience for processing lists of addresses that consist of a mixture
4357db96d56Sopenharmony_ci   of groups and single addresses, a ``Group`` may also be used to represent
4367db96d56Sopenharmony_ci   single addresses that are not part of a group by setting *display_name* to
4377db96d56Sopenharmony_ci   ``None`` and providing a list of the single address as *addresses*.
4387db96d56Sopenharmony_ci
4397db96d56Sopenharmony_ci   .. attribute:: display_name
4407db96d56Sopenharmony_ci
4417db96d56Sopenharmony_ci      The ``display_name`` of the group.  If it is ``None`` and there is
4427db96d56Sopenharmony_ci      exactly one ``Address`` in ``addresses``, then the ``Group`` represents a
4437db96d56Sopenharmony_ci      single address that is not in a group.
4447db96d56Sopenharmony_ci
4457db96d56Sopenharmony_ci   .. attribute:: addresses
4467db96d56Sopenharmony_ci
4477db96d56Sopenharmony_ci      A possibly empty tuple of :class:`.Address` objects representing the
4487db96d56Sopenharmony_ci      addresses in the group.
4497db96d56Sopenharmony_ci
4507db96d56Sopenharmony_ci   .. method:: __str__()
4517db96d56Sopenharmony_ci
4527db96d56Sopenharmony_ci      The ``str`` value of a ``Group`` is formatted according to :rfc:`5322`,
4537db96d56Sopenharmony_ci      but with no Content Transfer Encoding of any non-ASCII characters.  If
4547db96d56Sopenharmony_ci      ``display_name`` is none and there is a single ``Address`` in the
4557db96d56Sopenharmony_ci      ``addresses`` list, the ``str`` value will be the same as the ``str`` of
4567db96d56Sopenharmony_ci      that single ``Address``.
4577db96d56Sopenharmony_ci
4587db96d56Sopenharmony_ci
4597db96d56Sopenharmony_ci.. rubric:: Footnotes
4607db96d56Sopenharmony_ci
4617db96d56Sopenharmony_ci.. [1] Originally added in 3.3 as a :term:`provisional module <provisional
4627db96d56Sopenharmony_ci       package>`
463