blob: 916ba5d18a7caa53a9c82a457b87aa3ce1dd66a3 [file] [log] [blame]
R David Murray79cf3ba2012-05-27 17:10:36 -04001:mod:`email.encoders`: Encoders
2-------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: email.encoders
5 :synopsis: Encoders for email message payloads.
6
7
Georg Brandl3638e482009-04-27 16:46:17 +00008When creating :class:`~email.message.Message` objects from scratch, you often
9need to encode the payloads for transport through compliant mail servers. This
10is especially true for :mimetype:`image/\*` and :mimetype:`text/\*` type messages
11containing binary data.
Georg Brandl116aa622007-08-15 14:28:22 +000012
13The :mod:`email` package provides some convenient encodings in its
14:mod:`encoders` module. These encoders are actually used by the
Georg Brandl3638e482009-04-27 16:46:17 +000015:class:`~email.mime.audio.MIMEAudio` and :class:`~email.mime.image.MIMEImage`
16class constructors to provide default encodings. All encoder functions take
17exactly one argument, the message object to encode. They usually extract the
18payload, encode it, and reset the payload to this newly encoded value. They
19should also set the :mailheader:`Content-Transfer-Encoding` header as appropriate.
Georg Brandl116aa622007-08-15 14:28:22 +000020
R David Murray69ebfe42012-03-16 22:03:17 -040021Note that these functions are not meaningful for a multipart message. They
R David Murray41914342012-03-16 22:10:00 -040022must be applied to individual subparts instead, and will raise a
R David Murray69ebfe42012-03-16 22:03:17 -040023:exc:`TypeError` if passed a message whose type is multipart.
24
Georg Brandl116aa622007-08-15 14:28:22 +000025Here are the encoding functions provided:
26
27
28.. function:: encode_quopri(msg)
29
30 Encodes the payload into quoted-printable form and sets the
31 :mailheader:`Content-Transfer-Encoding` header to ``quoted-printable`` [#]_.
32 This is a good encoding to use when most of your payload is normal printable
33 data, but contains a few unprintable characters.
34
35
36.. function:: encode_base64(msg)
37
38 Encodes the payload into base64 form and sets the
39 :mailheader:`Content-Transfer-Encoding` header to ``base64``. This is a good
40 encoding to use when most of your payload is unprintable data since it is a more
41 compact form than quoted-printable. The drawback of base64 encoding is that it
42 renders the text non-human readable.
43
44
45.. function:: encode_7or8bit(msg)
46
47 This doesn't actually modify the message's payload, but it does set the
48 :mailheader:`Content-Transfer-Encoding` header to either ``7bit`` or ``8bit`` as
49 appropriate, based on the payload data.
50
51
52.. function:: encode_noop(msg)
53
54 This does nothing; it doesn't even set the
55 :mailheader:`Content-Transfer-Encoding` header.
56
57.. rubric:: Footnotes
58
59.. [#] Note that encoding with :meth:`encode_quopri` also encodes all tabs and space
60 characters in the data.
61