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