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.

classemail.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 isiso-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-jp character set.

Charset instances have the following data attributes:

input_charset

The initial character set specified. Common aliases are converted totheirofficial email names (e.g.latin_1 is 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 tocharset.QP (forquoted-printable),charset.BASE64 (for base64 encoding), orcharset.SHORTEST for 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.SHORTEST is 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 beNone.

input_codec

The name of the Python codec used to convert theinput_charset toUnicode. If no conversion codec is necessary, this attribute will beNone.

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.

Charset instances also have the following methods:

get_body_encoding()

Return the content transfer encoding used for body encoding.

This is either the stringquoted-printable orbase64 depending 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 stringquoted-printable ifbody_encoding isQP,returns the stringbase64 ifbody_encoding isBASE64, andreturns the string7bit otherwise.

get_output_charset()

Return the output character set.

This is theoutput_charset attribute if that is notNone, 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 toheader_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.

TheCharset class 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__().

__eq__(other)

This method allows you to compare twoCharset instances forequality.

__ne__(other)

This method allows you to compare twoCharset instances forinequality.

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 eithercharset.QP forquoted-printable,charset.BASE64 for base64 encoding,charset.SHORTEST for the shortest of quoted-printable or base64 encoding,orNone for no encoding.SHORTEST is only valid forheader_enc. The default isNone for 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 methodCharset.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; useadd_codec() to add codecs themodule does not know about. See thecodecs module’s documentation formore information.

The global character set registry is kept in the module global dictionaryCHARSETS.

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 dictionaryALIASES.

email.charset.add_codec(charset,codecname)

Add a codec that map characters in the given character set to and from Unicode.

charset is the canonical name of a character set.codecname is the name of aPython codec, as appropriate for the second argument to thestr’sencode() method.