blob: 31b13c15f10ed29d916b63404e1974d6b086db31 [file] [log] [blame]
R David Murray79cf3ba2012-05-27 17:10:36 -04001:mod:`email.policy`: Policy Objects
2-----------------------------------
R David Murray3edd22a2011-04-18 13:59:37 -04003
4.. module:: email.policy
5 :synopsis: Controlling the parsing and generating of messages
6
R David Murray79cf3ba2012-05-27 17:10:36 -04007.. moduleauthor:: R. David Murray <rdmurray@bitdance.com>
8.. sectionauthor:: R. David Murray <rdmurray@bitdance.com>
9
Éric Araujo54dbfbd2011-08-10 21:43:13 +020010.. versionadded:: 3.3
R David Murray6a45d3b2011-04-18 16:00:47 -040011
R David Murray3edd22a2011-04-18 13:59:37 -040012
13The :mod:`email` package's prime focus is the handling of email messages as
14described by the various email and MIME RFCs. However, the general format of
15email messages (a block of header fields each consisting of a name followed by
16a colon followed by a value, the whole block followed by a blank line and an
17arbitrary 'body'), is a format that has found utility outside of the realm of
18email. Some of these uses conform fairly closely to the main RFCs, some do
19not. And even when working with email, there are times when it is desirable to
20break strict compliance with the RFCs.
21
R David Murray6a45d3b2011-04-18 16:00:47 -040022Policy objects give the email package the flexibility to handle all these
23disparate use cases.
R David Murray3edd22a2011-04-18 13:59:37 -040024
25A :class:`Policy` object encapsulates a set of attributes and methods that
26control the behavior of various components of the email package during use.
27:class:`Policy` instances can be passed to various classes and methods in the
28email package to alter the default behavior. The settable values and their
R David Murrayc27e5222012-05-25 15:01:48 -040029defaults are described below.
R David Murray3edd22a2011-04-18 13:59:37 -040030
R David Murrayc27e5222012-05-25 15:01:48 -040031There is a default policy used by all classes in the email package. This
32policy is named :class:`Compat32`, with a corresponding pre-defined instance
33named :const:`compat32`. It provides for complete backward compatibility (in
34some cases, including bug compatibility) with the pre-Python3.3 version of the
35email package.
36
37The first part of this documentation covers the features of :class:`Policy`, an
38:term:`abstract base class` that defines the features that are common to all
39policy objects, including :const:`compat32`. This includes certain hook
40methods that are called internally by the email package, which a custom policy
41could override to obtain different behavior.
42
43When a :class:`~email.message.Message` object is created, it acquires a policy.
44By default this will be :const:`compat32`, but a different policy can be
45specified. If the ``Message`` is created by a :mod:`~email.parser`, a policy
46passed to the parser will be the policy used by the ``Message`` it creates. If
47the ``Message`` is created by the program, then the policy can be specified
48when it is created. When a ``Message`` is passed to a :mod:`~email.generator`,
49the generator uses the policy from the ``Message`` by default, but you can also
50pass a specific policy to the generator that will override the one stored on
51the ``Message`` object.
52
53:class:`Policy` instances are immutable, but they can be cloned, accepting the
54same keyword arguments as the class constructor and returning a new
55:class:`Policy` instance that is a copy of the original but with the specified
56attributes values changed.
R David Murray3edd22a2011-04-18 13:59:37 -040057
58As an example, the following code could be used to read an email message from a
R David Murray6a45d3b2011-04-18 16:00:47 -040059file on disk and pass it to the system ``sendmail`` program on a Unix system::
R David Murray3edd22a2011-04-18 13:59:37 -040060
61 >>> from email import msg_from_binary_file
62 >>> from email.generator import BytesGenerator
R David Murray3edd22a2011-04-18 13:59:37 -040063 >>> from subprocess import Popen, PIPE
64 >>> with open('mymsg.txt', 'b') as f:
R David Murrayc27e5222012-05-25 15:01:48 -040065 ... msg = msg_from_binary_file(f)
R David Murray3edd22a2011-04-18 13:59:37 -040066 >>> p = Popen(['sendmail', msg['To'][0].address], stdin=PIPE)
R David Murrayc27e5222012-05-25 15:01:48 -040067 >>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n'))
R David Murray3edd22a2011-04-18 13:59:37 -040068 >>> g.flatten(msg)
69 >>> p.stdin.close()
70 >>> rc = p.wait()
71
R David Murrayc27e5222012-05-25 15:01:48 -040072Here we are telling :class:`~email.generator.BytesGenerator` to use the RFC
73correct line separator characters when creating the binary string to feed into
74``sendmail's`` ``stdin``, where the default policy would use ``\n`` line
75separators.
Éric Araujofe0472e2011-12-03 16:00:56 +010076
R David Murray3edd22a2011-04-18 13:59:37 -040077Some email package methods accept a *policy* keyword argument, allowing the
R David Murray6a45d3b2011-04-18 16:00:47 -040078policy to be overridden for that method. For example, the following code uses
R David Murrayc27e5222012-05-25 15:01:48 -040079the :meth:`~email.message.Message.as_string` method of the *msg* object from
80the previous example and writes the message to a file using the native line
81separators for the platform on which it is running::
R David Murray3edd22a2011-04-18 13:59:37 -040082
83 >>> import os
R David Murray3edd22a2011-04-18 13:59:37 -040084 >>> with open('converted.txt', 'wb') as f:
R David Murrayc27e5222012-05-25 15:01:48 -040085 ... f.write(msg.as_string(policy=msg.policy.clone(linesep=os.linesep))
R David Murray3edd22a2011-04-18 13:59:37 -040086
87Policy objects can also be combined using the addition operator, producing a
88policy object whose settings are a combination of the non-default values of the
89summed objects::
90
R David Murrayc27e5222012-05-25 15:01:48 -040091 >>> compat_SMTP = email.policy.clone(linesep='\r\n')
92 >>> compat_strict = email.policy.clone(raise_on_defect=True)
93 >>> compat_strict_SMTP = compat_SMTP + compat_strict
R David Murray3edd22a2011-04-18 13:59:37 -040094
95This operation is not commutative; that is, the order in which the objects are
96added matters. To illustrate::
97
R David Murrayc27e5222012-05-25 15:01:48 -040098 >>> policy100 = compat32.clone(max_line_length=100)
99 >>> policy80 = compat32.clone(max_line_length=80)
100 >>> apolicy = policy100 + Policy80
R David Murray3edd22a2011-04-18 13:59:37 -0400101 >>> apolicy.max_line_length
102 80
R David Murrayc27e5222012-05-25 15:01:48 -0400103 >>> apolicy = policy80 + policy100
R David Murray3edd22a2011-04-18 13:59:37 -0400104 >>> apolicy.max_line_length
105 100
106
107
108.. class:: Policy(**kw)
109
R David Murrayc27e5222012-05-25 15:01:48 -0400110 This is the :term:`abstract base class` for all policy classes. It provides
111 default implementations for a couple of trivial methods, as well as the
112 implementation of the immutability property, the :meth:`clone` method, and
113 the constructor semantics.
114
115 The constructor of a policy class can be passed various keyword arguments.
116 The arguments that may be specified are any non-method properties on this
117 class, plus any additional non-method properties on the concrete class. A
118 value specified in the constructor will override the default value for the
119 corresponding attribute.
120
121 This class defines the following properties, and thus values for the
122 following may be passed in the constructor of any policy class:
R David Murray3edd22a2011-04-18 13:59:37 -0400123
124 .. attribute:: max_line_length
125
126 The maximum length of any line in the serialized output, not counting the
127 end of line character(s). Default is 78, per :rfc:`5322`. A value of
128 ``0`` or :const:`None` indicates that no line wrapping should be
129 done at all.
130
131 .. attribute:: linesep
132
133 The string to be used to terminate lines in serialized output. The
R David Murray6a45d3b2011-04-18 16:00:47 -0400134 default is ``\n`` because that's the internal end-of-line discipline used
R David Murrayc27e5222012-05-25 15:01:48 -0400135 by Python, though ``\r\n`` is required by the RFCs.
R David Murray3edd22a2011-04-18 13:59:37 -0400136
R David Murrayc27e5222012-05-25 15:01:48 -0400137 .. attribute:: cte_type
R David Murray3edd22a2011-04-18 13:59:37 -0400138
R David Murrayc27e5222012-05-25 15:01:48 -0400139 Controls the type of Content Transfer Encodings that may be or are
140 required to be used. The possible values are:
141
142 ======== ===============================================================
143 ``7bit`` all data must be "7 bit clean" (ASCII-only). This means that
144 where necessary data will be encoded using either
145 quoted-printable or base64 encoding.
146
147 ``8bit`` data is not constrained to be 7 bit clean. Data in headers is
148 still required to be ASCII-only and so will be encoded (see
149 'binary_fold' below for an exception), but body parts may use
150 the ``8bit`` CTE.
151 ======== ===============================================================
152
153 A ``cte_type`` value of ``8bit`` only works with ``BytesGenerator``, not
154 ``Generator``, because strings cannot contain binary data. If a
155 ``Generator`` is operating under a policy that specifies
156 ``cte_type=8bit``, it will act as if ``cte_type`` is ``7bit``.
R David Murray3edd22a2011-04-18 13:59:37 -0400157
158 .. attribute:: raise_on_defect
159
160 If :const:`True`, any defects encountered will be raised as errors. If
161 :const:`False` (the default), defects will be passed to the
162 :meth:`register_defect` method.
163
R David Murrayc27e5222012-05-25 15:01:48 -0400164 The following :class:`Policy` method is intended to be called by code using
165 the email library to create policy instances with custom settings:
R David Murray6a45d3b2011-04-18 16:00:47 -0400166
R David Murrayc27e5222012-05-25 15:01:48 -0400167 .. method:: clone(**kw)
R David Murray3edd22a2011-04-18 13:59:37 -0400168
169 Return a new :class:`Policy` instance whose attributes have the same
170 values as the current instance, except where those attributes are
171 given new values by the keyword arguments.
172
R David Murrayc27e5222012-05-25 15:01:48 -0400173 The remaining :class:`Policy` methods are called by the email package code,
174 and are not intended to be called by an application using the email package.
175 A custom policy must implement all of these methods.
R David Murray3edd22a2011-04-18 13:59:37 -0400176
R David Murrayc27e5222012-05-25 15:01:48 -0400177 .. method:: handle_defect(obj, defect)
R David Murray3edd22a2011-04-18 13:59:37 -0400178
R David Murrayc27e5222012-05-25 15:01:48 -0400179 Handle a *defect* found on *obj*. When the email package calls this
180 method, *defect* will always be a subclass of
181 :class:`~email.errors.Defect`.
R David Murray3edd22a2011-04-18 13:59:37 -0400182
R David Murrayc27e5222012-05-25 15:01:48 -0400183 The default implementation checks the :attr:`raise_on_defect` flag. If
184 it is ``True``, *defect* is raised as an exception. If it is ``False``
185 (the default), *obj* and *defect* are passed to :meth:`register_defect`.
R David Murray3edd22a2011-04-18 13:59:37 -0400186
R David Murrayc27e5222012-05-25 15:01:48 -0400187 .. method:: register_defect(obj, defect)
R David Murray3edd22a2011-04-18 13:59:37 -0400188
R David Murrayc27e5222012-05-25 15:01:48 -0400189 Register a *defect* on *obj*. In the email package, *defect* will always
190 be a subclass of :class:`~email.errors.Defect`.
R David Murray3edd22a2011-04-18 13:59:37 -0400191
R David Murrayc27e5222012-05-25 15:01:48 -0400192 The default implementation calls the ``append`` method of the ``defects``
193 attribute of *obj*. When the email package calls :attr:`handle_defect`,
194 *obj* will normally have a ``defects`` attribute that has an ``append``
195 method. Custom object types used with the email package (for example,
196 custom ``Message`` objects) should also provide such an attribute,
197 otherwise defects in parsed messages will raise unexpected errors.
R David Murray3edd22a2011-04-18 13:59:37 -0400198
R David Murrayabfc3742012-05-29 09:14:44 -0400199 .. method:: header_max_count(name)
200
201 Return the maximum allowed number of headers named *name*.
202
203 Called when a header is added to a :class:`~email.message.Message`
204 object. If the returned value is not ``0`` or ``None``, and there are
205 already a number of headers with the name *name* equal to the value
206 returned, a :exc:`ValueError` is raised.
207
208 Because the default behavior of ``Message.__setitem__`` is to append the
209 value to the list of headers, it is easy to create duplicate headers
210 without realizing it. This method allows certain headers to be limited
211 in the number of instances of that header that may be added to a
212 ``Message`` programmatically. (The limit is not observed by the parser,
213 which will faithfully produce as many headers as exist in the message
214 being parsed.)
215
216 The default implementation returns ``None`` for all header names.
217
R David Murrayc27e5222012-05-25 15:01:48 -0400218 .. method:: header_source_parse(sourcelines)
R David Murray3edd22a2011-04-18 13:59:37 -0400219
R David Murrayc27e5222012-05-25 15:01:48 -0400220 The email package calls this method with a list of strings, each string
221 ending with the line separation characters found in the source being
222 parsed. The first line includes the field header name and separator.
223 All whitespace in the source is preserved. The method should return the
224 ``(name, value)`` tuple that is to be stored in the ``Message`` to
225 represent the parsed header.
R David Murray3edd22a2011-04-18 13:59:37 -0400226
R David Murrayc27e5222012-05-25 15:01:48 -0400227 If an implementation wishes to retain compatibility with the existing
228 email package policies, *name* should be the case preserved name (all
229 characters up to the '``:``' separator), while *value* should be the
230 unfolded value (all line separator characters removed, but whitespace
231 kept intact), stripped of leading whitespace.
R David Murray3edd22a2011-04-18 13:59:37 -0400232
R David Murrayc27e5222012-05-25 15:01:48 -0400233 *sourcelines* may contain surrogateescaped binary data.
234
235 There is no default implementation
236
237 .. method:: header_store_parse(name, value)
238
239 The email package calls this method with the name and value provided by
240 the application program when the application program is modifying a
241 ``Message`` programmatically (as opposed to a ``Message`` created by a
242 parser). The method should return the ``(name, value)`` tuple that is to
243 be stored in the ``Message`` to represent the header.
244
245 If an implementation wishes to retain compatibility with the existing
246 email package policies, the *name* and *value* should be strings or
247 string subclasses that do not change the content of the passed in
248 arguments.
249
250 There is no default implementation
251
252 .. method:: header_fetch_parse(name, value)
253
254 The email package calls this method with the *name* and *value* currently
255 stored in the ``Message`` when that header is requested by the
256 application program, and whatever the method returns is what is passed
257 back to the application as the value of the header being retrieved.
258 Note that there may be more than one header with the same name stored in
259 the ``Message``; the method is passed the specific name and value of the
260 header destined to be returned to the application.
261
262 *value* may contain surrogateescaped binary data. There should be no
263 surrogateescaped binary data in the value returned by the method.
264
265 There is no default implementation
266
267 .. method:: fold(name, value)
268
269 The email package calls this method with the *name* and *value* currently
270 stored in the ``Message`` for a given header. The method should return a
271 string that represents that header "folded" correctly (according to the
272 policy settings) by composing the *name* with the *value* and inserting
273 :attr:`linesep` characters at the appropriate places. See :rfc:`5322`
274 for a discussion of the rules for folding email headers.
275
276 *value* may contain surrogateescaped binary data. There should be no
277 surrogateescaped binary data in the string returned by the method.
278
279 .. method:: fold_binary(name, value)
280
281 The same as :meth:`fold`, except that the returned value should be a
282 bytes object rather than a string.
283
284 *value* may contain surrogateescaped binary data. These could be
285 converted back into binary data in the returned bytes object.
286
287
288.. class:: Compat32(**kw)
289
290 This concrete :class:`Policy` is the backward compatibility policy. It
291 replicates the behavior of the email package in Python 3.2. The
292 :mod:`policy` module also defines an instance of this class,
293 :const:`compat32`, that is used as the default policy. Thus the default
294 behavior of the email package is to maintain compatibility with Python 3.2.
295
296 The class provides the following concrete implementations of the
297 abstract methods of :class:`Policy`:
298
299 .. method:: header_source_parse(sourcelines)
300
301 The name is parsed as everything up to the '``:``' and returned
302 unmodified. The value is determined by stripping leading whitespace off
303 the remainder of the first line, joining all subsequent lines together,
304 and stripping any trailing carriage return or linefeed characters.
305
306 .. method:: header_store_parse(name, value)
307
308 The name and value are returned unmodified.
309
310 .. method:: header_fetch_parse(name, value)
311
312 If the value contains binary data, it is converted into a
313 :class:`~email.header.Header` object using the ``unknown-8bit`` charset.
314 Otherwise it is returned unmodified.
315
316 .. method:: fold(name, value)
317
318 Headers are folded using the :class:`~email.header.Header` folding
319 algorithm, which preserves existing line breaks in the value, and wraps
320 each resulting line to the ``max_line_length``. Non-ASCII binary data are
321 CTE encoded using the ``unknown-8bit`` charset.
322
323 .. method:: fold_binary(name, value)
324
325 Headers are folded using the :class:`~email.header.Header` folding
326 algorithm, which preserves existing line breaks in the value, and wraps
327 each resulting line to the ``max_line_length``. If ``cte_type`` is
328 ``7bit``, non-ascii binary data is CTE encoded using the ``unknown-8bit``
329 charset. Otherwise the original source header is used, with its existing
330 line breaks and and any (RFC invalid) binary data it may contain.
R David Murray0b6f6c82012-05-25 18:42:14 -0400331
332
333.. note::
334
R David Murrayea976682012-05-27 15:03:38 -0400335 The documentation below describes new policies that are included in the
336 standard library on a :term:`provisional basis <provisional package>`.
337 Backwards incompatible changes (up to and including removal of the feature)
338 may occur if deemed necessary by the core developers.
R David Murray0b6f6c82012-05-25 18:42:14 -0400339
340
341.. class:: EmailPolicy(**kw)
342
343 This concrete :class:`Policy` provides behavior that is intended to be fully
344 compliant with the current email RFCs. These include (but are not limited
345 to) :rfc:`5322`, :rfc:`2047`, and the current MIME RFCs.
346
347 This policy adds new header parsing and folding algorithms. Instead of
348 simple strings, headers are custom objects with custom attributes depending
349 on the type of the field. The parsing and folding algorithm fully implement
350 :rfc:`2047` and :rfc:`5322`.
351
352 In addition to the settable attributes listed above that apply to all
353 policies, this policy adds the following additional attributes:
354
355 .. attribute:: refold_source
356
357 If the value for a header in the ``Message`` object originated from a
358 :mod:`~email.parser` (as opposed to being set by a program), this
359 attribute indicates whether or not a generator should refold that value
360 when transforming the message back into stream form. The possible values
361 are:
362
363 ======== ===============================================================
364 ``none`` all source values use original folding
365
366 ``long`` source values that have any line that is longer than
367 ``max_line_length`` will be refolded
368
369 ``all`` all values are refolded.
370 ======== ===============================================================
371
372 The default is ``long``.
373
374 .. attribute:: header_factory
375
376 A callable that takes two arguments, ``name`` and ``value``, where
377 ``name`` is a header field name and ``value`` is an unfolded header field
R David Murrayea976682012-05-27 15:03:38 -0400378 value, and returns a string subclass that represents that header. A
379 default ``header_factory`` (see :mod:`~email.headerregistry`) is provided
380 that understands some of the :RFC:`5322` header field types. (Currently
381 address fields and date fields have special treatment, while all other
382 fields are treated as unstructured. This list will be completed before
383 the extension is marked stable.)
R David Murray0b6f6c82012-05-25 18:42:14 -0400384
385 The class provides the following concrete implementations of the abstract
386 methods of :class:`Policy`:
387
R David Murrayabfc3742012-05-29 09:14:44 -0400388 .. method:: header_max_count(name)
389
390 Returns the value of the
391 :attr:`~email.headerregistry.BaseHeader.max_count` attribute of the
392 specialized class used to represent the header with the given name.
393
R David Murray0b6f6c82012-05-25 18:42:14 -0400394 .. method:: header_source_parse(sourcelines)
395
396 The implementation of this method is the same as that for the
397 :class:`Compat32` policy.
398
399 .. method:: header_store_parse(name, value)
400
401 The name is returned unchanged. If the input value has a ``name``
402 attribute and it matches *name* ignoring case, the value is returned
403 unchanged. Otherwise the *name* and *value* are passed to
404 ``header_factory``, and the resulting custom header object is returned as
405 the value. In this case a ``ValueError`` is raised if the input value
406 contains CR or LF characters.
407
408 .. method:: header_fetch_parse(name, value)
409
410 If the value has a ``name`` attribute, it is returned to unmodified.
411 Otherwise the *name*, and the *value* with any CR or LF characters
412 removed, are passed to the ``header_factory``, and the resulting custom
413 header object is returned. Any surrogateescaped bytes get turned into
414 the unicode unknown-character glyph.
415
416 .. method:: fold(name, value)
417
418 Header folding is controlled by the :attr:`refold_source` policy setting.
419 A value is considered to be a 'source value' if and only if it does not
420 have a ``name`` attribute (having a ``name`` attribute means it is a
421 header object of some sort). If a source value needs to be refolded
422 according to the policy, it is converted into a custom header object by
423 passing the *name* and the *value* with any CR and LF characters removed
424 to the ``header_factory``. Folding of a custom header object is done by
425 calling its ``fold`` method with the current policy.
426
427 Source values are split into lines using :meth:`~str.splitlines`. If
428 the value is not to be refolded, the lines are rejoined using the
429 ``linesep`` from the policy and returned. The exception is lines
430 containing non-ascii binary data. In that case the value is refolded
431 regardless of the ``refold_source`` setting, which causes the binary data
432 to be CTE encoded using the ``unknown-8bit`` charset.
433
434 .. method:: fold_binary(name, value)
435
436 The same as :meth:`fold` if :attr:`cte_type` is ``7bit``, except that
437 the returned value is bytes.
438
439 If :attr:`cte_type` is ``8bit``, non-ASCII binary data is converted back
440 into bytes. Headers with binary data are not refolded, regardless of the
441 ``refold_header`` setting, since there is no way to know whether the
442 binary data consists of single byte characters or multibyte characters.
443
444The following instances of :class:`EmailPolicy` provide defaults suitable for
445specific application domains. Note that in the future the behavior of these
Georg Brandl38e0e1e2012-05-27 09:31:10 +0200446instances (in particular the ``HTTP`` instance) may be adjusted to conform even
R David Murray0b6f6c82012-05-25 18:42:14 -0400447more closely to the RFCs relevant to their domains.
448
449.. data:: default
450
451 An instance of ``EmailPolicy`` with all defaults unchanged. This policy
452 uses the standard Python ``\n`` line endings rather than the RFC-correct
453 ``\r\n``.
454
455.. data:: SMTP
456
457 Suitable for serializing messages in conformance with the email RFCs.
458 Like ``default``, but with ``linesep`` set to ``\r\n``, which is RFC
459 compliant.
460
461.. data:: HTTP
462
463 Suitable for serializing headers with for use in HTTP traffic. Like
464 ``SMTP`` except that ``max_line_length`` is set to ``None`` (unlimited).
465
466.. data:: strict
467
468 Convenience instance. The same as ``default`` except that
469 ``raise_on_defect`` is set to ``True``. This allows any policy to be made
470 strict by writing::
471
472 somepolicy + policy.strict
473
474With all of these :class:`EmailPolicies <.EmailPolicy>`, the effective API of
475the email package is changed from the Python 3.2 API in the following ways:
476
477 * Setting a header on a :class:`~email.message.Message` results in that
478 header being parsed and a custom header object created.
479
480 * Fetching a header value from a :class:`~email.message.Message` results
481 in that header being parsed and a custom header object created and
482 returned.
483
484 * Any custom header object, or any header that is refolded due to the
485 policy settings, is folded using an algorithm that fully implements the
486 RFC folding algorithms, including knowing where encoded words are required
487 and allowed.
488
489From the application view, this means that any header obtained through the
490:class:`~email.message.Message` is a custom header object with custom
491attributes, whose string value is the fully decoded unicode value of the
492header. Likewise, a header may be assigned a new value, or a new header
493created, using a unicode string, and the policy will take care of converting
494the unicode string into the correct RFC encoded form.
495
R David Murrayea976682012-05-27 15:03:38 -0400496The custom header objects and their attributes are described in
497:mod:`~email.headerregistry`.