The email module’s prime focus is the handling of email messages as described by the various email and MIME RFCs. However, the general format of email messages (a block of header fields each consisting of a name followed by a colon followed by a value, the whole block followed by a blank line and an arbitrary ‘body’), is a format that has found utility outside of the realm of email. Some of these uses hew closely to the main RFCs, some do not. And even when working with email, there are times when it is desirable to break strict compliance with the RFCs.
Policy Objects are the mechanism used to provide the email package with the flexibility to handle all these disparate use cases,
A policy object encapsulates a set of attributes and methods that control the behavior of the various components of the email package during use. There are two base classes for policy objects: Policy and PolicySet. Policy provides a constructor that allows the setting of policy attributes via keyword arguments, but no default values or default method implementations. PolicySet provides a full set of default values and method implementations.
Almost every object created by the email package will have an associated policy attribute, which should be a PolicySet instance. When an application wishes to change the default policy for all objects it creates, it can create an instance of PolicySet (or of a subclass of it) with the non-default attribute values overridden, and use that to initialize the policy attribute of the objects it creates. For the most part the setting of the policy attribute on sub-objects happens automatically during the construction of a message, so in practice this means that the application-specific default policy needs to be passed to the message class constructor.
Many of the email package object methods accept a policy keyword-only argument. Under most circumstances the correct thing to pass to this argument is an instance of Policy (or of a subclass of it). Any attributes (or methods for a subclass) present on the Policy instance will override those from the object’s default policy.
Objects in the email package that have a policy attribute also have a policy_override attribute. This attribute is usually None, but can contain (or be set to) an object derived from Policy. Any attributes or methods on the policy_override object will override the corresponding attributes from the default policy. Certain object types use this to override certain defaults[1]. This mechanism can also be used by an application to set overrides on objects before attaching them to other objects (for example, setting an override on a header object before passing it to a message’s set_header() method).
If an application wants to ignore all policy_overrides during a given method call, it can pass a PolicySet-derived instance to the object’s method. Since the PolicySet defines all attributes and methods, none will be referred to the default policy on the object, and the object’s object-specific defaults will be ignored. (Note that this is is a very rare use case.)
In addition to the base classes already mentioned, the module provides a number of convenience subclasses for commonly used default policy overrides. These classes are subclasses of Policy. To use them as default policies, pass them to the constructor of a PolicySet class, which will copy the values of the attributes (they have no methods). Policy objects can also be composed in the same way; for example, a strict SMTP policy can be created by doing SMTP(Strict()) or Strict(SMTP()).
If an object is passed in as policy_overlay, it will be checked for any of the attributes listed for PolicySet, and if the attribute exists its value will be used as the value of that attribute for the Policy instance returned by the constructor.
The valid constructor keyword arguments are any of the attributes listed in policy_attributes, which is any of the attributes documented in PolicySet, below. If an attribute occurs both on the policy_overlay object and as a keyword argument, the keyword argument takes precedence.
If an object is passed in as policy_overlay, it will be checked for any of the attributes listed below, and if the attribute exists its value will be used as the value of that attribute for the PolicySet instance returned by the constructor.
The valid constructor keyword arguments are any of the attributes listed in policy_attributes, which is any of the attributes documented below. If an attribute occurs both on the policy_overlay object and as a keyword argument, the keyword argument takes precedence.
This policy attribute affects only bytes objects.
If True, data output by serialize is limited to ASCII characters. If False (the default), then bytes with the high bit set are allowed in certain contexts (for example, where possible a content transfer encoding of 8bit will be used).
These are subclasses of Policy.
SMTP
Output serialized from a bytes message will conform to the email and SMTP RFCs. The only changed attribute is newline, which is set to ‘\r\n’.
HTTP
Suitable for use when serializing headers for use in HTTP traffic. newline is set to ‘\r\n’, and max_line_length is set to None (unlimited).
Strict
raise_on_defect is set to True.
Footnotes
[1] | For example, the MIME signed message object will set the use_raw_data_if_possible policy attribute to True, so that the original data is preserved, as required by RFC 1847, even if the policy otherwise calls for rewrapping lines (as PolicySet does by default). |