# `email.encoders`: Encoders

Source code: Lib/email/encoders.py

This module is part of the legacy (`Compat32`) email API. In the new API the functionality is provided by the cte parameter of the `set_content()` method.

此模块在 Python 3 中已弃用。 这里提供的函数不应被显式地调用，因为 `MIMEText` 类会在类实例化期间使用 _subtype 和 _charset 值来设置内容类型和 CTE 头。

The remaining text in this section is the original documentation of the module.

When creating `Message` objects from scratch, you often need to encode the payloads for transport through compliant mail servers. This is especially true for image/* and text/* type messages containing binary data.

`email` 包在其 `encoders` 模块中提供了一些方便的编码器。 这些编码器实际上由 `MIMEAudio` 和 `MIMEImage` 类构造器所使用以提供默认编码格式。 所有编码器函数都只接受一个参数，即要编码的消息对象。 它们通常会提取有效载荷，对其进行编码，并将载荷重置为这种新编码的值。 它们还应当相应地设置 Content-Transfer-Encoding 标头。

Note that these functions are not meaningful for a multipart message. They must be applied to individual subparts instead, and will raise a `TypeError` 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 the Content-Transfer-Encoding header to `quoted-printable` 1. This is a good encoding to use when most of your payload is normal printable data, but contains a few unprintable characters.

`email.encoders.``encode_base64`(msg)

  Encodes the payload into base64 form and sets the Content-Transfer-Encoding header to `base64`. This is a good encoding to use when most of your payload is unprintable data since it is a more compact form than quoted-printable. The drawback of base64 encoding is that it renders the text non-human readable.

`email.encoders.``encode_7or8bit`(msg)

  This doesn't actually modify the message's payload, but it does set the Content-Transfer-Encoding header to either `7bit` or `8bit` as appropriate, based on the payload data.

`email.encoders.``encode_noop`(msg)

  This does nothing; it doesn't even set the Content-Transfer-Encoding header.

註解

1

  Note that encoding with `encode_quopri()` also encodes all tabs and space characters in the data.