17db96d56Sopenharmony_ci:mod:`email.encoders`: Encoders 27db96d56Sopenharmony_ci------------------------------- 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: email.encoders 57db96d56Sopenharmony_ci :synopsis: Encoders for email message payloads. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci**Source code:** :source:`Lib/email/encoders.py` 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ci-------------- 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ciThis module is part of the legacy (``Compat32``) email API. In the 127db96d56Sopenharmony_cinew API the functionality is provided by the *cte* parameter of 137db96d56Sopenharmony_cithe :meth:`~email.message.EmailMessage.set_content` method. 147db96d56Sopenharmony_ci 157db96d56Sopenharmony_ciThis module is deprecated in Python 3. The functions provided here 167db96d56Sopenharmony_cishould not be called explicitly since the :class:`~email.mime.text.MIMEText` 177db96d56Sopenharmony_ciclass sets the content type and CTE header using the *_subtype* and *_charset* 187db96d56Sopenharmony_civalues passed during the instantiation of that class. 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ciThe remaining text in this section is the original documentation of the module. 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ciWhen creating :class:`~email.message.Message` objects from scratch, you often 237db96d56Sopenharmony_cineed to encode the payloads for transport through compliant mail servers. This 247db96d56Sopenharmony_ciis especially true for :mimetype:`image/\*` and :mimetype:`text/\*` type messages 257db96d56Sopenharmony_cicontaining binary data. 267db96d56Sopenharmony_ci 277db96d56Sopenharmony_ciThe :mod:`email` package provides some convenient encoders in its 287db96d56Sopenharmony_ci:mod:`encoders` module. These encoders are actually used by the 297db96d56Sopenharmony_ci:class:`~email.mime.audio.MIMEAudio` and :class:`~email.mime.image.MIMEImage` 307db96d56Sopenharmony_ciclass constructors to provide default encodings. All encoder functions take 317db96d56Sopenharmony_ciexactly one argument, the message object to encode. They usually extract the 327db96d56Sopenharmony_cipayload, encode it, and reset the payload to this newly encoded value. They 337db96d56Sopenharmony_cishould also set the :mailheader:`Content-Transfer-Encoding` header as appropriate. 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ciNote that these functions are not meaningful for a multipart message. They 367db96d56Sopenharmony_cimust be applied to individual subparts instead, and will raise a 377db96d56Sopenharmony_ci:exc:`TypeError` if passed a message whose type is multipart. 387db96d56Sopenharmony_ci 397db96d56Sopenharmony_ciHere are the encoding functions provided: 407db96d56Sopenharmony_ci 417db96d56Sopenharmony_ci 427db96d56Sopenharmony_ci.. function:: encode_quopri(msg) 437db96d56Sopenharmony_ci 447db96d56Sopenharmony_ci Encodes the payload into quoted-printable form and sets the 457db96d56Sopenharmony_ci :mailheader:`Content-Transfer-Encoding` header to ``quoted-printable`` [#]_. 467db96d56Sopenharmony_ci This is a good encoding to use when most of your payload is normal printable 477db96d56Sopenharmony_ci data, but contains a few unprintable characters. 487db96d56Sopenharmony_ci 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ci.. function:: encode_base64(msg) 517db96d56Sopenharmony_ci 527db96d56Sopenharmony_ci Encodes the payload into base64 form and sets the 537db96d56Sopenharmony_ci :mailheader:`Content-Transfer-Encoding` header to ``base64``. This is a good 547db96d56Sopenharmony_ci encoding to use when most of your payload is unprintable data since it is a more 557db96d56Sopenharmony_ci compact form than quoted-printable. The drawback of base64 encoding is that it 567db96d56Sopenharmony_ci renders the text non-human readable. 577db96d56Sopenharmony_ci 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ci.. function:: encode_7or8bit(msg) 607db96d56Sopenharmony_ci 617db96d56Sopenharmony_ci This doesn't actually modify the message's payload, but it does set the 627db96d56Sopenharmony_ci :mailheader:`Content-Transfer-Encoding` header to either ``7bit`` or ``8bit`` as 637db96d56Sopenharmony_ci appropriate, based on the payload data. 647db96d56Sopenharmony_ci 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ci.. function:: encode_noop(msg) 677db96d56Sopenharmony_ci 687db96d56Sopenharmony_ci This does nothing; it doesn't even set the 697db96d56Sopenharmony_ci :mailheader:`Content-Transfer-Encoding` header. 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci.. rubric:: Footnotes 727db96d56Sopenharmony_ci 737db96d56Sopenharmony_ci.. [#] Note that encoding with :meth:`encode_quopri` also encodes all tabs and space 747db96d56Sopenharmony_ci characters in the data. 757db96d56Sopenharmony_ci 76