summaryrefslogtreecommitdiff
path: root/Doc/library/email.message.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/email.message.rst')
-rw-r--r--Doc/library/email.message.rst58
1 files changed, 39 insertions, 19 deletions
diff --git a/Doc/library/email.message.rst b/Doc/library/email.message.rst
index f685e540d2..3075e86d05 100644
--- a/Doc/library/email.message.rst
+++ b/Doc/library/email.message.rst
@@ -31,9 +31,15 @@ parameters, and for recursively walking over the object tree.
Here are the methods of the :class:`Message` class:
-.. class:: Message()
+.. class:: Message(policy=compat32)
- The constructor takes no arguments.
+ The *policy* argument determiens the :mod:`~email.policy` that will be used
+ to update the message model. The default value, :class:`compat32
+ <email.policy.Compat32>` maintains backward compatibility with the
+ Python 3.2 version of the email package. For more information see the
+ :mod:`~email.policy` documentation.
+
+ .. versionchanged:: 3.3 The *policy* keyword argument was added.
.. method:: as_string(unixfrom=False, maxheaderlen=0)
@@ -49,8 +55,8 @@ Here are the methods of the :class:`Message` class:
format the message the way you want. For example, by default it does
not do the mangling of lines that begin with ``From`` that is
required by the unix mbox format. For more flexibility, instantiate a
- :class:`~email.generator.Generator` instance and use its :meth:`flatten`
- method directly. For example::
+ :class:`~email.generator.Generator` instance and use its
+ :meth:`~email.generator.Generator.flatten` method directly. For example::
from io import StringIO
from email.generator import Generator
@@ -69,7 +75,7 @@ Here are the methods of the :class:`Message` class:
Return ``True`` if the message's payload is a list of sub-\
:class:`Message` objects, otherwise return ``False``. When
- :meth:`is_multipart` returns False, the payload should be a string object.
+ :meth:`is_multipart` returns ``False``, the payload should be a string object.
.. method:: set_unixfrom(unixfrom)
@@ -111,10 +117,14 @@ Here are the methods of the :class:`Message` class:
header. When ``True`` and the message is not a multipart, the payload will
be decoded if this header's value is ``quoted-printable`` or ``base64``.
If some other encoding is used, or :mailheader:`Content-Transfer-Encoding`
- header is missing, or if the payload has bogus base64 data, the payload is
+ header is missing, the payload is
returned as-is (undecoded). In all cases the returned value is binary
data. If the message is a multipart and the *decode* flag is ``True``,
- then ``None`` is returned.
+ then ``None`` is returned. If the payload is base64 and it was not
+ perfectly formed (missing padding, characters outside the base64
+ alphabet), then an appropriate defect will be added to the message's
+ defect property (:class:`~email.errors.InvalidBase64PaddingDefect` or
+ :class:`~email.errors.InvalidBase64CharactersDefect`, respectively).
When *decode* is ``False`` (the default) the body is returned as a string
without decoding the :mailheader:`Content-Transfer-Encoding`. However,
@@ -466,8 +476,8 @@ Here are the methods of the :class:`Message` class:
Set the ``boundary`` parameter of the :mailheader:`Content-Type` header to
*boundary*. :meth:`set_boundary` will always quote *boundary* if
- necessary. A :exc:`HeaderParseError` is raised if the message object has
- no :mailheader:`Content-Type` header.
+ necessary. A :exc:`~email.errors.HeaderParseError` is raised if the
+ message object has no :mailheader:`Content-Type` header.
Note that using this method is subtly different than deleting the old
:mailheader:`Content-Type` header and adding a new one with the new
@@ -509,16 +519,25 @@ Here are the methods of the :class:`Message` class:
iterator in a ``for`` loop; each iteration returns the next subpart.
Here's an example that prints the MIME type of every part of a multipart
- message structure::
+ message structure:
+
+ .. testsetup::
+
+ >>> from email import message_from_binary_file
+ >>> with open('Lib/test/test_email/data/msg_16.txt', 'rb') as f:
+ ... msg = message_from_binary_file(f)
+
+ .. doctest::
- >>> for part in msg.walk():
- ... print(part.get_content_type())
- multipart/report
- text/plain
- message/delivery-status
- text/plain
- text/plain
- message/rfc822
+ >>> for part in msg.walk():
+ ... print(part.get_content_type())
+ multipart/report
+ text/plain
+ message/delivery-status
+ text/plain
+ text/plain
+ message/rfc822
+ text/plain
:class:`Message` objects can also optionally contain two instance attributes,
which can be used when generating the plain text of a MIME message.
@@ -554,7 +573,8 @@ Here are the methods of the :class:`Message` class:
the end of the message.
You do not need to set the epilogue to the empty string in order for the
- :class:`Generator` to print a newline at the end of the file.
+ :class:`~email.generator.Generator` to print a newline at the end of the
+ file.
.. attribute:: defects