blob: 54ebb1cf11189ab1fdd003e285360e8e87f961fc [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 Murrayfdfb0052013-07-29 15:49:58 -040059file on disk and pass it to the system ``sendmail`` program on a Unix system:
R David Murray3edd22a2011-04-18 13:59:37 -040060
R David Murrayfdfb0052013-07-29 15:49:58 -040061.. testsetup::
62
63 >>> from unittest import mock
64 >>> mocker = mock.patch('subprocess.Popen')
65 >>> m = mocker.start()
66 >>> proc = mock.MagicMock()
67 >>> m.return_value = proc
68 >>> proc.stdin.close.return_value = None
69 >>> mymsg = open('mymsg.txt', 'w')
70 >>> mymsg.write('To: abc@xyz.com\n\n')
71 17
72 >>> mymsg.flush()
73
74.. doctest::
75
76 >>> from email import message_from_binary_file
R David Murray3edd22a2011-04-18 13:59:37 -040077 >>> from email.generator import BytesGenerator
R David Murrayfdfb0052013-07-29 15:49:58 -040078 >>> from email import policy
R David Murray3edd22a2011-04-18 13:59:37 -040079 >>> from subprocess import Popen, PIPE
R David Murrayfdfb0052013-07-29 15:49:58 -040080 >>> with open('mymsg.txt', 'rb') as f:
81 ... msg = message_from_binary_file(f, policy=policy.default)
82 >>> p = Popen(['sendmail', msg['To'].addresses[0]], stdin=PIPE)
R David Murrayc27e5222012-05-25 15:01:48 -040083 >>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n'))
R David Murray3edd22a2011-04-18 13:59:37 -040084 >>> g.flatten(msg)
85 >>> p.stdin.close()
86 >>> rc = p.wait()
87
R David Murray11bfd322013-07-30 14:42:40 -040088.. testsetup::
R David Murrayfdfb0052013-07-29 15:49:58 -040089
90 >>> mymsg.close()
91 >>> mocker.stop()
92 >>> import os
93 >>> os.remove('mymsg.txt')
94
R David Murrayc27e5222012-05-25 15:01:48 -040095Here we are telling :class:`~email.generator.BytesGenerator` to use the RFC
96correct line separator characters when creating the binary string to feed into
97``sendmail's`` ``stdin``, where the default policy would use ``\n`` line
98separators.
Éric Araujofe0472e2011-12-03 16:00:56 +010099
R David Murray3edd22a2011-04-18 13:59:37 -0400100Some email package methods accept a *policy* keyword argument, allowing the
R David Murray6a45d3b2011-04-18 16:00:47 -0400101policy to be overridden for that method. For example, the following code uses
R David Murrayc27e5222012-05-25 15:01:48 -0400102the :meth:`~email.message.Message.as_string` method of the *msg* object from
103the previous example and writes the message to a file using the native line
104separators for the platform on which it is running::
R David Murray3edd22a2011-04-18 13:59:37 -0400105
106 >>> import os
R David Murray3edd22a2011-04-18 13:59:37 -0400107 >>> with open('converted.txt', 'wb') as f:
R David Murrayfdfb0052013-07-29 15:49:58 -0400108 ... f.write(msg.as_string(policy=msg.policy.clone(linesep=os.linesep)))
R David Murray3edd22a2011-04-18 13:59:37 -0400109
110Policy objects can also be combined using the addition operator, producing a
111policy object whose settings are a combination of the non-default values of the
112summed objects::
113
R David Murrayfdfb0052013-07-29 15:49:58 -0400114 >>> compat_SMTP = policy.compat32.clone(linesep='\r\n')
115 >>> compat_strict = policy.compat32.clone(raise_on_defect=True)
R David Murrayc27e5222012-05-25 15:01:48 -0400116 >>> compat_strict_SMTP = compat_SMTP + compat_strict
R David Murray3edd22a2011-04-18 13:59:37 -0400117
118This operation is not commutative; that is, the order in which the objects are
119added matters. To illustrate::
120
R David Murrayfdfb0052013-07-29 15:49:58 -0400121 >>> policy100 = policy.compat32.clone(max_line_length=100)
122 >>> policy80 = policy.compat32.clone(max_line_length=80)
123 >>> apolicy = policy100 + policy80
R David Murray3edd22a2011-04-18 13:59:37 -0400124 >>> apolicy.max_line_length
125 80
R David Murrayc27e5222012-05-25 15:01:48 -0400126 >>> apolicy = policy80 + policy100
R David Murray3edd22a2011-04-18 13:59:37 -0400127 >>> apolicy.max_line_length
128 100
129
130
131.. class:: Policy(**kw)
132
R David Murrayc27e5222012-05-25 15:01:48 -0400133 This is the :term:`abstract base class` for all policy classes. It provides
134 default implementations for a couple of trivial methods, as well as the
135 implementation of the immutability property, the :meth:`clone` method, and
136 the constructor semantics.
137
138 The constructor of a policy class can be passed various keyword arguments.
139 The arguments that may be specified are any non-method properties on this
140 class, plus any additional non-method properties on the concrete class. A
141 value specified in the constructor will override the default value for the
142 corresponding attribute.
143
144 This class defines the following properties, and thus values for the
145 following may be passed in the constructor of any policy class:
R David Murray3edd22a2011-04-18 13:59:37 -0400146
147 .. attribute:: max_line_length
148
149 The maximum length of any line in the serialized output, not counting the
150 end of line character(s). Default is 78, per :rfc:`5322`. A value of
151 ``0`` or :const:`None` indicates that no line wrapping should be
152 done at all.
153
154 .. attribute:: linesep
155
156 The string to be used to terminate lines in serialized output. The
R David Murray6a45d3b2011-04-18 16:00:47 -0400157 default is ``\n`` because that's the internal end-of-line discipline used
R David Murrayc27e5222012-05-25 15:01:48 -0400158 by Python, though ``\r\n`` is required by the RFCs.
R David Murray3edd22a2011-04-18 13:59:37 -0400159
R David Murrayc27e5222012-05-25 15:01:48 -0400160 .. attribute:: cte_type
R David Murray3edd22a2011-04-18 13:59:37 -0400161
R David Murrayc27e5222012-05-25 15:01:48 -0400162 Controls the type of Content Transfer Encodings that may be or are
163 required to be used. The possible values are:
164
Georg Brandl44ea77b2013-03-28 13:28:44 +0100165 .. tabularcolumns:: |l|L|
166
R David Murrayc27e5222012-05-25 15:01:48 -0400167 ======== ===============================================================
168 ``7bit`` all data must be "7 bit clean" (ASCII-only). This means that
169 where necessary data will be encoded using either
170 quoted-printable or base64 encoding.
171
172 ``8bit`` data is not constrained to be 7 bit clean. Data in headers is
173 still required to be ASCII-only and so will be encoded (see
174 'binary_fold' below for an exception), but body parts may use
175 the ``8bit`` CTE.
176 ======== ===============================================================
177
178 A ``cte_type`` value of ``8bit`` only works with ``BytesGenerator``, not
179 ``Generator``, because strings cannot contain binary data. If a
180 ``Generator`` is operating under a policy that specifies
181 ``cte_type=8bit``, it will act as if ``cte_type`` is ``7bit``.
R David Murray3edd22a2011-04-18 13:59:37 -0400182
183 .. attribute:: raise_on_defect
184
185 If :const:`True`, any defects encountered will be raised as errors. If
186 :const:`False` (the default), defects will be passed to the
187 :meth:`register_defect` method.
188
R David Murrayc27e5222012-05-25 15:01:48 -0400189 The following :class:`Policy` method is intended to be called by code using
190 the email library to create policy instances with custom settings:
R David Murray6a45d3b2011-04-18 16:00:47 -0400191
R David Murrayc27e5222012-05-25 15:01:48 -0400192 .. method:: clone(**kw)
R David Murray3edd22a2011-04-18 13:59:37 -0400193
194 Return a new :class:`Policy` instance whose attributes have the same
195 values as the current instance, except where those attributes are
196 given new values by the keyword arguments.
197
R David Murrayc27e5222012-05-25 15:01:48 -0400198 The remaining :class:`Policy` methods are called by the email package code,
199 and are not intended to be called by an application using the email package.
200 A custom policy must implement all of these methods.
R David Murray3edd22a2011-04-18 13:59:37 -0400201
R David Murrayc27e5222012-05-25 15:01:48 -0400202 .. method:: handle_defect(obj, defect)
R David Murray3edd22a2011-04-18 13:59:37 -0400203
R David Murrayc27e5222012-05-25 15:01:48 -0400204 Handle a *defect* found on *obj*. When the email package calls this
205 method, *defect* will always be a subclass of
206 :class:`~email.errors.Defect`.
R David Murray3edd22a2011-04-18 13:59:37 -0400207
R David Murrayc27e5222012-05-25 15:01:48 -0400208 The default implementation checks the :attr:`raise_on_defect` flag. If
209 it is ``True``, *defect* is raised as an exception. If it is ``False``
210 (the default), *obj* and *defect* are passed to :meth:`register_defect`.
R David Murray3edd22a2011-04-18 13:59:37 -0400211
R David Murrayc27e5222012-05-25 15:01:48 -0400212 .. method:: register_defect(obj, defect)
R David Murray3edd22a2011-04-18 13:59:37 -0400213
R David Murrayc27e5222012-05-25 15:01:48 -0400214 Register a *defect* on *obj*. In the email package, *defect* will always
215 be a subclass of :class:`~email.errors.Defect`.
R David Murray3edd22a2011-04-18 13:59:37 -0400216
R David Murrayc27e5222012-05-25 15:01:48 -0400217 The default implementation calls the ``append`` method of the ``defects``
218 attribute of *obj*. When the email package calls :attr:`handle_defect`,
219 *obj* will normally have a ``defects`` attribute that has an ``append``
220 method. Custom object types used with the email package (for example,
221 custom ``Message`` objects) should also provide such an attribute,
222 otherwise defects in parsed messages will raise unexpected errors.
R David Murray3edd22a2011-04-18 13:59:37 -0400223
R David Murrayabfc3742012-05-29 09:14:44 -0400224 .. method:: header_max_count(name)
225
226 Return the maximum allowed number of headers named *name*.
227
228 Called when a header is added to a :class:`~email.message.Message`
229 object. If the returned value is not ``0`` or ``None``, and there are
230 already a number of headers with the name *name* equal to the value
231 returned, a :exc:`ValueError` is raised.
232
233 Because the default behavior of ``Message.__setitem__`` is to append the
234 value to the list of headers, it is easy to create duplicate headers
235 without realizing it. This method allows certain headers to be limited
236 in the number of instances of that header that may be added to a
237 ``Message`` programmatically. (The limit is not observed by the parser,
238 which will faithfully produce as many headers as exist in the message
239 being parsed.)
240
241 The default implementation returns ``None`` for all header names.
242
R David Murrayc27e5222012-05-25 15:01:48 -0400243 .. method:: header_source_parse(sourcelines)
R David Murray3edd22a2011-04-18 13:59:37 -0400244
R David Murrayc27e5222012-05-25 15:01:48 -0400245 The email package calls this method with a list of strings, each string
246 ending with the line separation characters found in the source being
247 parsed. The first line includes the field header name and separator.
248 All whitespace in the source is preserved. The method should return the
249 ``(name, value)`` tuple that is to be stored in the ``Message`` to
250 represent the parsed header.
R David Murray3edd22a2011-04-18 13:59:37 -0400251
R David Murrayc27e5222012-05-25 15:01:48 -0400252 If an implementation wishes to retain compatibility with the existing
253 email package policies, *name* should be the case preserved name (all
254 characters up to the '``:``' separator), while *value* should be the
255 unfolded value (all line separator characters removed, but whitespace
256 kept intact), stripped of leading whitespace.
R David Murray3edd22a2011-04-18 13:59:37 -0400257
R David Murrayc27e5222012-05-25 15:01:48 -0400258 *sourcelines* may contain surrogateescaped binary data.
259
260 There is no default implementation
261
262 .. method:: header_store_parse(name, value)
263
264 The email package calls this method with the name and value provided by
265 the application program when the application program is modifying a
266 ``Message`` programmatically (as opposed to a ``Message`` created by a
267 parser). The method should return the ``(name, value)`` tuple that is to
268 be stored in the ``Message`` to represent the header.
269
270 If an implementation wishes to retain compatibility with the existing
271 email package policies, the *name* and *value* should be strings or
272 string subclasses that do not change the content of the passed in
273 arguments.
274
275 There is no default implementation
276
277 .. method:: header_fetch_parse(name, value)
278
279 The email package calls this method with the *name* and *value* currently
280 stored in the ``Message`` when that header is requested by the
281 application program, and whatever the method returns is what is passed
282 back to the application as the value of the header being retrieved.
283 Note that there may be more than one header with the same name stored in
284 the ``Message``; the method is passed the specific name and value of the
285 header destined to be returned to the application.
286
287 *value* may contain surrogateescaped binary data. There should be no
288 surrogateescaped binary data in the value returned by the method.
289
290 There is no default implementation
291
292 .. method:: fold(name, value)
293
294 The email package calls this method with the *name* and *value* currently
295 stored in the ``Message`` for a given header. The method should return a
296 string that represents that header "folded" correctly (according to the
297 policy settings) by composing the *name* with the *value* and inserting
298 :attr:`linesep` characters at the appropriate places. See :rfc:`5322`
299 for a discussion of the rules for folding email headers.
300
301 *value* may contain surrogateescaped binary data. There should be no
302 surrogateescaped binary data in the string returned by the method.
303
304 .. method:: fold_binary(name, value)
305
306 The same as :meth:`fold`, except that the returned value should be a
307 bytes object rather than a string.
308
309 *value* may contain surrogateescaped binary data. These could be
310 converted back into binary data in the returned bytes object.
311
312
313.. class:: Compat32(**kw)
314
315 This concrete :class:`Policy` is the backward compatibility policy. It
316 replicates the behavior of the email package in Python 3.2. The
317 :mod:`policy` module also defines an instance of this class,
318 :const:`compat32`, that is used as the default policy. Thus the default
319 behavior of the email package is to maintain compatibility with Python 3.2.
320
321 The class provides the following concrete implementations of the
322 abstract methods of :class:`Policy`:
323
324 .. method:: header_source_parse(sourcelines)
325
326 The name is parsed as everything up to the '``:``' and returned
327 unmodified. The value is determined by stripping leading whitespace off
328 the remainder of the first line, joining all subsequent lines together,
329 and stripping any trailing carriage return or linefeed characters.
330
331 .. method:: header_store_parse(name, value)
332
333 The name and value are returned unmodified.
334
335 .. method:: header_fetch_parse(name, value)
336
337 If the value contains binary data, it is converted into a
338 :class:`~email.header.Header` object using the ``unknown-8bit`` charset.
339 Otherwise it is returned unmodified.
340
341 .. method:: fold(name, value)
342
343 Headers are folded using the :class:`~email.header.Header` folding
344 algorithm, which preserves existing line breaks in the value, and wraps
345 each resulting line to the ``max_line_length``. Non-ASCII binary data are
346 CTE encoded using the ``unknown-8bit`` charset.
347
348 .. method:: fold_binary(name, value)
349
350 Headers are folded using the :class:`~email.header.Header` folding
351 algorithm, which preserves existing line breaks in the value, and wraps
352 each resulting line to the ``max_line_length``. If ``cte_type`` is
353 ``7bit``, non-ascii binary data is CTE encoded using the ``unknown-8bit``
354 charset. Otherwise the original source header is used, with its existing
Terry Jan Reedy0f847642013-03-11 18:34:00 -0400355 line breaks and any (RFC invalid) binary data it may contain.
R David Murray0b6f6c82012-05-25 18:42:14 -0400356
357
358.. note::
359
R David Murrayea976682012-05-27 15:03:38 -0400360 The documentation below describes new policies that are included in the
361 standard library on a :term:`provisional basis <provisional package>`.
362 Backwards incompatible changes (up to and including removal of the feature)
363 may occur if deemed necessary by the core developers.
R David Murray0b6f6c82012-05-25 18:42:14 -0400364
365
366.. class:: EmailPolicy(**kw)
367
368 This concrete :class:`Policy` provides behavior that is intended to be fully
369 compliant with the current email RFCs. These include (but are not limited
370 to) :rfc:`5322`, :rfc:`2047`, and the current MIME RFCs.
371
372 This policy adds new header parsing and folding algorithms. Instead of
373 simple strings, headers are custom objects with custom attributes depending
374 on the type of the field. The parsing and folding algorithm fully implement
375 :rfc:`2047` and :rfc:`5322`.
376
377 In addition to the settable attributes listed above that apply to all
378 policies, this policy adds the following additional attributes:
379
380 .. attribute:: refold_source
381
382 If the value for a header in the ``Message`` object originated from a
383 :mod:`~email.parser` (as opposed to being set by a program), this
384 attribute indicates whether or not a generator should refold that value
385 when transforming the message back into stream form. The possible values
386 are:
387
388 ======== ===============================================================
389 ``none`` all source values use original folding
390
391 ``long`` source values that have any line that is longer than
392 ``max_line_length`` will be refolded
393
394 ``all`` all values are refolded.
395 ======== ===============================================================
396
397 The default is ``long``.
398
399 .. attribute:: header_factory
400
401 A callable that takes two arguments, ``name`` and ``value``, where
402 ``name`` is a header field name and ``value`` is an unfolded header field
R David Murrayea976682012-05-27 15:03:38 -0400403 value, and returns a string subclass that represents that header. A
404 default ``header_factory`` (see :mod:`~email.headerregistry`) is provided
405 that understands some of the :RFC:`5322` header field types. (Currently
406 address fields and date fields have special treatment, while all other
407 fields are treated as unstructured. This list will be completed before
408 the extension is marked stable.)
R David Murray0b6f6c82012-05-25 18:42:14 -0400409
410 The class provides the following concrete implementations of the abstract
411 methods of :class:`Policy`:
412
R David Murrayabfc3742012-05-29 09:14:44 -0400413 .. method:: header_max_count(name)
414
415 Returns the value of the
416 :attr:`~email.headerregistry.BaseHeader.max_count` attribute of the
417 specialized class used to represent the header with the given name.
418
R David Murray0b6f6c82012-05-25 18:42:14 -0400419 .. method:: header_source_parse(sourcelines)
420
421 The implementation of this method is the same as that for the
422 :class:`Compat32` policy.
423
424 .. method:: header_store_parse(name, value)
425
426 The name is returned unchanged. If the input value has a ``name``
427 attribute and it matches *name* ignoring case, the value is returned
428 unchanged. Otherwise the *name* and *value* are passed to
429 ``header_factory``, and the resulting custom header object is returned as
430 the value. In this case a ``ValueError`` is raised if the input value
431 contains CR or LF characters.
432
433 .. method:: header_fetch_parse(name, value)
434
435 If the value has a ``name`` attribute, it is returned to unmodified.
436 Otherwise the *name*, and the *value* with any CR or LF characters
437 removed, are passed to the ``header_factory``, and the resulting custom
438 header object is returned. Any surrogateescaped bytes get turned into
439 the unicode unknown-character glyph.
440
441 .. method:: fold(name, value)
442
443 Header folding is controlled by the :attr:`refold_source` policy setting.
444 A value is considered to be a 'source value' if and only if it does not
445 have a ``name`` attribute (having a ``name`` attribute means it is a
446 header object of some sort). If a source value needs to be refolded
447 according to the policy, it is converted into a custom header object by
448 passing the *name* and the *value* with any CR and LF characters removed
449 to the ``header_factory``. Folding of a custom header object is done by
450 calling its ``fold`` method with the current policy.
451
452 Source values are split into lines using :meth:`~str.splitlines`. If
453 the value is not to be refolded, the lines are rejoined using the
454 ``linesep`` from the policy and returned. The exception is lines
455 containing non-ascii binary data. In that case the value is refolded
456 regardless of the ``refold_source`` setting, which causes the binary data
457 to be CTE encoded using the ``unknown-8bit`` charset.
458
459 .. method:: fold_binary(name, value)
460
461 The same as :meth:`fold` if :attr:`cte_type` is ``7bit``, except that
462 the returned value is bytes.
463
464 If :attr:`cte_type` is ``8bit``, non-ASCII binary data is converted back
465 into bytes. Headers with binary data are not refolded, regardless of the
466 ``refold_header`` setting, since there is no way to know whether the
467 binary data consists of single byte characters or multibyte characters.
468
469The following instances of :class:`EmailPolicy` provide defaults suitable for
470specific application domains. Note that in the future the behavior of these
Georg Brandl38e0e1e2012-05-27 09:31:10 +0200471instances (in particular the ``HTTP`` instance) may be adjusted to conform even
R David Murray0b6f6c82012-05-25 18:42:14 -0400472more closely to the RFCs relevant to their domains.
473
474.. data:: default
475
476 An instance of ``EmailPolicy`` with all defaults unchanged. This policy
477 uses the standard Python ``\n`` line endings rather than the RFC-correct
478 ``\r\n``.
479
480.. data:: SMTP
481
482 Suitable for serializing messages in conformance with the email RFCs.
483 Like ``default``, but with ``linesep`` set to ``\r\n``, which is RFC
484 compliant.
485
486.. data:: HTTP
487
488 Suitable for serializing headers with for use in HTTP traffic. Like
489 ``SMTP`` except that ``max_line_length`` is set to ``None`` (unlimited).
490
491.. data:: strict
492
493 Convenience instance. The same as ``default`` except that
494 ``raise_on_defect`` is set to ``True``. This allows any policy to be made
495 strict by writing::
496
497 somepolicy + policy.strict
498
499With all of these :class:`EmailPolicies <.EmailPolicy>`, the effective API of
500the email package is changed from the Python 3.2 API in the following ways:
501
502 * Setting a header on a :class:`~email.message.Message` results in that
503 header being parsed and a custom header object created.
504
505 * Fetching a header value from a :class:`~email.message.Message` results
506 in that header being parsed and a custom header object created and
507 returned.
508
509 * Any custom header object, or any header that is refolded due to the
510 policy settings, is folded using an algorithm that fully implements the
511 RFC folding algorithms, including knowing where encoded words are required
512 and allowed.
513
514From the application view, this means that any header obtained through the
515:class:`~email.message.Message` is a custom header object with custom
516attributes, whose string value is the fully decoded unicode value of the
517header. Likewise, a header may be assigned a new value, or a new header
518created, using a unicode string, and the policy will take care of converting
519the unicode string into the correct RFC encoded form.
520
R David Murrayea976682012-05-27 15:03:38 -0400521The custom header objects and their attributes are described in
522:mod:`~email.headerregistry`.