17db96d56Sopenharmony_ci:mod:`email.policy`: Policy Objects
27db96d56Sopenharmony_ci-----------------------------------
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: email.policy
57db96d56Sopenharmony_ci   :synopsis: Controlling the parsing and generating of messages
67db96d56Sopenharmony_ci
77db96d56Sopenharmony_ci.. moduleauthor:: R. David Murray <rdmurray@bitdance.com>
87db96d56Sopenharmony_ci.. sectionauthor:: R. David Murray <rdmurray@bitdance.com>
97db96d56Sopenharmony_ci
107db96d56Sopenharmony_ci.. versionadded:: 3.3
117db96d56Sopenharmony_ci
127db96d56Sopenharmony_ci**Source code:** :source:`Lib/email/policy.py`
137db96d56Sopenharmony_ci
147db96d56Sopenharmony_ci--------------
157db96d56Sopenharmony_ci
167db96d56Sopenharmony_ciThe :mod:`email` package's prime focus is the handling of email messages as
177db96d56Sopenharmony_cidescribed by the various email and MIME RFCs.  However, the general format of
187db96d56Sopenharmony_ciemail messages (a block of header fields each consisting of a name followed by
197db96d56Sopenharmony_cia colon followed by a value, the whole block followed by a blank line and an
207db96d56Sopenharmony_ciarbitrary 'body'), is a format that has found utility outside of the realm of
217db96d56Sopenharmony_ciemail.  Some of these uses conform fairly closely to the main email RFCs, some
227db96d56Sopenharmony_cido not.  Even when working with email, there are times when it is desirable to
237db96d56Sopenharmony_cibreak strict compliance with the RFCs, such as generating emails that
247db96d56Sopenharmony_ciinteroperate with email servers that do not themselves follow the standards, or
257db96d56Sopenharmony_cithat implement extensions you want to use in ways that violate the
267db96d56Sopenharmony_cistandards.
277db96d56Sopenharmony_ci
287db96d56Sopenharmony_ciPolicy objects give the email package the flexibility to handle all these
297db96d56Sopenharmony_cidisparate use cases.
307db96d56Sopenharmony_ci
317db96d56Sopenharmony_ciA :class:`Policy` object encapsulates a set of attributes and methods that
327db96d56Sopenharmony_cicontrol the behavior of various components of the email package during use.
337db96d56Sopenharmony_ci:class:`Policy` instances can be passed to various classes and methods in the
347db96d56Sopenharmony_ciemail package to alter the default behavior.  The settable values and their
357db96d56Sopenharmony_cidefaults are described below.
367db96d56Sopenharmony_ci
377db96d56Sopenharmony_ciThere is a default policy used by all classes in the email package.  For all of
387db96d56Sopenharmony_cithe :mod:`~email.parser` classes and the related convenience functions, and for
397db96d56Sopenharmony_cithe :class:`~email.message.Message` class, this is the :class:`Compat32`
407db96d56Sopenharmony_cipolicy, via its corresponding pre-defined instance :const:`compat32`.  This
417db96d56Sopenharmony_cipolicy provides for complete backward compatibility (in some cases, including
427db96d56Sopenharmony_cibug compatibility) with the pre-Python3.3 version of the email package.
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ciThis default value for the *policy* keyword to
457db96d56Sopenharmony_ci:class:`~email.message.EmailMessage` is the :class:`EmailPolicy` policy, via
467db96d56Sopenharmony_ciits pre-defined instance :data:`~default`.
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ciWhen a :class:`~email.message.Message` or :class:`~email.message.EmailMessage`
497db96d56Sopenharmony_ciobject is created, it acquires a policy.  If the message is created by a
507db96d56Sopenharmony_ci:mod:`~email.parser`, a policy passed to the parser will be the policy used by
517db96d56Sopenharmony_cithe message it creates.  If the message is created by the program, then the
527db96d56Sopenharmony_cipolicy can be specified when it is created.  When a message is passed to a
537db96d56Sopenharmony_ci:mod:`~email.generator`, the generator uses the policy from the message by
547db96d56Sopenharmony_cidefault, but you can also pass a specific policy to the generator that will
557db96d56Sopenharmony_cioverride the one stored on the message object.
567db96d56Sopenharmony_ci
577db96d56Sopenharmony_ciThe default value for the *policy* keyword for the :mod:`email.parser` classes
587db96d56Sopenharmony_ciand the parser convenience functions **will be changing** in a future version of
597db96d56Sopenharmony_ciPython.  Therefore you should **always specify explicitly which policy you want
607db96d56Sopenharmony_cito use** when calling any of the classes and functions described in the
617db96d56Sopenharmony_ci:mod:`~email.parser` module.
627db96d56Sopenharmony_ci
637db96d56Sopenharmony_ciThe first part of this documentation covers the features of :class:`Policy`, an
647db96d56Sopenharmony_ci:term:`abstract base class` that defines the features that are common to all
657db96d56Sopenharmony_cipolicy objects, including :const:`compat32`.  This includes certain hook
667db96d56Sopenharmony_cimethods that are called internally by the email package, which a custom policy
677db96d56Sopenharmony_cicould override to obtain different behavior.  The second part describes the
687db96d56Sopenharmony_ciconcrete classes :class:`EmailPolicy` and :class:`Compat32`, which implement
697db96d56Sopenharmony_cithe hooks that provide the standard behavior and the backward compatible
707db96d56Sopenharmony_cibehavior and features, respectively.
717db96d56Sopenharmony_ci
727db96d56Sopenharmony_ci:class:`Policy` instances are immutable, but they can be cloned, accepting the
737db96d56Sopenharmony_cisame keyword arguments as the class constructor and returning a new
747db96d56Sopenharmony_ci:class:`Policy` instance that is a copy of the original but with the specified
757db96d56Sopenharmony_ciattributes values changed.
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ciAs an example, the following code could be used to read an email message from a
787db96d56Sopenharmony_cifile on disk and pass it to the system ``sendmail`` program on a Unix system:
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci.. testsetup::
817db96d56Sopenharmony_ci
827db96d56Sopenharmony_ci   from unittest import mock
837db96d56Sopenharmony_ci   mocker = mock.patch('subprocess.Popen')
847db96d56Sopenharmony_ci   m = mocker.start()
857db96d56Sopenharmony_ci   proc = mock.MagicMock()
867db96d56Sopenharmony_ci   m.return_value = proc
877db96d56Sopenharmony_ci   proc.stdin.close.return_value = None
887db96d56Sopenharmony_ci   mymsg = open('mymsg.txt', 'w')
897db96d56Sopenharmony_ci   mymsg.write('To: abc@xyz.com\n\n')
907db96d56Sopenharmony_ci   mymsg.flush()
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ci.. doctest::
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ci   >>> from email import message_from_binary_file
957db96d56Sopenharmony_ci   >>> from email.generator import BytesGenerator
967db96d56Sopenharmony_ci   >>> from email import policy
977db96d56Sopenharmony_ci   >>> from subprocess import Popen, PIPE
987db96d56Sopenharmony_ci   >>> with open('mymsg.txt', 'rb') as f:
997db96d56Sopenharmony_ci   ...     msg = message_from_binary_file(f, policy=policy.default)
1007db96d56Sopenharmony_ci   >>> p = Popen(['sendmail', msg['To'].addresses[0]], stdin=PIPE)
1017db96d56Sopenharmony_ci   >>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n'))
1027db96d56Sopenharmony_ci   >>> g.flatten(msg)
1037db96d56Sopenharmony_ci   >>> p.stdin.close()
1047db96d56Sopenharmony_ci   >>> rc = p.wait()
1057db96d56Sopenharmony_ci
1067db96d56Sopenharmony_ci.. testcleanup::
1077db96d56Sopenharmony_ci
1087db96d56Sopenharmony_ci   mymsg.close()
1097db96d56Sopenharmony_ci   mocker.stop()
1107db96d56Sopenharmony_ci   import os
1117db96d56Sopenharmony_ci   os.remove('mymsg.txt')
1127db96d56Sopenharmony_ci
1137db96d56Sopenharmony_ciHere we are telling :class:`~email.generator.BytesGenerator` to use the RFC
1147db96d56Sopenharmony_cicorrect line separator characters when creating the binary string to feed into
1157db96d56Sopenharmony_ci``sendmail's`` ``stdin``, where the default policy would use ``\n`` line
1167db96d56Sopenharmony_ciseparators.
1177db96d56Sopenharmony_ci
1187db96d56Sopenharmony_ciSome email package methods accept a *policy* keyword argument, allowing the
1197db96d56Sopenharmony_cipolicy to be overridden for that method.  For example, the following code uses
1207db96d56Sopenharmony_cithe :meth:`~email.message.Message.as_bytes` method of the *msg* object from
1217db96d56Sopenharmony_cithe previous example and writes the message to a file using the native line
1227db96d56Sopenharmony_ciseparators for the platform on which it is running::
1237db96d56Sopenharmony_ci
1247db96d56Sopenharmony_ci   >>> import os
1257db96d56Sopenharmony_ci   >>> with open('converted.txt', 'wb') as f:
1267db96d56Sopenharmony_ci   ...     f.write(msg.as_bytes(policy=msg.policy.clone(linesep=os.linesep)))
1277db96d56Sopenharmony_ci   17
1287db96d56Sopenharmony_ci
1297db96d56Sopenharmony_ciPolicy objects can also be combined using the addition operator, producing a
1307db96d56Sopenharmony_cipolicy object whose settings are a combination of the non-default values of the
1317db96d56Sopenharmony_cisummed objects::
1327db96d56Sopenharmony_ci
1337db96d56Sopenharmony_ci   >>> compat_SMTP = policy.compat32.clone(linesep='\r\n')
1347db96d56Sopenharmony_ci   >>> compat_strict = policy.compat32.clone(raise_on_defect=True)
1357db96d56Sopenharmony_ci   >>> compat_strict_SMTP = compat_SMTP + compat_strict
1367db96d56Sopenharmony_ci
1377db96d56Sopenharmony_ciThis operation is not commutative; that is, the order in which the objects are
1387db96d56Sopenharmony_ciadded matters.  To illustrate::
1397db96d56Sopenharmony_ci
1407db96d56Sopenharmony_ci   >>> policy100 = policy.compat32.clone(max_line_length=100)
1417db96d56Sopenharmony_ci   >>> policy80 = policy.compat32.clone(max_line_length=80)
1427db96d56Sopenharmony_ci   >>> apolicy = policy100 + policy80
1437db96d56Sopenharmony_ci   >>> apolicy.max_line_length
1447db96d56Sopenharmony_ci   80
1457db96d56Sopenharmony_ci   >>> apolicy = policy80 + policy100
1467db96d56Sopenharmony_ci   >>> apolicy.max_line_length
1477db96d56Sopenharmony_ci   100
1487db96d56Sopenharmony_ci
1497db96d56Sopenharmony_ci
1507db96d56Sopenharmony_ci.. class:: Policy(**kw)
1517db96d56Sopenharmony_ci
1527db96d56Sopenharmony_ci   This is the :term:`abstract base class` for all policy classes.  It provides
1537db96d56Sopenharmony_ci   default implementations for a couple of trivial methods, as well as the
1547db96d56Sopenharmony_ci   implementation of the immutability property, the :meth:`clone` method, and
1557db96d56Sopenharmony_ci   the constructor semantics.
1567db96d56Sopenharmony_ci
1577db96d56Sopenharmony_ci   The constructor of a policy class can be passed various keyword arguments.
1587db96d56Sopenharmony_ci   The arguments that may be specified are any non-method properties on this
1597db96d56Sopenharmony_ci   class, plus any additional non-method properties on the concrete class.  A
1607db96d56Sopenharmony_ci   value specified in the constructor will override the default value for the
1617db96d56Sopenharmony_ci   corresponding attribute.
1627db96d56Sopenharmony_ci
1637db96d56Sopenharmony_ci   This class defines the following properties, and thus values for the
1647db96d56Sopenharmony_ci   following may be passed in the constructor of any policy class:
1657db96d56Sopenharmony_ci
1667db96d56Sopenharmony_ci
1677db96d56Sopenharmony_ci   .. attribute:: max_line_length
1687db96d56Sopenharmony_ci
1697db96d56Sopenharmony_ci      The maximum length of any line in the serialized output, not counting the
1707db96d56Sopenharmony_ci      end of line character(s).  Default is 78, per :rfc:`5322`.  A value of
1717db96d56Sopenharmony_ci      ``0`` or :const:`None` indicates that no line wrapping should be
1727db96d56Sopenharmony_ci      done at all.
1737db96d56Sopenharmony_ci
1747db96d56Sopenharmony_ci
1757db96d56Sopenharmony_ci   .. attribute:: linesep
1767db96d56Sopenharmony_ci
1777db96d56Sopenharmony_ci      The string to be used to terminate lines in serialized output.  The
1787db96d56Sopenharmony_ci      default is ``\n`` because that's the internal end-of-line discipline used
1797db96d56Sopenharmony_ci      by Python, though ``\r\n`` is required by the RFCs.
1807db96d56Sopenharmony_ci
1817db96d56Sopenharmony_ci
1827db96d56Sopenharmony_ci   .. attribute:: cte_type
1837db96d56Sopenharmony_ci
1847db96d56Sopenharmony_ci      Controls the type of Content Transfer Encodings that may be or are
1857db96d56Sopenharmony_ci      required to be used.  The possible values are:
1867db96d56Sopenharmony_ci
1877db96d56Sopenharmony_ci      .. tabularcolumns:: |l|L|
1887db96d56Sopenharmony_ci
1897db96d56Sopenharmony_ci      ========  ===============================================================
1907db96d56Sopenharmony_ci      ``7bit``  all data must be "7 bit clean" (ASCII-only).  This means that
1917db96d56Sopenharmony_ci                where necessary data will be encoded using either
1927db96d56Sopenharmony_ci                quoted-printable or base64 encoding.
1937db96d56Sopenharmony_ci
1947db96d56Sopenharmony_ci      ``8bit``  data is not constrained to be 7 bit clean.  Data in headers is
1957db96d56Sopenharmony_ci                still required to be ASCII-only and so will be encoded (see
1967db96d56Sopenharmony_ci                :meth:`fold_binary` and :attr:`~EmailPolicy.utf8` below for
1977db96d56Sopenharmony_ci                exceptions), but body parts may use the ``8bit`` CTE.
1987db96d56Sopenharmony_ci      ========  ===============================================================
1997db96d56Sopenharmony_ci
2007db96d56Sopenharmony_ci      A ``cte_type`` value of ``8bit`` only works with ``BytesGenerator``, not
2017db96d56Sopenharmony_ci      ``Generator``, because strings cannot contain binary data.  If a
2027db96d56Sopenharmony_ci      ``Generator`` is operating under a policy that specifies
2037db96d56Sopenharmony_ci      ``cte_type=8bit``, it will act as if ``cte_type`` is ``7bit``.
2047db96d56Sopenharmony_ci
2057db96d56Sopenharmony_ci
2067db96d56Sopenharmony_ci   .. attribute:: raise_on_defect
2077db96d56Sopenharmony_ci
2087db96d56Sopenharmony_ci      If :const:`True`, any defects encountered will be raised as errors.  If
2097db96d56Sopenharmony_ci      :const:`False` (the default), defects will be passed to the
2107db96d56Sopenharmony_ci      :meth:`register_defect` method.
2117db96d56Sopenharmony_ci
2127db96d56Sopenharmony_ci
2137db96d56Sopenharmony_ci   .. attribute:: mangle_from_
2147db96d56Sopenharmony_ci
2157db96d56Sopenharmony_ci      If :const:`True`, lines starting with *"From "* in the body are
2167db96d56Sopenharmony_ci      escaped by putting a ``>`` in front of them. This parameter is used when
2177db96d56Sopenharmony_ci      the message is being serialized by a generator.
2187db96d56Sopenharmony_ci      Default: :const:`False`.
2197db96d56Sopenharmony_ci
2207db96d56Sopenharmony_ci      .. versionadded:: 3.5
2217db96d56Sopenharmony_ci         The *mangle_from_* parameter.
2227db96d56Sopenharmony_ci
2237db96d56Sopenharmony_ci
2247db96d56Sopenharmony_ci   .. attribute:: message_factory
2257db96d56Sopenharmony_ci
2267db96d56Sopenharmony_ci      A factory function for constructing a new empty message object.  Used
2277db96d56Sopenharmony_ci      by the parser when building messages.  Defaults to ``None``, in
2287db96d56Sopenharmony_ci      which case :class:`~email.message.Message` is used.
2297db96d56Sopenharmony_ci
2307db96d56Sopenharmony_ci      .. versionadded:: 3.6
2317db96d56Sopenharmony_ci
2327db96d56Sopenharmony_ci   The following :class:`Policy` method is intended to be called by code using
2337db96d56Sopenharmony_ci   the email library to create policy instances with custom settings:
2347db96d56Sopenharmony_ci
2357db96d56Sopenharmony_ci
2367db96d56Sopenharmony_ci   .. method:: clone(**kw)
2377db96d56Sopenharmony_ci
2387db96d56Sopenharmony_ci      Return a new :class:`Policy` instance whose attributes have the same
2397db96d56Sopenharmony_ci      values as the current instance, except where those attributes are
2407db96d56Sopenharmony_ci      given new values by the keyword arguments.
2417db96d56Sopenharmony_ci
2427db96d56Sopenharmony_ci
2437db96d56Sopenharmony_ci   The remaining :class:`Policy` methods are called by the email package code,
2447db96d56Sopenharmony_ci   and are not intended to be called by an application using the email package.
2457db96d56Sopenharmony_ci   A custom policy must implement all of these methods.
2467db96d56Sopenharmony_ci
2477db96d56Sopenharmony_ci
2487db96d56Sopenharmony_ci   .. method:: handle_defect(obj, defect)
2497db96d56Sopenharmony_ci
2507db96d56Sopenharmony_ci      Handle a *defect* found on *obj*.  When the email package calls this
2517db96d56Sopenharmony_ci      method, *defect* will always be a subclass of
2527db96d56Sopenharmony_ci      :class:`~email.errors.Defect`.
2537db96d56Sopenharmony_ci
2547db96d56Sopenharmony_ci      The default implementation checks the :attr:`raise_on_defect` flag.  If
2557db96d56Sopenharmony_ci      it is ``True``, *defect* is raised as an exception.  If it is ``False``
2567db96d56Sopenharmony_ci      (the default), *obj* and *defect* are passed to :meth:`register_defect`.
2577db96d56Sopenharmony_ci
2587db96d56Sopenharmony_ci
2597db96d56Sopenharmony_ci   .. method:: register_defect(obj, defect)
2607db96d56Sopenharmony_ci
2617db96d56Sopenharmony_ci      Register a *defect* on *obj*.  In the email package, *defect* will always
2627db96d56Sopenharmony_ci      be a subclass of :class:`~email.errors.Defect`.
2637db96d56Sopenharmony_ci
2647db96d56Sopenharmony_ci      The default implementation calls the ``append`` method of the ``defects``
2657db96d56Sopenharmony_ci      attribute of *obj*.  When the email package calls :attr:`handle_defect`,
2667db96d56Sopenharmony_ci      *obj* will normally have a ``defects`` attribute that has an ``append``
2677db96d56Sopenharmony_ci      method.  Custom object types used with the email package (for example,
2687db96d56Sopenharmony_ci      custom ``Message`` objects) should also provide such an attribute,
2697db96d56Sopenharmony_ci      otherwise defects in parsed messages will raise unexpected errors.
2707db96d56Sopenharmony_ci
2717db96d56Sopenharmony_ci
2727db96d56Sopenharmony_ci   .. method:: header_max_count(name)
2737db96d56Sopenharmony_ci
2747db96d56Sopenharmony_ci      Return the maximum allowed number of headers named *name*.
2757db96d56Sopenharmony_ci
2767db96d56Sopenharmony_ci      Called when a header is added to an :class:`~email.message.EmailMessage`
2777db96d56Sopenharmony_ci      or :class:`~email.message.Message` object.  If the returned value is not
2787db96d56Sopenharmony_ci      ``0`` or ``None``, and there are already a number of headers with the
2797db96d56Sopenharmony_ci      name *name* greater than or equal to the value returned, a
2807db96d56Sopenharmony_ci      :exc:`ValueError` is raised.
2817db96d56Sopenharmony_ci
2827db96d56Sopenharmony_ci      Because the default behavior of ``Message.__setitem__`` is to append the
2837db96d56Sopenharmony_ci      value to the list of headers, it is easy to create duplicate headers
2847db96d56Sopenharmony_ci      without realizing it.  This method allows certain headers to be limited
2857db96d56Sopenharmony_ci      in the number of instances of that header that may be added to a
2867db96d56Sopenharmony_ci      ``Message`` programmatically.  (The limit is not observed by the parser,
2877db96d56Sopenharmony_ci      which will faithfully produce as many headers as exist in the message
2887db96d56Sopenharmony_ci      being parsed.)
2897db96d56Sopenharmony_ci
2907db96d56Sopenharmony_ci      The default implementation returns ``None`` for all header names.
2917db96d56Sopenharmony_ci
2927db96d56Sopenharmony_ci
2937db96d56Sopenharmony_ci   .. method:: header_source_parse(sourcelines)
2947db96d56Sopenharmony_ci
2957db96d56Sopenharmony_ci      The email package calls this method with a list of strings, each string
2967db96d56Sopenharmony_ci      ending with the line separation characters found in the source being
2977db96d56Sopenharmony_ci      parsed.  The first line includes the field header name and separator.
2987db96d56Sopenharmony_ci      All whitespace in the source is preserved.  The method should return the
2997db96d56Sopenharmony_ci      ``(name, value)`` tuple that is to be stored in the ``Message`` to
3007db96d56Sopenharmony_ci      represent the parsed header.
3017db96d56Sopenharmony_ci
3027db96d56Sopenharmony_ci      If an implementation wishes to retain compatibility with the existing
3037db96d56Sopenharmony_ci      email package policies, *name* should be the case preserved name (all
3047db96d56Sopenharmony_ci      characters up to the '``:``' separator), while *value* should be the
3057db96d56Sopenharmony_ci      unfolded value (all line separator characters removed, but whitespace
3067db96d56Sopenharmony_ci      kept intact), stripped of leading whitespace.
3077db96d56Sopenharmony_ci
3087db96d56Sopenharmony_ci      *sourcelines* may contain surrogateescaped binary data.
3097db96d56Sopenharmony_ci
3107db96d56Sopenharmony_ci      There is no default implementation
3117db96d56Sopenharmony_ci
3127db96d56Sopenharmony_ci
3137db96d56Sopenharmony_ci   .. method:: header_store_parse(name, value)
3147db96d56Sopenharmony_ci
3157db96d56Sopenharmony_ci      The email package calls this method with the name and value provided by
3167db96d56Sopenharmony_ci      the application program when the application program is modifying a
3177db96d56Sopenharmony_ci      ``Message`` programmatically (as opposed to a ``Message`` created by a
3187db96d56Sopenharmony_ci      parser).  The method should return the ``(name, value)`` tuple that is to
3197db96d56Sopenharmony_ci      be stored in the ``Message`` to represent the header.
3207db96d56Sopenharmony_ci
3217db96d56Sopenharmony_ci      If an implementation wishes to retain compatibility with the existing
3227db96d56Sopenharmony_ci      email package policies, the *name* and *value* should be strings or
3237db96d56Sopenharmony_ci      string subclasses that do not change the content of the passed in
3247db96d56Sopenharmony_ci      arguments.
3257db96d56Sopenharmony_ci
3267db96d56Sopenharmony_ci      There is no default implementation
3277db96d56Sopenharmony_ci
3287db96d56Sopenharmony_ci
3297db96d56Sopenharmony_ci   .. method:: header_fetch_parse(name, value)
3307db96d56Sopenharmony_ci
3317db96d56Sopenharmony_ci      The email package calls this method with the *name* and *value* currently
3327db96d56Sopenharmony_ci      stored in the ``Message`` when that header is requested by the
3337db96d56Sopenharmony_ci      application program, and whatever the method returns is what is passed
3347db96d56Sopenharmony_ci      back to the application as the value of the header being retrieved.
3357db96d56Sopenharmony_ci      Note that there may be more than one header with the same name stored in
3367db96d56Sopenharmony_ci      the ``Message``; the method is passed the specific name and value of the
3377db96d56Sopenharmony_ci      header destined to be returned to the application.
3387db96d56Sopenharmony_ci
3397db96d56Sopenharmony_ci      *value* may contain surrogateescaped binary data.  There should be no
3407db96d56Sopenharmony_ci      surrogateescaped binary data in the value returned by the method.
3417db96d56Sopenharmony_ci
3427db96d56Sopenharmony_ci      There is no default implementation
3437db96d56Sopenharmony_ci
3447db96d56Sopenharmony_ci
3457db96d56Sopenharmony_ci   .. method:: fold(name, value)
3467db96d56Sopenharmony_ci
3477db96d56Sopenharmony_ci      The email package calls this method with the *name* and *value* currently
3487db96d56Sopenharmony_ci      stored in the ``Message`` for a given header.  The method should return a
3497db96d56Sopenharmony_ci      string that represents that header "folded" correctly (according to the
3507db96d56Sopenharmony_ci      policy settings) by composing the *name* with the *value* and inserting
3517db96d56Sopenharmony_ci      :attr:`linesep` characters at the appropriate places.  See :rfc:`5322`
3527db96d56Sopenharmony_ci      for a discussion of the rules for folding email headers.
3537db96d56Sopenharmony_ci
3547db96d56Sopenharmony_ci      *value* may contain surrogateescaped binary data.  There should be no
3557db96d56Sopenharmony_ci      surrogateescaped binary data in the string returned by the method.
3567db96d56Sopenharmony_ci
3577db96d56Sopenharmony_ci
3587db96d56Sopenharmony_ci   .. method:: fold_binary(name, value)
3597db96d56Sopenharmony_ci
3607db96d56Sopenharmony_ci      The same as :meth:`fold`, except that the returned value should be a
3617db96d56Sopenharmony_ci      bytes object rather than a string.
3627db96d56Sopenharmony_ci
3637db96d56Sopenharmony_ci      *value* may contain surrogateescaped binary data.  These could be
3647db96d56Sopenharmony_ci      converted back into binary data in the returned bytes object.
3657db96d56Sopenharmony_ci
3667db96d56Sopenharmony_ci
3677db96d56Sopenharmony_ci
3687db96d56Sopenharmony_ci.. class:: EmailPolicy(**kw)
3697db96d56Sopenharmony_ci
3707db96d56Sopenharmony_ci   This concrete :class:`Policy` provides behavior that is intended to be fully
3717db96d56Sopenharmony_ci   compliant with the current email RFCs.  These include (but are not limited
3727db96d56Sopenharmony_ci   to) :rfc:`5322`, :rfc:`2047`, and the current MIME RFCs.
3737db96d56Sopenharmony_ci
3747db96d56Sopenharmony_ci   This policy adds new header parsing and folding algorithms.  Instead of
3757db96d56Sopenharmony_ci   simple strings, headers are ``str`` subclasses with attributes that depend
3767db96d56Sopenharmony_ci   on the type of the field.  The parsing and folding algorithm fully implement
3777db96d56Sopenharmony_ci   :rfc:`2047` and :rfc:`5322`.
3787db96d56Sopenharmony_ci
3797db96d56Sopenharmony_ci   The default value for the :attr:`~email.policy.Policy.message_factory`
3807db96d56Sopenharmony_ci   attribute is :class:`~email.message.EmailMessage`.
3817db96d56Sopenharmony_ci
3827db96d56Sopenharmony_ci   In addition to the settable attributes listed above that apply to all
3837db96d56Sopenharmony_ci   policies, this policy adds the following additional attributes:
3847db96d56Sopenharmony_ci
3857db96d56Sopenharmony_ci   .. versionadded:: 3.6 [1]_
3867db96d56Sopenharmony_ci
3877db96d56Sopenharmony_ci
3887db96d56Sopenharmony_ci   .. attribute:: utf8
3897db96d56Sopenharmony_ci
3907db96d56Sopenharmony_ci      If ``False``, follow :rfc:`5322`, supporting non-ASCII characters in
3917db96d56Sopenharmony_ci      headers by encoding them as "encoded words".  If ``True``, follow
3927db96d56Sopenharmony_ci      :rfc:`6532` and use ``utf-8`` encoding for headers.  Messages
3937db96d56Sopenharmony_ci      formatted in this way may be passed to SMTP servers that support
3947db96d56Sopenharmony_ci      the ``SMTPUTF8`` extension (:rfc:`6531`).
3957db96d56Sopenharmony_ci
3967db96d56Sopenharmony_ci
3977db96d56Sopenharmony_ci   .. attribute:: refold_source
3987db96d56Sopenharmony_ci
3997db96d56Sopenharmony_ci      If the value for a header in the ``Message`` object originated from a
4007db96d56Sopenharmony_ci      :mod:`~email.parser` (as opposed to being set by a program), this
4017db96d56Sopenharmony_ci      attribute indicates whether or not a generator should refold that value
4027db96d56Sopenharmony_ci      when transforming the message back into serialized form.  The possible
4037db96d56Sopenharmony_ci      values are:
4047db96d56Sopenharmony_ci
4057db96d56Sopenharmony_ci      ========  ===============================================================
4067db96d56Sopenharmony_ci      ``none``  all source values use original folding
4077db96d56Sopenharmony_ci
4087db96d56Sopenharmony_ci      ``long``  source values that have any line that is longer than
4097db96d56Sopenharmony_ci                ``max_line_length`` will be refolded
4107db96d56Sopenharmony_ci
4117db96d56Sopenharmony_ci      ``all``   all values are refolded.
4127db96d56Sopenharmony_ci      ========  ===============================================================
4137db96d56Sopenharmony_ci
4147db96d56Sopenharmony_ci      The default is ``long``.
4157db96d56Sopenharmony_ci
4167db96d56Sopenharmony_ci
4177db96d56Sopenharmony_ci   .. attribute:: header_factory
4187db96d56Sopenharmony_ci
4197db96d56Sopenharmony_ci      A callable that takes two arguments, ``name`` and ``value``, where
4207db96d56Sopenharmony_ci      ``name`` is a header field name and ``value`` is an unfolded header field
4217db96d56Sopenharmony_ci      value, and returns a string subclass that represents that header.  A
4227db96d56Sopenharmony_ci      default ``header_factory`` (see :mod:`~email.headerregistry`) is provided
4237db96d56Sopenharmony_ci      that supports custom parsing for the various address and date :RFC:`5322`
4247db96d56Sopenharmony_ci      header field types, and the major MIME header field stypes.  Support for
4257db96d56Sopenharmony_ci      additional custom parsing will be added in the future.
4267db96d56Sopenharmony_ci
4277db96d56Sopenharmony_ci
4287db96d56Sopenharmony_ci   .. attribute:: content_manager
4297db96d56Sopenharmony_ci
4307db96d56Sopenharmony_ci      An object with at least two methods: get_content and set_content.  When
4317db96d56Sopenharmony_ci      the :meth:`~email.message.EmailMessage.get_content` or
4327db96d56Sopenharmony_ci      :meth:`~email.message.EmailMessage.set_content` method of an
4337db96d56Sopenharmony_ci      :class:`~email.message.EmailMessage` object is called, it calls the
4347db96d56Sopenharmony_ci      corresponding method of this object, passing it the message object as its
4357db96d56Sopenharmony_ci      first argument, and any arguments or keywords that were passed to it as
4367db96d56Sopenharmony_ci      additional arguments.  By default ``content_manager`` is set to
4377db96d56Sopenharmony_ci      :data:`~email.contentmanager.raw_data_manager`.
4387db96d56Sopenharmony_ci
4397db96d56Sopenharmony_ci      .. versionadded:: 3.4
4407db96d56Sopenharmony_ci
4417db96d56Sopenharmony_ci
4427db96d56Sopenharmony_ci   The class provides the following concrete implementations of the abstract
4437db96d56Sopenharmony_ci   methods of :class:`Policy`:
4447db96d56Sopenharmony_ci
4457db96d56Sopenharmony_ci
4467db96d56Sopenharmony_ci   .. method:: header_max_count(name)
4477db96d56Sopenharmony_ci
4487db96d56Sopenharmony_ci      Returns the value of the
4497db96d56Sopenharmony_ci      :attr:`~email.headerregistry.BaseHeader.max_count` attribute of the
4507db96d56Sopenharmony_ci      specialized class used to represent the header with the given name.
4517db96d56Sopenharmony_ci
4527db96d56Sopenharmony_ci
4537db96d56Sopenharmony_ci   .. method:: header_source_parse(sourcelines)
4547db96d56Sopenharmony_ci
4557db96d56Sopenharmony_ci
4567db96d56Sopenharmony_ci      The name is parsed as everything up to the '``:``' and returned
4577db96d56Sopenharmony_ci      unmodified.  The value is determined by stripping leading whitespace off
4587db96d56Sopenharmony_ci      the remainder of the first line, joining all subsequent lines together,
4597db96d56Sopenharmony_ci      and stripping any trailing carriage return or linefeed characters.
4607db96d56Sopenharmony_ci
4617db96d56Sopenharmony_ci
4627db96d56Sopenharmony_ci   .. method:: header_store_parse(name, value)
4637db96d56Sopenharmony_ci
4647db96d56Sopenharmony_ci      The name is returned unchanged.  If the input value has a ``name``
4657db96d56Sopenharmony_ci      attribute and it matches *name* ignoring case, the value is returned
4667db96d56Sopenharmony_ci      unchanged.  Otherwise the *name* and *value* are passed to
4677db96d56Sopenharmony_ci      ``header_factory``, and the resulting header object is returned as
4687db96d56Sopenharmony_ci      the value.  In this case a ``ValueError`` is raised if the input value
4697db96d56Sopenharmony_ci      contains CR or LF characters.
4707db96d56Sopenharmony_ci
4717db96d56Sopenharmony_ci
4727db96d56Sopenharmony_ci   .. method:: header_fetch_parse(name, value)
4737db96d56Sopenharmony_ci
4747db96d56Sopenharmony_ci      If the value has a ``name`` attribute, it is returned to unmodified.
4757db96d56Sopenharmony_ci      Otherwise the *name*, and the *value* with any CR or LF characters
4767db96d56Sopenharmony_ci      removed, are passed to the ``header_factory``, and the resulting
4777db96d56Sopenharmony_ci      header object is returned.  Any surrogateescaped bytes get turned into
4787db96d56Sopenharmony_ci      the unicode unknown-character glyph.
4797db96d56Sopenharmony_ci
4807db96d56Sopenharmony_ci
4817db96d56Sopenharmony_ci   .. method:: fold(name, value)
4827db96d56Sopenharmony_ci
4837db96d56Sopenharmony_ci      Header folding is controlled by the :attr:`refold_source` policy setting.
4847db96d56Sopenharmony_ci      A value is considered to be a 'source value' if and only if it does not
4857db96d56Sopenharmony_ci      have a ``name`` attribute (having a ``name`` attribute means it is a
4867db96d56Sopenharmony_ci      header object of some sort).  If a source value needs to be refolded
4877db96d56Sopenharmony_ci      according to the policy, it is converted into a header object by
4887db96d56Sopenharmony_ci      passing the *name* and the *value* with any CR and LF characters removed
4897db96d56Sopenharmony_ci      to the ``header_factory``.  Folding of a header object is done by
4907db96d56Sopenharmony_ci      calling its ``fold`` method with the current policy.
4917db96d56Sopenharmony_ci
4927db96d56Sopenharmony_ci      Source values are split into lines using :meth:`~str.splitlines`.  If
4937db96d56Sopenharmony_ci      the value is not to be refolded, the lines are rejoined using the
4947db96d56Sopenharmony_ci      ``linesep`` from the policy and returned.  The exception is lines
4957db96d56Sopenharmony_ci      containing non-ascii binary data.  In that case the value is refolded
4967db96d56Sopenharmony_ci      regardless of the ``refold_source`` setting, which causes the binary data
4977db96d56Sopenharmony_ci      to be CTE encoded using the ``unknown-8bit`` charset.
4987db96d56Sopenharmony_ci
4997db96d56Sopenharmony_ci
5007db96d56Sopenharmony_ci   .. method:: fold_binary(name, value)
5017db96d56Sopenharmony_ci
5027db96d56Sopenharmony_ci      The same as :meth:`fold` if :attr:`~Policy.cte_type` is ``7bit``, except
5037db96d56Sopenharmony_ci      that the returned value is bytes.
5047db96d56Sopenharmony_ci
5057db96d56Sopenharmony_ci      If :attr:`~Policy.cte_type` is ``8bit``, non-ASCII binary data is
5067db96d56Sopenharmony_ci      converted back
5077db96d56Sopenharmony_ci      into bytes.  Headers with binary data are not refolded, regardless of the
5087db96d56Sopenharmony_ci      ``refold_header`` setting, since there is no way to know whether the
5097db96d56Sopenharmony_ci      binary data consists of single byte characters or multibyte characters.
5107db96d56Sopenharmony_ci
5117db96d56Sopenharmony_ci
5127db96d56Sopenharmony_ciThe following instances of :class:`EmailPolicy` provide defaults suitable for
5137db96d56Sopenharmony_cispecific application domains.  Note that in the future the behavior of these
5147db96d56Sopenharmony_ciinstances (in particular the ``HTTP`` instance) may be adjusted to conform even
5157db96d56Sopenharmony_cimore closely to the RFCs relevant to their domains.
5167db96d56Sopenharmony_ci
5177db96d56Sopenharmony_ci
5187db96d56Sopenharmony_ci.. data:: default
5197db96d56Sopenharmony_ci
5207db96d56Sopenharmony_ci   An instance of ``EmailPolicy`` with all defaults unchanged.  This policy
5217db96d56Sopenharmony_ci   uses the standard Python ``\n`` line endings rather than the RFC-correct
5227db96d56Sopenharmony_ci   ``\r\n``.
5237db96d56Sopenharmony_ci
5247db96d56Sopenharmony_ci
5257db96d56Sopenharmony_ci.. data:: SMTP
5267db96d56Sopenharmony_ci
5277db96d56Sopenharmony_ci   Suitable for serializing messages in conformance with the email RFCs.
5287db96d56Sopenharmony_ci   Like ``default``, but with ``linesep`` set to ``\r\n``, which is RFC
5297db96d56Sopenharmony_ci   compliant.
5307db96d56Sopenharmony_ci
5317db96d56Sopenharmony_ci
5327db96d56Sopenharmony_ci.. data:: SMTPUTF8
5337db96d56Sopenharmony_ci
5347db96d56Sopenharmony_ci   The same as ``SMTP`` except that :attr:`~EmailPolicy.utf8` is ``True``.
5357db96d56Sopenharmony_ci   Useful for serializing messages to a message store without using encoded
5367db96d56Sopenharmony_ci   words in the headers.  Should only be used for SMTP transmission if the
5377db96d56Sopenharmony_ci   sender or recipient addresses have non-ASCII characters (the
5387db96d56Sopenharmony_ci   :meth:`smtplib.SMTP.send_message` method handles this automatically).
5397db96d56Sopenharmony_ci
5407db96d56Sopenharmony_ci
5417db96d56Sopenharmony_ci.. data:: HTTP
5427db96d56Sopenharmony_ci
5437db96d56Sopenharmony_ci   Suitable for serializing headers with for use in HTTP traffic.  Like
5447db96d56Sopenharmony_ci   ``SMTP`` except that ``max_line_length`` is set to ``None`` (unlimited).
5457db96d56Sopenharmony_ci
5467db96d56Sopenharmony_ci
5477db96d56Sopenharmony_ci.. data:: strict
5487db96d56Sopenharmony_ci
5497db96d56Sopenharmony_ci   Convenience instance.  The same as ``default`` except that
5507db96d56Sopenharmony_ci   ``raise_on_defect`` is set to ``True``.  This allows any policy to be made
5517db96d56Sopenharmony_ci   strict by writing::
5527db96d56Sopenharmony_ci
5537db96d56Sopenharmony_ci        somepolicy + policy.strict
5547db96d56Sopenharmony_ci
5557db96d56Sopenharmony_ci
5567db96d56Sopenharmony_ciWith all of these :class:`EmailPolicies <.EmailPolicy>`, the effective API of
5577db96d56Sopenharmony_cithe email package is changed from the Python 3.2 API in the following ways:
5587db96d56Sopenharmony_ci
5597db96d56Sopenharmony_ci   * Setting a header on a :class:`~email.message.Message` results in that
5607db96d56Sopenharmony_ci     header being parsed and a header object created.
5617db96d56Sopenharmony_ci
5627db96d56Sopenharmony_ci   * Fetching a header value from a :class:`~email.message.Message` results
5637db96d56Sopenharmony_ci     in that header being parsed and a header object created and
5647db96d56Sopenharmony_ci     returned.
5657db96d56Sopenharmony_ci
5667db96d56Sopenharmony_ci   * Any header object, or any header that is refolded due to the
5677db96d56Sopenharmony_ci     policy settings, is folded using an algorithm that fully implements the
5687db96d56Sopenharmony_ci     RFC folding algorithms, including knowing where encoded words are required
5697db96d56Sopenharmony_ci     and allowed.
5707db96d56Sopenharmony_ci
5717db96d56Sopenharmony_ciFrom the application view, this means that any header obtained through the
5727db96d56Sopenharmony_ci:class:`~email.message.EmailMessage` is a header object with extra
5737db96d56Sopenharmony_ciattributes, whose string value is the fully decoded unicode value of the
5747db96d56Sopenharmony_ciheader.  Likewise, a header may be assigned a new value, or a new header
5757db96d56Sopenharmony_cicreated, using a unicode string, and the policy will take care of converting
5767db96d56Sopenharmony_cithe unicode string into the correct RFC encoded form.
5777db96d56Sopenharmony_ci
5787db96d56Sopenharmony_ciThe header objects and their attributes are described in
5797db96d56Sopenharmony_ci:mod:`~email.headerregistry`.
5807db96d56Sopenharmony_ci
5817db96d56Sopenharmony_ci
5827db96d56Sopenharmony_ci
5837db96d56Sopenharmony_ci.. class:: Compat32(**kw)
5847db96d56Sopenharmony_ci
5857db96d56Sopenharmony_ci   This concrete :class:`Policy` is the backward compatibility policy.  It
5867db96d56Sopenharmony_ci   replicates the behavior of the email package in Python 3.2.  The
5877db96d56Sopenharmony_ci   :mod:`~email.policy` module also defines an instance of this class,
5887db96d56Sopenharmony_ci   :const:`compat32`, that is used as the default policy.  Thus the default
5897db96d56Sopenharmony_ci   behavior of the email package is to maintain compatibility with Python 3.2.
5907db96d56Sopenharmony_ci
5917db96d56Sopenharmony_ci   The following attributes have values that are different from the
5927db96d56Sopenharmony_ci   :class:`Policy` default:
5937db96d56Sopenharmony_ci
5947db96d56Sopenharmony_ci
5957db96d56Sopenharmony_ci   .. attribute:: mangle_from_
5967db96d56Sopenharmony_ci
5977db96d56Sopenharmony_ci      The default is ``True``.
5987db96d56Sopenharmony_ci
5997db96d56Sopenharmony_ci
6007db96d56Sopenharmony_ci   The class provides the following concrete implementations of the
6017db96d56Sopenharmony_ci   abstract methods of :class:`Policy`:
6027db96d56Sopenharmony_ci
6037db96d56Sopenharmony_ci
6047db96d56Sopenharmony_ci   .. method:: header_source_parse(sourcelines)
6057db96d56Sopenharmony_ci
6067db96d56Sopenharmony_ci      The name is parsed as everything up to the '``:``' and returned
6077db96d56Sopenharmony_ci      unmodified.  The value is determined by stripping leading whitespace off
6087db96d56Sopenharmony_ci      the remainder of the first line, joining all subsequent lines together,
6097db96d56Sopenharmony_ci      and stripping any trailing carriage return or linefeed characters.
6107db96d56Sopenharmony_ci
6117db96d56Sopenharmony_ci
6127db96d56Sopenharmony_ci   .. method:: header_store_parse(name, value)
6137db96d56Sopenharmony_ci
6147db96d56Sopenharmony_ci      The name and value are returned unmodified.
6157db96d56Sopenharmony_ci
6167db96d56Sopenharmony_ci
6177db96d56Sopenharmony_ci   .. method:: header_fetch_parse(name, value)
6187db96d56Sopenharmony_ci
6197db96d56Sopenharmony_ci      If the value contains binary data, it is converted into a
6207db96d56Sopenharmony_ci      :class:`~email.header.Header` object using the ``unknown-8bit`` charset.
6217db96d56Sopenharmony_ci      Otherwise it is returned unmodified.
6227db96d56Sopenharmony_ci
6237db96d56Sopenharmony_ci
6247db96d56Sopenharmony_ci   .. method:: fold(name, value)
6257db96d56Sopenharmony_ci
6267db96d56Sopenharmony_ci      Headers are folded using the :class:`~email.header.Header` folding
6277db96d56Sopenharmony_ci      algorithm, which preserves existing line breaks in the value, and wraps
6287db96d56Sopenharmony_ci      each resulting line to the ``max_line_length``.  Non-ASCII binary data are
6297db96d56Sopenharmony_ci      CTE encoded using the ``unknown-8bit`` charset.
6307db96d56Sopenharmony_ci
6317db96d56Sopenharmony_ci
6327db96d56Sopenharmony_ci   .. method:: fold_binary(name, value)
6337db96d56Sopenharmony_ci
6347db96d56Sopenharmony_ci      Headers are folded using the :class:`~email.header.Header` folding
6357db96d56Sopenharmony_ci      algorithm, which preserves existing line breaks in the value, and wraps
6367db96d56Sopenharmony_ci      each resulting line to the ``max_line_length``.  If ``cte_type`` is
6377db96d56Sopenharmony_ci      ``7bit``, non-ascii binary data is CTE encoded using the ``unknown-8bit``
6387db96d56Sopenharmony_ci      charset.  Otherwise the original source header is used, with its existing
6397db96d56Sopenharmony_ci      line breaks and any (RFC invalid) binary data it may contain.
6407db96d56Sopenharmony_ci
6417db96d56Sopenharmony_ci
6427db96d56Sopenharmony_ci.. data:: compat32
6437db96d56Sopenharmony_ci
6447db96d56Sopenharmony_ci   An instance of :class:`Compat32`, providing  backward compatibility with the
6457db96d56Sopenharmony_ci   behavior of the email package in Python 3.2.
6467db96d56Sopenharmony_ci
6477db96d56Sopenharmony_ci
6487db96d56Sopenharmony_ci.. rubric:: Footnotes
6497db96d56Sopenharmony_ci
6507db96d56Sopenharmony_ci.. [1] Originally added in 3.3 as a :term:`provisional feature <provisional
6517db96d56Sopenharmony_ci       package>`.
652