email.encoders: Encoders¶
Source code:Lib/email/encoders.py
This module is part of the legacy (Compat32) email API. In thenew API the functionality is provided by thecte parameter oftheset_content() method.
This module is deprecated in Python 3. The functions provided hereshould not be called explicitly since theMIMETextclass sets the content type and CTE header using the_subtype and_charsetvalues passed during the instantiation of that class.
The remaining text in this section is the original documentation of the module.
When creatingMessage objects from scratch, you oftenneed to encode the payloads for transport through compliant mail servers. Thisis especially true forimage/* andtext/* type messagescontaining binary data.
Theemail package provides some convenient encoders in itsencoders module. These encoders are actually used by theMIMEAudio andMIMEImageclass constructors to provide default encodings. All encoder functions takeexactly one argument, the message object to encode. They usually extract thepayload, encode it, and reset the payload to this newly encoded value. Theyshould also set theContent-Transfer-Encoding header as appropriate.
Note that these functions are not meaningful for a multipart message. Theymust be applied to individual subparts instead, and will raise aTypeError if passed a message whose type is multipart.
Here are the encoding functions provided:
- email.encoders.encode_quopri(msg)¶
Encodes the payload into quoted-printable form and sets theContent-Transfer-Encoding header to
quoted-printable[1].This is a good encoding to use when most of your payload is normal printabledata, but contains a few unprintable characters.
- email.encoders.encode_base64(msg)¶
Encodes the payload into base64 form and sets theContent-Transfer-Encoding header to
base64. This is a goodencoding to use when most of your payload is unprintable data since it is a morecompact form than quoted-printable. The drawback of base64 encoding is that itrenders the text non-human readable.
- email.encoders.encode_7or8bit(msg)¶
This doesn’t actually modify the message’s payload, but it does set theContent-Transfer-Encoding header to either
7bitor8bitasappropriate, based on the payload data.
- email.encoders.encode_noop(msg)¶
This does nothing; it doesn’t even set theContent-Transfer-Encoding header.
Footnotes
[1]Note that encoding withencode_quopri() also encodes all tabs and spacecharacters in the data.