blob: 2a6047d3a41d48f6da51c793029b0a1e908e99f6 [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040012**Source code:** :source:`Lib/email/policy.py`
13
14--------------
R David Murray3edd22a2011-04-18 13:59:37 -040015
16The :mod:`email` package's prime focus is the handling of email messages as
17described by the various email and MIME RFCs. However, the general format of
18email messages (a block of header fields each consisting of a name followed by
19a colon followed by a value, the whole block followed by a blank line and an
20arbitrary 'body'), is a format that has found utility outside of the realm of
21email. Some of these uses conform fairly closely to the main RFCs, some do
22not. And even when working with email, there are times when it is desirable to
23break strict compliance with the RFCs.
24
R David Murray6a45d3b2011-04-18 16:00:47 -040025Policy objects give the email package the flexibility to handle all these
26disparate use cases.
R David Murray3edd22a2011-04-18 13:59:37 -040027
28A :class:`Policy` object encapsulates a set of attributes and methods that
29control the behavior of various components of the email package during use.
30:class:`Policy` instances can be passed to various classes and methods in the
31email package to alter the default behavior. The settable values and their
R David Murrayc27e5222012-05-25 15:01:48 -040032defaults are described below.
R David Murray3edd22a2011-04-18 13:59:37 -040033
R David Murrayc27e5222012-05-25 15:01:48 -040034There is a default policy used by all classes in the email package. This
35policy is named :class:`Compat32`, with a corresponding pre-defined instance
36named :const:`compat32`. It provides for complete backward compatibility (in
37some cases, including bug compatibility) with the pre-Python3.3 version of the
38email package.
39
40The first part of this documentation covers the features of :class:`Policy`, an
41:term:`abstract base class` that defines the features that are common to all
42policy objects, including :const:`compat32`. This includes certain hook
43methods that are called internally by the email package, which a custom policy
44could override to obtain different behavior.
45
46When a :class:`~email.message.Message` object is created, it acquires a policy.
47By default this will be :const:`compat32`, but a different policy can be
48specified. If the ``Message`` is created by a :mod:`~email.parser`, a policy
49passed to the parser will be the policy used by the ``Message`` it creates. If
50the ``Message`` is created by the program, then the policy can be specified
51when it is created. When a ``Message`` is passed to a :mod:`~email.generator`,
52the generator uses the policy from the ``Message`` by default, but you can also
53pass a specific policy to the generator that will override the one stored on
54the ``Message`` object.
55
56:class:`Policy` instances are immutable, but they can be cloned, accepting the
57same keyword arguments as the class constructor and returning a new
58:class:`Policy` instance that is a copy of the original but with the specified
59attributes values changed.
R David Murray3edd22a2011-04-18 13:59:37 -040060
61As an example, the following code could be used to read an email message from a
R David Murrayfdfb0052013-07-29 15:49:58 -040062file on disk and pass it to the system ``sendmail`` program on a Unix system:
R David Murray3edd22a2011-04-18 13:59:37 -040063
R David Murrayfdfb0052013-07-29 15:49:58 -040064.. testsetup::
65
Zachary Ware640b1ca2016-08-10 00:39:41 -050066 from unittest import mock
67 mocker = mock.patch('subprocess.Popen')
68 m = mocker.start()
69 proc = mock.MagicMock()
70 m.return_value = proc
71 proc.stdin.close.return_value = None
72 mymsg = open('mymsg.txt', 'w')
73 mymsg.write('To: abc@xyz.com\n\n')
74 mymsg.flush()
R David Murrayfdfb0052013-07-29 15:49:58 -040075
76.. doctest::
77
78 >>> from email import message_from_binary_file
R David Murray3edd22a2011-04-18 13:59:37 -040079 >>> from email.generator import BytesGenerator
R David Murrayfdfb0052013-07-29 15:49:58 -040080 >>> from email import policy
R David Murray3edd22a2011-04-18 13:59:37 -040081 >>> from subprocess import Popen, PIPE
R David Murrayfdfb0052013-07-29 15:49:58 -040082 >>> with open('mymsg.txt', 'rb') as f:
83 ... msg = message_from_binary_file(f, policy=policy.default)
84 >>> p = Popen(['sendmail', msg['To'].addresses[0]], stdin=PIPE)
R David Murrayc27e5222012-05-25 15:01:48 -040085 >>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n'))
R David Murray3edd22a2011-04-18 13:59:37 -040086 >>> g.flatten(msg)
87 >>> p.stdin.close()
88 >>> rc = p.wait()
89
Zachary Ware640b1ca2016-08-10 00:39:41 -050090.. testcleanup::
R David Murrayfdfb0052013-07-29 15:49:58 -040091
Zachary Ware640b1ca2016-08-10 00:39:41 -050092 mymsg.close()
93 mocker.stop()
94 import os
95 os.remove('mymsg.txt')
R David Murrayfdfb0052013-07-29 15:49:58 -040096
R David Murrayc27e5222012-05-25 15:01:48 -040097Here we are telling :class:`~email.generator.BytesGenerator` to use the RFC
98correct line separator characters when creating the binary string to feed into
99``sendmail's`` ``stdin``, where the default policy would use ``\n`` line
100separators.
Éric Araujofe0472e2011-12-03 16:00:56 +0100101
R David Murray3edd22a2011-04-18 13:59:37 -0400102Some email package methods accept a *policy* keyword argument, allowing the
R David Murray6a45d3b2011-04-18 16:00:47 -0400103policy to be overridden for that method. For example, the following code uses
Barry Warsaw904c4812014-12-19 11:20:00 -0500104the :meth:`~email.message.Message.as_bytes` method of the *msg* object from
R David Murrayc27e5222012-05-25 15:01:48 -0400105the previous example and writes the message to a file using the native line
106separators for the platform on which it is running::
R David Murray3edd22a2011-04-18 13:59:37 -0400107
108 >>> import os
R David Murray3edd22a2011-04-18 13:59:37 -0400109 >>> with open('converted.txt', 'wb') as f:
R David Murraybb17d2b2013-08-09 16:15:28 -0400110 ... f.write(msg.as_bytes(policy=msg.policy.clone(linesep=os.linesep)))
111 17
R David Murray3edd22a2011-04-18 13:59:37 -0400112
113Policy objects can also be combined using the addition operator, producing a
114policy object whose settings are a combination of the non-default values of the
115summed objects::
116
R David Murrayfdfb0052013-07-29 15:49:58 -0400117 >>> compat_SMTP = policy.compat32.clone(linesep='\r\n')
118 >>> compat_strict = policy.compat32.clone(raise_on_defect=True)
R David Murrayc27e5222012-05-25 15:01:48 -0400119 >>> compat_strict_SMTP = compat_SMTP + compat_strict
R David Murray3edd22a2011-04-18 13:59:37 -0400120
121This operation is not commutative; that is, the order in which the objects are
122added matters. To illustrate::
123
R David Murrayfdfb0052013-07-29 15:49:58 -0400124 >>> policy100 = policy.compat32.clone(max_line_length=100)
125 >>> policy80 = policy.compat32.clone(max_line_length=80)
126 >>> apolicy = policy100 + policy80
R David Murray3edd22a2011-04-18 13:59:37 -0400127 >>> apolicy.max_line_length
128 80
R David Murrayc27e5222012-05-25 15:01:48 -0400129 >>> apolicy = policy80 + policy100
R David Murray3edd22a2011-04-18 13:59:37 -0400130 >>> apolicy.max_line_length
131 100
132
133
134.. class:: Policy(**kw)
135
R David Murrayc27e5222012-05-25 15:01:48 -0400136 This is the :term:`abstract base class` for all policy classes. It provides
137 default implementations for a couple of trivial methods, as well as the
138 implementation of the immutability property, the :meth:`clone` method, and
139 the constructor semantics.
140
141 The constructor of a policy class can be passed various keyword arguments.
142 The arguments that may be specified are any non-method properties on this
143 class, plus any additional non-method properties on the concrete class. A
144 value specified in the constructor will override the default value for the
145 corresponding attribute.
146
147 This class defines the following properties, and thus values for the
148 following may be passed in the constructor of any policy class:
R David Murray3edd22a2011-04-18 13:59:37 -0400149
150 .. attribute:: max_line_length
151
152 The maximum length of any line in the serialized output, not counting the
153 end of line character(s). Default is 78, per :rfc:`5322`. A value of
154 ``0`` or :const:`None` indicates that no line wrapping should be
155 done at all.
156
157 .. attribute:: linesep
158
159 The string to be used to terminate lines in serialized output. The
R David Murray6a45d3b2011-04-18 16:00:47 -0400160 default is ``\n`` because that's the internal end-of-line discipline used
R David Murrayc27e5222012-05-25 15:01:48 -0400161 by Python, though ``\r\n`` is required by the RFCs.
R David Murray3edd22a2011-04-18 13:59:37 -0400162
R David Murrayc27e5222012-05-25 15:01:48 -0400163 .. attribute:: cte_type
R David Murray3edd22a2011-04-18 13:59:37 -0400164
R David Murrayc27e5222012-05-25 15:01:48 -0400165 Controls the type of Content Transfer Encodings that may be or are
166 required to be used. The possible values are:
167
Georg Brandl44ea77b2013-03-28 13:28:44 +0100168 .. tabularcolumns:: |l|L|
169
R David Murrayc27e5222012-05-25 15:01:48 -0400170 ======== ===============================================================
171 ``7bit`` all data must be "7 bit clean" (ASCII-only). This means that
172 where necessary data will be encoded using either
173 quoted-printable or base64 encoding.
174
175 ``8bit`` data is not constrained to be 7 bit clean. Data in headers is
176 still required to be ASCII-only and so will be encoded (see
177 'binary_fold' below for an exception), but body parts may use
178 the ``8bit`` CTE.
179 ======== ===============================================================
180
181 A ``cte_type`` value of ``8bit`` only works with ``BytesGenerator``, not
182 ``Generator``, because strings cannot contain binary data. If a
183 ``Generator`` is operating under a policy that specifies
184 ``cte_type=8bit``, it will act as if ``cte_type`` is ``7bit``.
R David Murray3edd22a2011-04-18 13:59:37 -0400185
186 .. attribute:: raise_on_defect
187
188 If :const:`True`, any defects encountered will be raised as errors. If
189 :const:`False` (the default), defects will be passed to the
190 :meth:`register_defect` method.
191
R David Murrayfdb23c22015-05-17 14:24:33 -0400192
193
194 .. attribute:: mangle_from\_
195
196 If :const:`True`, lines starting with *"From "* in the body are
197 escaped by putting a ``>`` in front of them. This parameter is used when
198 the message is being serialized by a generator.
199 Default: :const:`False`.
200
201 .. versionadded:: 3.5
202 The *mangle_from_* parameter.
203
R David Murrayc27e5222012-05-25 15:01:48 -0400204 The following :class:`Policy` method is intended to be called by code using
205 the email library to create policy instances with custom settings:
R David Murray6a45d3b2011-04-18 16:00:47 -0400206
R David Murrayc27e5222012-05-25 15:01:48 -0400207 .. method:: clone(**kw)
R David Murray3edd22a2011-04-18 13:59:37 -0400208
209 Return a new :class:`Policy` instance whose attributes have the same
210 values as the current instance, except where those attributes are
211 given new values by the keyword arguments.
212
R David Murrayc27e5222012-05-25 15:01:48 -0400213 The remaining :class:`Policy` methods are called by the email package code,
214 and are not intended to be called by an application using the email package.
215 A custom policy must implement all of these methods.
R David Murray3edd22a2011-04-18 13:59:37 -0400216
R David Murrayc27e5222012-05-25 15:01:48 -0400217 .. method:: handle_defect(obj, defect)
R David Murray3edd22a2011-04-18 13:59:37 -0400218
R David Murrayc27e5222012-05-25 15:01:48 -0400219 Handle a *defect* found on *obj*. When the email package calls this
220 method, *defect* will always be a subclass of
221 :class:`~email.errors.Defect`.
R David Murray3edd22a2011-04-18 13:59:37 -0400222
R David Murrayc27e5222012-05-25 15:01:48 -0400223 The default implementation checks the :attr:`raise_on_defect` flag. If
224 it is ``True``, *defect* is raised as an exception. If it is ``False``
225 (the default), *obj* and *defect* are passed to :meth:`register_defect`.
R David Murray3edd22a2011-04-18 13:59:37 -0400226
R David Murrayc27e5222012-05-25 15:01:48 -0400227 .. method:: register_defect(obj, defect)
R David Murray3edd22a2011-04-18 13:59:37 -0400228
R David Murrayc27e5222012-05-25 15:01:48 -0400229 Register a *defect* on *obj*. In the email package, *defect* will always
230 be a subclass of :class:`~email.errors.Defect`.
R David Murray3edd22a2011-04-18 13:59:37 -0400231
R David Murrayc27e5222012-05-25 15:01:48 -0400232 The default implementation calls the ``append`` method of the ``defects``
233 attribute of *obj*. When the email package calls :attr:`handle_defect`,
234 *obj* will normally have a ``defects`` attribute that has an ``append``
235 method. Custom object types used with the email package (for example,
236 custom ``Message`` objects) should also provide such an attribute,
237 otherwise defects in parsed messages will raise unexpected errors.
R David Murray3edd22a2011-04-18 13:59:37 -0400238
R David Murrayabfc3742012-05-29 09:14:44 -0400239 .. method:: header_max_count(name)
240
241 Return the maximum allowed number of headers named *name*.
242
243 Called when a header is added to a :class:`~email.message.Message`
244 object. If the returned value is not ``0`` or ``None``, and there are
245 already a number of headers with the name *name* equal to the value
246 returned, a :exc:`ValueError` is raised.
247
248 Because the default behavior of ``Message.__setitem__`` is to append the
249 value to the list of headers, it is easy to create duplicate headers
250 without realizing it. This method allows certain headers to be limited
251 in the number of instances of that header that may be added to a
252 ``Message`` programmatically. (The limit is not observed by the parser,
253 which will faithfully produce as many headers as exist in the message
254 being parsed.)
255
256 The default implementation returns ``None`` for all header names.
257
R David Murrayc27e5222012-05-25 15:01:48 -0400258 .. method:: header_source_parse(sourcelines)
R David Murray3edd22a2011-04-18 13:59:37 -0400259
R David Murrayc27e5222012-05-25 15:01:48 -0400260 The email package calls this method with a list of strings, each string
261 ending with the line separation characters found in the source being
262 parsed. The first line includes the field header name and separator.
263 All whitespace in the source is preserved. The method should return the
264 ``(name, value)`` tuple that is to be stored in the ``Message`` to
265 represent the parsed header.
R David Murray3edd22a2011-04-18 13:59:37 -0400266
R David Murrayc27e5222012-05-25 15:01:48 -0400267 If an implementation wishes to retain compatibility with the existing
268 email package policies, *name* should be the case preserved name (all
269 characters up to the '``:``' separator), while *value* should be the
270 unfolded value (all line separator characters removed, but whitespace
271 kept intact), stripped of leading whitespace.
R David Murray3edd22a2011-04-18 13:59:37 -0400272
R David Murrayc27e5222012-05-25 15:01:48 -0400273 *sourcelines* may contain surrogateescaped binary data.
274
275 There is no default implementation
276
277 .. method:: header_store_parse(name, value)
278
279 The email package calls this method with the name and value provided by
280 the application program when the application program is modifying a
281 ``Message`` programmatically (as opposed to a ``Message`` created by a
282 parser). The method should return the ``(name, value)`` tuple that is to
283 be stored in the ``Message`` to represent the header.
284
285 If an implementation wishes to retain compatibility with the existing
286 email package policies, the *name* and *value* should be strings or
287 string subclasses that do not change the content of the passed in
288 arguments.
289
290 There is no default implementation
291
292 .. method:: header_fetch_parse(name, value)
293
294 The email package calls this method with the *name* and *value* currently
295 stored in the ``Message`` when that header is requested by the
296 application program, and whatever the method returns is what is passed
297 back to the application as the value of the header being retrieved.
298 Note that there may be more than one header with the same name stored in
299 the ``Message``; the method is passed the specific name and value of the
300 header destined to be returned to the application.
301
302 *value* may contain surrogateescaped binary data. There should be no
303 surrogateescaped binary data in the value returned by the method.
304
305 There is no default implementation
306
307 .. method:: fold(name, value)
308
309 The email package calls this method with the *name* and *value* currently
310 stored in the ``Message`` for a given header. The method should return a
311 string that represents that header "folded" correctly (according to the
312 policy settings) by composing the *name* with the *value* and inserting
313 :attr:`linesep` characters at the appropriate places. See :rfc:`5322`
314 for a discussion of the rules for folding email headers.
315
316 *value* may contain surrogateescaped binary data. There should be no
317 surrogateescaped binary data in the string returned by the method.
318
319 .. method:: fold_binary(name, value)
320
321 The same as :meth:`fold`, except that the returned value should be a
322 bytes object rather than a string.
323
324 *value* may contain surrogateescaped binary data. These could be
325 converted back into binary data in the returned bytes object.
326
327
328.. class:: Compat32(**kw)
329
330 This concrete :class:`Policy` is the backward compatibility policy. It
331 replicates the behavior of the email package in Python 3.2. The
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300332 :mod:`~email.policy` module also defines an instance of this class,
R David Murrayc27e5222012-05-25 15:01:48 -0400333 :const:`compat32`, that is used as the default policy. Thus the default
334 behavior of the email package is to maintain compatibility with Python 3.2.
335
R David Murrayfdb23c22015-05-17 14:24:33 -0400336 The following attributes have values that are different from the
337 :class:`Policy` default:
338
339 .. attribute:: mangle_from_
340
341 The default is ``True``.
342
R David Murrayc27e5222012-05-25 15:01:48 -0400343 The class provides the following concrete implementations of the
344 abstract methods of :class:`Policy`:
345
346 .. method:: header_source_parse(sourcelines)
347
348 The name is parsed as everything up to the '``:``' and returned
349 unmodified. The value is determined by stripping leading whitespace off
350 the remainder of the first line, joining all subsequent lines together,
351 and stripping any trailing carriage return or linefeed characters.
352
353 .. method:: header_store_parse(name, value)
354
355 The name and value are returned unmodified.
356
357 .. method:: header_fetch_parse(name, value)
358
359 If the value contains binary data, it is converted into a
360 :class:`~email.header.Header` object using the ``unknown-8bit`` charset.
361 Otherwise it is returned unmodified.
362
363 .. method:: fold(name, value)
364
365 Headers are folded using the :class:`~email.header.Header` folding
366 algorithm, which preserves existing line breaks in the value, and wraps
367 each resulting line to the ``max_line_length``. Non-ASCII binary data are
368 CTE encoded using the ``unknown-8bit`` charset.
369
370 .. method:: fold_binary(name, value)
371
372 Headers are folded using the :class:`~email.header.Header` folding
373 algorithm, which preserves existing line breaks in the value, and wraps
374 each resulting line to the ``max_line_length``. If ``cte_type`` is
375 ``7bit``, non-ascii binary data is CTE encoded using the ``unknown-8bit``
376 charset. Otherwise the original source header is used, with its existing
Terry Jan Reedy0f847642013-03-11 18:34:00 -0400377 line breaks and any (RFC invalid) binary data it may contain.
R David Murray0b6f6c82012-05-25 18:42:14 -0400378
379
R David Murrayfdb23c22015-05-17 14:24:33 -0400380An instance of :class:`Compat32` is provided as a module constant:
381
382.. data:: compat32
383
384 An instance of :class:`Compat32`, providing backward compatibility with the
385 behavior of the email package in Python 3.2.
386
387
R David Murray0b6f6c82012-05-25 18:42:14 -0400388.. note::
389
R David Murrayea976682012-05-27 15:03:38 -0400390 The documentation below describes new policies that are included in the
391 standard library on a :term:`provisional basis <provisional package>`.
392 Backwards incompatible changes (up to and including removal of the feature)
393 may occur if deemed necessary by the core developers.
R David Murray0b6f6c82012-05-25 18:42:14 -0400394
395
396.. class:: EmailPolicy(**kw)
397
398 This concrete :class:`Policy` provides behavior that is intended to be fully
399 compliant with the current email RFCs. These include (but are not limited
400 to) :rfc:`5322`, :rfc:`2047`, and the current MIME RFCs.
401
402 This policy adds new header parsing and folding algorithms. Instead of
R David Murray3da240f2013-10-16 22:48:40 -0400403 simple strings, headers are ``str`` subclasses with attributes that depend
R David Murray0b6f6c82012-05-25 18:42:14 -0400404 on the type of the field. The parsing and folding algorithm fully implement
405 :rfc:`2047` and :rfc:`5322`.
406
407 In addition to the settable attributes listed above that apply to all
408 policies, this policy adds the following additional attributes:
409
R David Murray224ef3e2015-05-17 11:29:21 -0400410 .. attribute:: utf8
411
412 If ``False``, follow :rfc:`5322`, supporting non-ASCII characters in
413 headers by encoding them as "encoded words". If ``True``, follow
414 :rfc:`6532` and use ``utf-8`` encoding for headers. Messages
415 formatted in this way may be passed to SMTP servers that support
416 the ``SMTPUTF8`` extension (:rfc:`6531`).
417
R David Murray0b6f6c82012-05-25 18:42:14 -0400418 .. attribute:: refold_source
419
420 If the value for a header in the ``Message`` object originated from a
421 :mod:`~email.parser` (as opposed to being set by a program), this
422 attribute indicates whether or not a generator should refold that value
423 when transforming the message back into stream form. The possible values
424 are:
425
426 ======== ===============================================================
427 ``none`` all source values use original folding
428
429 ``long`` source values that have any line that is longer than
430 ``max_line_length`` will be refolded
431
432 ``all`` all values are refolded.
433 ======== ===============================================================
434
435 The default is ``long``.
436
437 .. attribute:: header_factory
438
439 A callable that takes two arguments, ``name`` and ``value``, where
440 ``name`` is a header field name and ``value`` is an unfolded header field
R David Murrayea976682012-05-27 15:03:38 -0400441 value, and returns a string subclass that represents that header. A
442 default ``header_factory`` (see :mod:`~email.headerregistry`) is provided
443 that understands some of the :RFC:`5322` header field types. (Currently
444 address fields and date fields have special treatment, while all other
445 fields are treated as unstructured. This list will be completed before
446 the extension is marked stable.)
R David Murray0b6f6c82012-05-25 18:42:14 -0400447
R David Murray3da240f2013-10-16 22:48:40 -0400448 .. attribute:: content_manager
449
450 An object with at least two methods: get_content and set_content. When
451 the :meth:`~email.message.Message.get_content` or
452 :meth:`~email.message.Message.set_content` method of a
453 :class:`~email.message.Message` object is called, it calls the
454 corresponding method of this object, passing it the message object as its
455 first argument, and any arguments or keywords that were passed to it as
456 additional arguments. By default ``content_manager`` is set to
457 :data:`~email.contentmanager.raw_data_manager`.
458
Larry Hastings3732ed22014-03-15 21:13:56 -0700459 .. versionadded:: 3.4
R David Murray3da240f2013-10-16 22:48:40 -0400460
461
R David Murray0b6f6c82012-05-25 18:42:14 -0400462 The class provides the following concrete implementations of the abstract
463 methods of :class:`Policy`:
464
R David Murrayabfc3742012-05-29 09:14:44 -0400465 .. method:: header_max_count(name)
466
467 Returns the value of the
468 :attr:`~email.headerregistry.BaseHeader.max_count` attribute of the
469 specialized class used to represent the header with the given name.
470
R David Murray0b6f6c82012-05-25 18:42:14 -0400471 .. method:: header_source_parse(sourcelines)
472
473 The implementation of this method is the same as that for the
474 :class:`Compat32` policy.
475
476 .. method:: header_store_parse(name, value)
477
478 The name is returned unchanged. If the input value has a ``name``
479 attribute and it matches *name* ignoring case, the value is returned
480 unchanged. Otherwise the *name* and *value* are passed to
R David Murray3da240f2013-10-16 22:48:40 -0400481 ``header_factory``, and the resulting header object is returned as
R David Murray0b6f6c82012-05-25 18:42:14 -0400482 the value. In this case a ``ValueError`` is raised if the input value
483 contains CR or LF characters.
484
485 .. method:: header_fetch_parse(name, value)
486
487 If the value has a ``name`` attribute, it is returned to unmodified.
488 Otherwise the *name*, and the *value* with any CR or LF characters
R David Murray3da240f2013-10-16 22:48:40 -0400489 removed, are passed to the ``header_factory``, and the resulting
R David Murray0b6f6c82012-05-25 18:42:14 -0400490 header object is returned. Any surrogateescaped bytes get turned into
491 the unicode unknown-character glyph.
492
493 .. method:: fold(name, value)
494
495 Header folding is controlled by the :attr:`refold_source` policy setting.
496 A value is considered to be a 'source value' if and only if it does not
497 have a ``name`` attribute (having a ``name`` attribute means it is a
498 header object of some sort). If a source value needs to be refolded
R David Murray3da240f2013-10-16 22:48:40 -0400499 according to the policy, it is converted into a header object by
R David Murray0b6f6c82012-05-25 18:42:14 -0400500 passing the *name* and the *value* with any CR and LF characters removed
R David Murray3da240f2013-10-16 22:48:40 -0400501 to the ``header_factory``. Folding of a header object is done by
R David Murray0b6f6c82012-05-25 18:42:14 -0400502 calling its ``fold`` method with the current policy.
503
504 Source values are split into lines using :meth:`~str.splitlines`. If
505 the value is not to be refolded, the lines are rejoined using the
506 ``linesep`` from the policy and returned. The exception is lines
507 containing non-ascii binary data. In that case the value is refolded
508 regardless of the ``refold_source`` setting, which causes the binary data
509 to be CTE encoded using the ``unknown-8bit`` charset.
510
511 .. method:: fold_binary(name, value)
512
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300513 The same as :meth:`fold` if :attr:`~Policy.cte_type` is ``7bit``, except
514 that the returned value is bytes.
R David Murray0b6f6c82012-05-25 18:42:14 -0400515
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300516 If :attr:`~Policy.cte_type` is ``8bit``, non-ASCII binary data is
517 converted back
R David Murray0b6f6c82012-05-25 18:42:14 -0400518 into bytes. Headers with binary data are not refolded, regardless of the
519 ``refold_header`` setting, since there is no way to know whether the
520 binary data consists of single byte characters or multibyte characters.
521
522The following instances of :class:`EmailPolicy` provide defaults suitable for
523specific application domains. Note that in the future the behavior of these
Georg Brandl38e0e1e2012-05-27 09:31:10 +0200524instances (in particular the ``HTTP`` instance) may be adjusted to conform even
R David Murray0b6f6c82012-05-25 18:42:14 -0400525more closely to the RFCs relevant to their domains.
526
527.. data:: default
528
529 An instance of ``EmailPolicy`` with all defaults unchanged. This policy
530 uses the standard Python ``\n`` line endings rather than the RFC-correct
531 ``\r\n``.
532
533.. data:: SMTP
534
535 Suitable for serializing messages in conformance with the email RFCs.
536 Like ``default``, but with ``linesep`` set to ``\r\n``, which is RFC
537 compliant.
538
R David Murray1dbee942015-05-17 19:36:16 -0400539.. data:: SMTPUTF8
540
541 The same as ``SMTP`` except that :attr:`~EmailPolicy.utf8` is ``True``.
542 Useful for serializing messages to a message store without using encoded
543 words in the headers. Should only be used for SMTP trasmission if the
544 sender or recipient addresses have non-ASCII characters (the
545 :meth:`smtplib.SMTP.send_message` method handles this automatically).
546
R David Murray0b6f6c82012-05-25 18:42:14 -0400547.. data:: HTTP
548
549 Suitable for serializing headers with for use in HTTP traffic. Like
550 ``SMTP`` except that ``max_line_length`` is set to ``None`` (unlimited).
551
552.. data:: strict
553
554 Convenience instance. The same as ``default`` except that
555 ``raise_on_defect`` is set to ``True``. This allows any policy to be made
556 strict by writing::
557
558 somepolicy + policy.strict
559
560With all of these :class:`EmailPolicies <.EmailPolicy>`, the effective API of
561the email package is changed from the Python 3.2 API in the following ways:
562
563 * Setting a header on a :class:`~email.message.Message` results in that
R David Murray3da240f2013-10-16 22:48:40 -0400564 header being parsed and a header object created.
R David Murray0b6f6c82012-05-25 18:42:14 -0400565
566 * Fetching a header value from a :class:`~email.message.Message` results
R David Murray3da240f2013-10-16 22:48:40 -0400567 in that header being parsed and a header object created and
R David Murray0b6f6c82012-05-25 18:42:14 -0400568 returned.
569
R David Murray3da240f2013-10-16 22:48:40 -0400570 * Any header object, or any header that is refolded due to the
R David Murray0b6f6c82012-05-25 18:42:14 -0400571 policy settings, is folded using an algorithm that fully implements the
572 RFC folding algorithms, including knowing where encoded words are required
573 and allowed.
574
575From the application view, this means that any header obtained through the
R David Murray3da240f2013-10-16 22:48:40 -0400576:class:`~email.message.Message` is a header object with extra
R David Murray0b6f6c82012-05-25 18:42:14 -0400577attributes, whose string value is the fully decoded unicode value of the
578header. Likewise, a header may be assigned a new value, or a new header
579created, using a unicode string, and the policy will take care of converting
580the unicode string into the correct RFC encoded form.
581
R David Murray3da240f2013-10-16 22:48:40 -0400582The header objects and their attributes are described in
R David Murrayea976682012-05-27 15:03:38 -0400583:mod:`~email.headerregistry`.