You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.3 KiB

  1. :mod:`email`: Encoders
  2. ----------------------
  3. .. module:: email.encoders
  4. :synopsis: Encoders for email message payloads.
  5. When creating :class:`~email.message.Message` objects from scratch, you often
  6. need to encode the payloads for transport through compliant mail servers. This
  7. is especially true for :mimetype:`image/\*` and :mimetype:`text/\*` type messages
  8. containing binary data.
  9. The :mod:`email` package provides some convenient encodings in its
  10. :mod:`encoders` module. These encoders are actually used by the
  11. :class:`~email.mime.audio.MIMEAudio` and :class:`~email.mime.image.MIMEImage`
  12. class constructors to provide default encodings. All encoder functions take
  13. exactly one argument, the message object to encode. They usually extract the
  14. payload, encode it, and reset the payload to this newly encoded value. They
  15. should also set the :mailheader:`Content-Transfer-Encoding` header as appropriate.
  16. Note that these functions are not meaningful for a multipart message. They
  17. must be applied to individual subparts instead, and will raise a
  18. :exc:`TypeError` if passed a message whose type is multipart.
  19. Here are the encoding functions provided:
  20. .. function:: encode_quopri(msg)
  21. Encodes the payload into quoted-printable form and sets the
  22. :mailheader:`Content-Transfer-Encoding` header to ``quoted-printable`` [#]_.
  23. This is a good encoding to use when most of your payload is normal printable
  24. data, but contains a few unprintable characters.
  25. .. function:: encode_base64(msg)
  26. Encodes the payload into base64 form and sets the
  27. :mailheader:`Content-Transfer-Encoding` header to ``base64``. This is a good
  28. encoding to use when most of your payload is unprintable data since it is a more
  29. compact form than quoted-printable. The drawback of base64 encoding is that it
  30. renders the text non-human readable.
  31. .. function:: encode_7or8bit(msg)
  32. This doesn't actually modify the message's payload, but it does set the
  33. :mailheader:`Content-Transfer-Encoding` header to either ``7bit`` or ``8bit`` as
  34. appropriate, based on the payload data.
  35. .. function:: encode_noop(msg)
  36. This does nothing; it doesn't even set the
  37. :mailheader:`Content-Transfer-Encoding` header.
  38. .. rubric:: Footnotes
  39. .. [#] Note that encoding with :meth:`encode_quopri` also encodes all tabs and space
  40. characters in the data.