@@ -31,19 +31,32 @@ parameters, and for recursively walking over the object tree.
3131Here are the methods of the :class: `Message ` class:
3232
3333
34- .. class :: Message()
34+ .. class :: Message(policy=compat32 )
3535
36- The constructor takes no arguments.
36+ The *policy * argument determiens the :mod: `~email.policy ` that will be used
37+ to update the message model. The default value, :class: `compat32
38+ <email.policy.Compat32> ` maintains backward compatibility with the
39+ Python 3.2 version of the email package. For more information see the
40+ :mod: `~email.policy ` documentation.
3741
42+ .. versionchanged :: 3.3 The *policy* keyword argument was added.
3843
39- .. method :: as_string(unixfrom=False, maxheaderlen=0)
44+
45+ .. method :: as_string(unixfrom=False, maxheaderlen=0, policy=None)
4046
4147 Return the entire message flattened as a string. When optional *unixfrom *
42- is ``True ``, the envelope header is included in the returned string.
43- *unixfrom * defaults to ``False ``. Flattening the message may trigger
44- changes to the :class: `Message ` if defaults need to be filled in to
45- complete the transformation to a string (for example, MIME boundaries may
46- be generated or modified).
48+ is true, the envelope header is included in the returned string.
49+ *unixfrom * defaults to ``False ``. For backward compabitility reasons,
50+ *maxheaderlen * defaults to ``0 ``, so if you want a different value you
51+ must override it explicitly (the value specified for *max_line_length * in
52+ the policy will be ignored by this method). The *policy * argument may be
53+ used to override the default policy obtained from the message instance.
54+ This can be used to control some of the formatting produced by the
55+ method, since the specified *policy * will be passed to the ``Generator ``.
56+
57+ Flattening the message may trigger changes to the :class: `Message ` if
58+ defaults need to be filled in to complete the transformation to a string
59+ (for example, MIME boundaries may be generated or modified).
4760
4861 Note that this method is provided as a convenience and may not always
4962 format the message the way you want. For example, by default it does
@@ -59,10 +72,57 @@ Here are the methods of the :class:`Message` class:
5972 g.flatten(msg)
6073 text = fp.getvalue()
6174
75+ If the message object contains binary data that is not encoded according
76+ to RFC standards, the non-compliant data will be replaced by unicode
77+ "unknown character" code points. (See also :meth: `.as_bytes ` and
78+ :class: `~email.generator.BytesGenerator `.)
79+
80+ .. versionchanged :: 3.4 the *policy* keyword argument was added.
81+
6282
6383 .. method :: __str__()
6484
65- Equivalent to ``as_string(unixfrom=True) ``.
85+ Equivalent to :meth: `.as_string() `. Allows ``str(msg) `` to produce a
86+ string containing the formatted message.
87+
88+
89+ .. method :: as_bytes(unixfrom=False, policy=None)
90+
91+ Return the entire message flattened as a bytes object. When optional
92+ *unixfrom * is true, the envelope header is included in the returned
93+ string. *unixfrom * defaults to ``False ``. The *policy * argument may be
94+ used to override the default policy obtained from the message instance.
95+ This can be used to control some of the formatting produced by the
96+ method, since the specified *policy * will be passed to the
97+ ``BytesGenerator ``.
98+
99+ Flattening the message may trigger changes to the :class: `Message ` if
100+ defaults need to be filled in to complete the transformation to a string
101+ (for example, MIME boundaries may be generated or modified).
102+
103+ Note that this method is provided as a convenience and may not always
104+ format the message the way you want. For example, by default it does
105+ not do the mangling of lines that begin with ``From `` that is
106+ required by the unix mbox format. For more flexibility, instantiate a
107+ :class: `~email.generator.BytesGenerator ` instance and use its
108+ :meth: `flatten ` method directly. For example::
109+
110+ from io import BytesIO
111+ from email.generator import BytesGenerator
112+ fp = BytesIO()
113+ g = BytesGenerator(fp, mangle_from_=True, maxheaderlen=60)
114+ g.flatten(msg)
115+ text = fp.getvalue()
116+
117+ .. versionadded :: 3.4
118+
119+
120+ .. method :: __bytes__()
121+
122+ Equivalent to :meth: `.as_bytes() `. Allows ``bytes(msg) `` to produce a
123+ bytes object containing the formatted message.
124+
125+ .. versionadded :: 3.4
66126
67127
68128 .. method :: is_multipart()
0 commit comments