blob: c1b103ee89df03bf89411c880d83309c41e74185 [file] [log] [blame]
R David Murray3da240f2013-10-16 22:48:40 -04001:mod:`email.contentmanager`: Managing MIME Content
2--------------------------------------------------
3
4.. module:: email.contentmanager
5 :synopsis: Storing and Retrieving Content from MIME Parts
6
7.. moduleauthor:: R. David Murray <rdmurray@bitdance.com>
8.. sectionauthor:: R. David Murray <rdmurray@bitdance.com>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/email/contentmanager.py`
R David Murray3da240f2013-10-16 22:48:40 -040011
R David Murray29d1bc02016-09-07 21:15:59 -040012------------
R David Murray3da240f2013-10-16 22:48:40 -040013
R David Murray29d1bc02016-09-07 21:15:59 -040014.. versionadded:: 3.4 as a :term:`provisional module <provisional package>`.
R David Murray3da240f2013-10-16 22:48:40 -040015
R David Murray29d1bc02016-09-07 21:15:59 -040016.. versionchanged:: 3.6 provisional status removed.
Larry Hastings3732ed22014-03-15 21:13:56 -070017
Larry Hastings3732ed22014-03-15 21:13:56 -070018
R David Murray3da240f2013-10-16 22:48:40 -040019.. class:: ContentManager()
20
21 Base class for content managers. Provides the standard registry mechanisms
22 to register converters between MIME content and other representations, as
23 well as the ``get_content`` and ``set_content`` dispatch methods.
24
25
26 .. method:: get_content(msg, *args, **kw)
27
28 Look up a handler function based on the ``mimetype`` of *msg* (see next
29 paragraph), call it, passing through all arguments, and return the result
30 of the call. The expectation is that the handler will extract the
31 payload from *msg* and return an object that encodes information about
32 the extracted data.
33
34 To find the handler, look for the following keys in the registry,
35 stopping with the first one found:
36
37 * the string representing the full MIME type (``maintype/subtype``)
38 * the string representing the ``maintype``
39 * the empty string
40
41 If none of these keys produce a handler, raise a :exc:`KeyError` for the
42 full MIME type.
43
44
45 .. method:: set_content(msg, obj, *args, **kw)
46
47 If the ``maintype`` is ``multipart``, raise a :exc:`TypeError`; otherwise
48 look up a handler function based on the type of *obj* (see next
49 paragraph), call :meth:`~email.message.EmailMessage.clear_content` on the
50 *msg*, and call the handler function, passing through all arguments. The
51 expectation is that the handler will transform and store *obj* into
52 *msg*, possibly making other changes to *msg* as well, such as adding
53 various MIME headers to encode information needed to interpret the stored
54 data.
55
56 To find the handler, obtain the type of *obj* (``typ = type(obj)``), and
57 look for the following keys in the registry, stopping with the first one
58 found:
59
60 * the type itself (``typ``)
61 * the type's fully qualified name (``typ.__module__ + '.' +
62 typ.__qualname__``).
63 * the type's qualname (``typ.__qualname__``)
64 * the type's name (``typ.__name__``).
65
66 If none of the above match, repeat all of the checks above for each of
67 the types in the :term:`MRO` (``typ.__mro__``). Finally, if no other key
68 yields a handler, check for a handler for the key ``None``. If there is
69 no handler for ``None``, raise a :exc:`KeyError` for the fully
70 qualified name of the type.
71
72 Also add a :mailheader:`MIME-Version` header if one is not present (see
73 also :class:`.MIMEPart`).
74
75
76 .. method:: add_get_handler(key, handler)
77
78 Record the function *handler* as the handler for *key*. For the possible
79 values of *key*, see :meth:`get_content`.
80
81
82 .. method:: add_set_handler(typekey, handler)
83
84 Record *handler* as the function to call when an object of a type
85 matching *typekey* is passed to :meth:`set_content`. For the possible
86 values of *typekey*, see :meth:`set_content`.
87
88
R David Murray3da240f2013-10-16 22:48:40 -040089Content Manager Instances
90~~~~~~~~~~~~~~~~~~~~~~~~~
91
92Currently the email package provides only one concrete content manager,
93:data:`raw_data_manager`, although more may be added in the future.
94:data:`raw_data_manager` is the
95:attr:`~email.policy.EmailPolicy.content_manager` provided by
96:attr:`~email.policy.EmailPolicy` and its derivatives.
97
98
99.. data:: raw_data_manager
100
101 This content manager provides only a minimum interface beyond that provided
102 by :class:`~email.message.Message` itself: it deals only with text, raw
103 byte strings, and :class:`~email.message.Message` objects. Nevertheless, it
104 provides significant advantages compared to the base API: ``get_content`` on
105 a text part will return a unicode string without the application needing to
106 manually decode it, ``set_content`` provides a rich set of options for
107 controlling the headers added to a part and controlling the content transfer
108 encoding, and it enables the use of the various ``add_`` methods, thereby
109 simplifying the creation of multipart messages.
110
111 .. method:: get_content(msg, errors='replace')
112
Martin Panterd210a702016-08-20 08:03:06 +0000113 Return the payload of the part as either a string (for ``text`` parts), an
R David Murray3da240f2013-10-16 22:48:40 -0400114 :class:`~email.message.EmailMessage` object (for ``message/rfc822``
115 parts), or a ``bytes`` object (for all other non-multipart types). Raise
116 a :exc:`KeyError` if called on a ``multipart``. If the part is a
117 ``text`` part and *errors* is specified, use it as the error handler when
118 decoding the payload to unicode. The default error handler is
119 ``replace``.
120
121 .. method:: set_content(msg, <'str'>, subtype="plain", charset='utf-8' \
122 cte=None, \
123 disposition=None, filename=None, cid=None, \
124 params=None, headers=None)
125 set_content(msg, <'bytes'>, maintype, subtype, cte="base64", \
126 disposition=None, filename=None, cid=None, \
127 params=None, headers=None)
R David Murray29d1bc02016-09-07 21:15:59 -0400128 set_content(msg, <'EmailMessage'>, cte=None, \
R David Murray3da240f2013-10-16 22:48:40 -0400129 disposition=None, filename=None, cid=None, \
130 params=None, headers=None)
131 set_content(msg, <'list'>, subtype='mixed', \
132 disposition=None, filename=None, cid=None, \
133 params=None, headers=None)
134
135 Add headers and payload to *msg*:
136
137 Add a :mailheader:`Content-Type` header with a ``maintype/subtype``
138 value.
139
140 * For ``str``, set the MIME ``maintype`` to ``text``, and set the
141 subtype to *subtype* if it is specified, or ``plain`` if it is not.
142 * For ``bytes``, use the specified *maintype* and *subtype*, or
143 raise a :exc:`TypeError` if they are not specified.
R David Murray29d1bc02016-09-07 21:15:59 -0400144 * For :class:`~email.message.EmailMessage` objects, set the maintype
145 to ``message``, and set the subtype to *subtype* if it is
146 specified or ``rfc822`` if it is not. If *subtype* is
147 ``partial``, raise an error (``bytes`` objects must be used to
148 construct ``message/partial`` parts).
R David Murray3da240f2013-10-16 22:48:40 -0400149 * For *<'list'>*, which should be a list of
R David Murray29d1bc02016-09-07 21:15:59 -0400150 :class:`~email.message.EmailMessage` objects, set the ``maintype``
151 to ``multipart``, and the ``subtype`` to *subtype* if it is
R David Murray3da240f2013-10-16 22:48:40 -0400152 specified, and ``mixed`` if it is not. If the message parts in
153 the *<'list'>* have :mailheader:`MIME-Version` headers, remove
154 them.
155
156 If *charset* is provided (which is valid only for ``str``), encode the
157 string to bytes using the specified character set. The default is
158 ``utf-8``. If the specified *charset* is a known alias for a standard
159 MIME charset name, use the standard charset instead.
160
161 If *cte* is set, encode the payload using the specified content transfer
162 encoding, and set the :mailheader:`Content-Transfer-Endcoding` header to
R David Murray29d1bc02016-09-07 21:15:59 -0400163 that value. Possible values for *cte* are ``quoted-printable``,
164 ``base64``, ``7bit``, ``8bit``, and ``binary``. If the input cannot be
165 encoded in the specified encoding (for example, specifying a *cte* of
166 ``7bit`` for an input that contains non-ASCII values), raise a
167 :exc:`ValueError`.
168
169 * For ``str`` objects, if *cte* is not set use heuristics to
170 determine the most compact encoding.
171 * For :class:`~email.message.EmailMessage`, per :rfc:`2046`, raise
172 an error if a *cte* of ``quoted-printable`` or ``base64`` is
173 requested for *subtype* ``rfc822``, and for any *cte* other than
174 ``7bit`` for *subtype* ``external-body``. For
175 ``message/rfc822``, use ``8bit`` if *cte* is not specified. For
176 all other values of *subtype*, use ``7bit``.
R David Murray3da240f2013-10-16 22:48:40 -0400177
Berker Peksag9c1dba22014-09-28 00:00:58 +0300178 .. note:: A *cte* of ``binary`` does not actually work correctly yet.
R David Murray29d1bc02016-09-07 21:15:59 -0400179 The ``EmailMessage`` object as modified by ``set_content`` is
180 correct, but :class:`~email.generator.BytesGenerator` does not
181 serialize it correctly.
R David Murray3da240f2013-10-16 22:48:40 -0400182
183 If *disposition* is set, use it as the value of the
184 :mailheader:`Content-Disposition` header. If not specified, and
185 *filename* is specified, add the header with the value ``attachment``.
R David Murray29d1bc02016-09-07 21:15:59 -0400186 If *disposition* is not specified and *filename* is also not specified,
187 do not add the header. The only valid values for *disposition* are
188 ``attachment`` and ``inline``.
R David Murray3da240f2013-10-16 22:48:40 -0400189
190 If *filename* is specified, use it as the value of the ``filename``
R David Murray29d1bc02016-09-07 21:15:59 -0400191 parameter of the :mailheader:`Content-Disposition` header.
R David Murray3da240f2013-10-16 22:48:40 -0400192
193 If *cid* is specified, add a :mailheader:`Content-ID` header with
194 *cid* as its value.
195
196 If *params* is specified, iterate its ``items`` method and use the
Berker Peksag4882cac2015-04-14 09:30:01 +0300197 resulting ``(key, value)`` pairs to set additional parameters on the
R David Murray3da240f2013-10-16 22:48:40 -0400198 :mailheader:`Content-Type` header.
199
200 If *headers* is specified and is a list of strings of the form
201 ``headername: headervalue`` or a list of ``header`` objects
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700202 (distinguished from strings by having a ``name`` attribute), add the
R David Murray3da240f2013-10-16 22:48:40 -0400203 headers to *msg*.