email.charset: Representing character sets¶
Source code:Lib/email/charset.py
This module is part of the legacy (Compat32) email API. In the newAPI only the aliases table is used.
The remaining text in this section is the original documentation of the module.
This module provides a classCharset for representing character setsand character set conversions in email messages, as well as a character setregistry and several convenience methods for manipulating this registry.Instances ofCharset are used in several other modules within theemail package.
Import this class from theemail.charset module.
- class
email.charset.Charset(input_charset=DEFAULT_CHARSET)¶ Map character sets to their email properties.
This class provides information about the requirements imposed on email for aspecific character set. It also provides convenience routines for convertingbetween character sets, given the availability of the applicable codecs. Givena character set, it will do its best to provide information on how to use thatcharacter set in an email message in an RFC-compliant way.
Certain character sets must be encoded with quoted-printable or base64 when usedin email headers or bodies. Certain character sets must be converted outright,and are not allowed in email.
Optionalinput_charset is as described below; it is always coerced to lowercase. After being alias normalized it is also used as a lookup into theregistry of character sets to find out the header encoding, body encoding, andoutput conversion codec to be used for the character set. For example, ifinput_charset is
iso-8859-1, then headers and bodies will be encoded usingquoted-printable and no output conversion codec is necessary. Ifinput_charset iseuc-jp, then headers will be encoded with base64, bodieswill not be encoded, but output text will be converted from theeuc-jpcharacter set to theiso-2022-jpcharacter set.Charsetinstances have the following data attributes:input_charset¶The initial character set specified. Common aliases are converted totheirofficial email names (e.g.
latin_1is converted toiso-8859-1). Defaults to 7-bitus-ascii.
header_encoding¶If the character set must be encoded before it can be used in an emailheader, this attribute will be set to
charset.QP(forquoted-printable),charset.BASE64(for base64 encoding), orcharset.SHORTESTfor the shortest of QP or BASE64 encoding. Otherwise,it will beNone.
body_encoding¶Same asheader_encoding, but describes the encoding for the mailmessage’s body, which indeed may be different than the header encoding.
charset.SHORTESTis not allowed forbody_encoding.
output_charset¶Some character sets must be converted before they can be used in emailheaders or bodies. If theinput_charset is one of them, this attributewill contain the name of the character set output will be converted to.Otherwise, it will be
None.
input_codec¶The name of the Python codec used to convert theinput_charset toUnicode. If no conversion codec is necessary, this attribute will be
None.
output_codec¶The name of the Python codec used to convert Unicode to theoutput_charset. If no conversion codec is necessary, this attributewill have the same value as theinput_codec.
Charsetinstances also have the following methods:get_body_encoding()¶Return the content transfer encoding used for body encoding.
This is either the string
quoted-printableorbase64depending onthe encoding used, or it is a function, in which case you should call thefunction with a single argument, the Message object being encoded. Thefunction should then set theContent-Transfer-Encodingheader itself to whatever is appropriate.Returns the string
quoted-printableifbody_encoding isQP,returns the stringbase64ifbody_encoding isBASE64, andreturns the string7bitotherwise.
get_output_charset()¶Return the output character set.
This is theoutput_charset attribute if that is not
None, otherwiseit isinput_charset.
header_encode(string)¶Header-encode the stringstring.
The type of encoding (base64 or quoted-printable) will be based on theheader_encoding attribute.
header_encode_lines(string,maxlengths)¶Header-encode astring by converting it first to bytes.
This is similar to
header_encode()except that the string is fitinto maximum line lengths as given by the argumentmaxlengths, whichmust be an iterator: each element returned from this iterator will providethe next maximum line length.
body_encode(string)¶Body-encode the stringstring.
The type of encoding (base64 or quoted-printable) will be based on thebody_encoding attribute.
The
Charsetclass also provides a number of methods to supportstandard operations and built-in functions.__str__()¶Returnsinput_charset as a string coerced to lowercase.
__repr__()is an alias for__str__().
Theemail.charset module also provides the following functions for addingnew entries to the global character set, alias, and codec registries:
email.charset.add_charset(charset,header_enc=None,body_enc=None,output_charset=None)¶Add character properties to the global registry.
charset is the input character set, and must be the canonical name of acharacter set.
Optionalheader_enc andbody_enc is either
charset.QPforquoted-printable,charset.BASE64for base64 encoding,charset.SHORTESTfor the shortest of quoted-printable or base64 encoding,orNonefor no encoding.SHORTESTis only valid forheader_enc. The default isNonefor no encoding.Optionaloutput_charset is the character set that the output should be in.Conversions will proceed from input charset, to Unicode, to the output charsetwhen the method
Charset.convert()is called. The default is to output inthe same character set as the input.Bothinput_charset andoutput_charset must have Unicode codec entries in themodule’s character set-to-codec mapping; use
add_codec()to add codecs themodule does not know about. See thecodecsmodule’s documentation formore information.The global character set registry is kept in the module global dictionary
CHARSETS.
email.charset.add_alias(alias,canonical)¶Add a character set alias.alias is the alias name, e.g.
latin-1.canonical is the character set’s canonical name, e.g.iso-8859-1.The global charset alias registry is kept in the module global dictionary
ALIASES.