2002-04-10 18:01:31 -03:00
|
|
|
|
# Copyright (C) 2001,2002 Python Software Foundation
|
2002-09-28 14:47:56 -03:00
|
|
|
|
# Author: che@debian.org (Ben Gertzfield), barry@zope.com (Barry Warsaw)
|
2002-08-23 15:19:30 -03:00
|
|
|
|
|
2002-09-28 14:47:56 -03:00
|
|
|
|
from types import UnicodeType
|
2002-04-10 18:01:31 -03:00
|
|
|
|
from email.Encoders import encode_7or8bit
|
|
|
|
|
import email.base64MIME
|
|
|
|
|
import email.quopriMIME
|
|
|
|
|
|
2002-09-28 14:47:56 -03:00
|
|
|
|
def _isunicode(s):
|
|
|
|
|
return isinstance(s, UnicodeType)
|
|
|
|
|
|
|
|
|
|
# Python 2.2.1 and beyond has these symbols
|
|
|
|
|
try:
|
|
|
|
|
True, False
|
|
|
|
|
except NameError:
|
|
|
|
|
True = 1
|
|
|
|
|
False = 0
|
|
|
|
|
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Flags for types of header encodings
|
2002-09-28 14:47:56 -03:00
|
|
|
|
QP = 1 # Quoted-Printable
|
|
|
|
|
BASE64 = 2 # Base64
|
|
|
|
|
SHORTEST = 3 # the shorter of QP and base64, but only for headers
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
# In "=?charset?q?hello_world?=", the =?, ?q?, and ?= add up to 7
|
2002-05-23 12:15:30 -03:00
|
|
|
|
MISC_LEN = 7
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
DEFAULT_CHARSET = 'us-ascii'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Defaults
|
|
|
|
|
CHARSETS = {
|
|
|
|
|
# input header enc body enc output conv
|
2002-05-23 12:15:30 -03:00
|
|
|
|
'iso-8859-1': (QP, QP, None),
|
2002-04-10 18:01:31 -03:00
|
|
|
|
'iso-8859-2': (QP, QP, None),
|
2003-01-06 20:29:07 -04:00
|
|
|
|
'iso-8859-3': (QP, QP, None),
|
|
|
|
|
'iso-8859-4': (QP, QP, None),
|
|
|
|
|
# iso-8859-5 is Cyrillic, and not especially used
|
|
|
|
|
# iso-8859-6 is Arabic, also not particularly used
|
|
|
|
|
# iso-8859-7 is Greek, QP will not make it readable
|
|
|
|
|
# iso-8859-8 is Hebrew, QP will not make it readable
|
|
|
|
|
'iso-8859-9': (QP, QP, None),
|
|
|
|
|
'iso-8859-10': (QP, QP, None),
|
|
|
|
|
# iso-8859-11 is Thai, QP will not make it readable
|
|
|
|
|
'iso-8859-13': (QP, QP, None),
|
|
|
|
|
'iso-8859-14': (QP, QP, None),
|
|
|
|
|
'iso-8859-15': (QP, QP, None),
|
|
|
|
|
'windows-1252':(QP, QP, None),
|
|
|
|
|
'viscii': (QP, QP, None),
|
2002-04-10 18:01:31 -03:00
|
|
|
|
'us-ascii': (None, None, None),
|
|
|
|
|
'big5': (BASE64, BASE64, None),
|
2002-05-23 12:15:30 -03:00
|
|
|
|
'gb2312': (BASE64, BASE64, None),
|
2002-04-10 18:01:31 -03:00
|
|
|
|
'euc-jp': (BASE64, None, 'iso-2022-jp'),
|
|
|
|
|
'shift_jis': (BASE64, None, 'iso-2022-jp'),
|
|
|
|
|
'iso-2022-jp': (BASE64, None, None),
|
|
|
|
|
'koi8-r': (BASE64, BASE64, None),
|
2002-09-28 14:47:56 -03:00
|
|
|
|
'utf-8': (SHORTEST, BASE64, 'utf-8'),
|
2002-10-14 12:06:55 -03:00
|
|
|
|
# We're making this one up to represent raw unencoded 8-bit
|
|
|
|
|
'8bit': (None, BASE64, 'utf-8'),
|
2002-04-10 18:01:31 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Aliases for other commonly-used names for character sets. Map
|
|
|
|
|
# them to the real ones used in email.
|
|
|
|
|
ALIASES = {
|
|
|
|
|
'latin_1': 'iso-8859-1',
|
|
|
|
|
'latin-1': 'iso-8859-1',
|
2003-01-06 20:29:07 -04:00
|
|
|
|
'latin_2': 'iso-8859-2',
|
|
|
|
|
'latin-2': 'iso-8859-2',
|
|
|
|
|
'latin_3': 'iso-8859-3',
|
|
|
|
|
'latin-3': 'iso-8859-3',
|
|
|
|
|
'latin_4': 'iso-8859-4',
|
|
|
|
|
'latin-4': 'iso-8859-4',
|
|
|
|
|
'latin_5': 'iso-8859-9',
|
|
|
|
|
'latin-5': 'iso-8859-9',
|
|
|
|
|
'latin_6': 'iso-8859-10',
|
|
|
|
|
'latin-6': 'iso-8859-10',
|
|
|
|
|
'latin_7': 'iso-8859-13',
|
|
|
|
|
'latin-7': 'iso-8859-13',
|
|
|
|
|
'latin_8': 'iso-8859-14',
|
|
|
|
|
'latin-8': 'iso-8859-14',
|
|
|
|
|
'latin_9': 'iso-8859-15',
|
|
|
|
|
'latin-9': 'iso-8859-15',
|
|
|
|
|
'cp949': 'ks_c_5601-1987',
|
|
|
|
|
'euc_jp': 'euc-jp',
|
|
|
|
|
'euc_kr': 'euc-kr',
|
2002-04-10 18:01:31 -03:00
|
|
|
|
'ascii': 'us-ascii',
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-13 01:00:45 -03:00
|
|
|
|
# Map charsets to their Unicode codec strings. Note that Python doesn't come
|
|
|
|
|
# with any Asian codecs by default. Here's where to get them:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
#
|
2002-10-13 01:00:45 -03:00
|
|
|
|
# Japanese -- http://www.asahi-net.or.jp/~rd6t-kjym/python
|
|
|
|
|
# Korean -- http://sf.net/projects/koco
|
|
|
|
|
# Chinese -- http://sf.net/projects/python-codecs
|
2002-04-10 18:01:31 -03:00
|
|
|
|
#
|
2002-10-13 01:00:45 -03:00
|
|
|
|
# Note that these codecs have their own lifecycle and may be in varying states
|
|
|
|
|
# of stability and useability.
|
|
|
|
|
|
2002-04-10 18:01:31 -03:00
|
|
|
|
CODEC_MAP = {
|
|
|
|
|
'euc-jp': 'japanese.euc-jp',
|
|
|
|
|
'iso-2022-jp': 'japanese.iso-2022-jp',
|
|
|
|
|
'shift_jis': 'japanese.shift_jis',
|
2003-01-06 20:29:07 -04:00
|
|
|
|
'euc-kr': 'korean.euc-kr',
|
|
|
|
|
'ks_c_5601-1987': 'korean.cp949',
|
|
|
|
|
'iso-2022-kr': 'korean.iso-2022-kr',
|
|
|
|
|
'johab': 'korean.johab',
|
2002-04-10 18:01:31 -03:00
|
|
|
|
'gb2132': 'eucgb2312_cn',
|
|
|
|
|
'big5': 'big5_tw',
|
|
|
|
|
'utf-8': 'utf-8',
|
|
|
|
|
# Hack: We don't want *any* conversion for stuff marked us-ascii, as all
|
|
|
|
|
# sorts of garbage might be sent to us in the guise of 7-bit us-ascii.
|
|
|
|
|
# Let that stuff pass through without conversion to/from Unicode.
|
|
|
|
|
'us-ascii': None,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Convenience functions for extending the above mappings
|
|
|
|
|
def add_charset(charset, header_enc=None, body_enc=None, output_charset=None):
|
2002-09-30 21:05:24 -03:00
|
|
|
|
"""Add character set properties to the global registry.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
charset is the input character set, and must be the canonical name of a
|
|
|
|
|
character set.
|
|
|
|
|
|
|
|
|
|
Optional header_enc and body_enc is either Charset.QP for
|
2002-09-28 14:47:56 -03:00
|
|
|
|
quoted-printable, Charset.BASE64 for base64 encoding, Charset.SHORTEST for
|
|
|
|
|
the shortest of qp or base64 encoding, or None for no encoding. SHORTEST
|
|
|
|
|
is only valid for header_enc. It describes how message headers and
|
|
|
|
|
message bodies in the input charset are to be encoded. Default is no
|
|
|
|
|
encoding.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
Optional output_charset is the character set that the output should be
|
|
|
|
|
in. Conversions will proceed from input charset, to Unicode, to the
|
|
|
|
|
output charset when the method Charset.convert() is called. The default
|
|
|
|
|
is to output in the same character set as the input.
|
|
|
|
|
|
|
|
|
|
Both input_charset and output_charset must have Unicode codec entries in
|
|
|
|
|
the module's charset-to-codec mapping; use add_codec(charset, codecname)
|
2002-09-30 21:05:24 -03:00
|
|
|
|
to add codecs the module does not know about. See the codecs module's
|
2002-04-10 18:01:31 -03:00
|
|
|
|
documentation for more information.
|
|
|
|
|
"""
|
2002-09-28 14:47:56 -03:00
|
|
|
|
if body_enc == SHORTEST:
|
|
|
|
|
raise ValueError, 'SHORTEST not allowed for body_enc'
|
2002-04-10 18:01:31 -03:00
|
|
|
|
CHARSETS[charset] = (header_enc, body_enc, output_charset)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def 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
|
|
|
|
|
"""
|
|
|
|
|
ALIASES[alias] = canonical
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_codec(charset, codecname):
|
|
|
|
|
"""Add a codec that map characters in the given charset to/from Unicode.
|
|
|
|
|
|
|
|
|
|
charset is the canonical name of a character set. codecname is the name
|
|
|
|
|
of a Python codec, as appropriate for the second argument to the unicode()
|
2002-09-30 21:05:24 -03:00
|
|
|
|
built-in, or to the encode() method of a Unicode string.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""
|
|
|
|
|
CODEC_MAP[charset] = codecname
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Charset:
|
|
|
|
|
"""Map character sets to their email properties.
|
|
|
|
|
|
|
|
|
|
This class provides information about the requirements imposed on email
|
|
|
|
|
for a specific character set. It also provides convenience routines for
|
|
|
|
|
converting between character sets, given the availability of the
|
2002-09-30 21:05:24 -03:00
|
|
|
|
applicable codecs. Given a character set, it will do its best to provide
|
|
|
|
|
information on how to use that character set in an email in an
|
|
|
|
|
RFC-compliant way.
|
2002-05-23 12:15:30 -03:00
|
|
|
|
|
2002-04-10 18:01:31 -03:00
|
|
|
|
Certain character sets must be encoded with quoted-printable or base64
|
|
|
|
|
when used in email headers or bodies. Certain character sets must be
|
|
|
|
|
converted outright, and are not allowed in email. Instances of this
|
|
|
|
|
module expose the following information about a character set:
|
|
|
|
|
|
|
|
|
|
input_charset: The initial character set specified. Common aliases
|
|
|
|
|
are converted to their `official' email names (e.g. latin_1
|
|
|
|
|
is converted to iso-8859-1). Defaults to 7-bit us-ascii.
|
|
|
|
|
|
|
|
|
|
header_encoding: If the character set must be encoded before it can be
|
|
|
|
|
used in an email header, this attribute will be set to
|
2002-09-28 14:47:56 -03:00
|
|
|
|
Charset.QP (for quoted-printable), Charset.BASE64 (for
|
|
|
|
|
base64 encoding), or Charset.SHORTEST for the shortest of
|
|
|
|
|
QP or BASE64 encoding. Otherwise, it will be None.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
body_encoding: Same as header_encoding, but describes the encoding for the
|
|
|
|
|
mail message's body, which indeed may be different than the
|
2002-09-28 14:47:56 -03:00
|
|
|
|
header encoding. Charset.SHORTEST is not allowed for
|
|
|
|
|
body_encoding.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
output_charset: Some character sets must be converted before the can be
|
|
|
|
|
used in email headers or bodies. If the input_charset is
|
|
|
|
|
one of them, this attribute will contain the name of the
|
|
|
|
|
charset output will be converted to. Otherwise, it will
|
|
|
|
|
be None.
|
|
|
|
|
|
|
|
|
|
input_codec: The name of the Python codec used to convert the
|
|
|
|
|
input_charset to Unicode. If no conversion codec is
|
|
|
|
|
necessary, this attribute will be None.
|
|
|
|
|
|
|
|
|
|
output_codec: The name of the Python codec used to convert Unicode
|
|
|
|
|
to the output_charset. If no conversion codec is necessary,
|
|
|
|
|
this attribute will have the same value as the input_codec.
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self, input_charset=DEFAULT_CHARSET):
|
2002-10-10 12:11:20 -03:00
|
|
|
|
# RFC 2046, $4.1.2 says charsets are not case sensitive
|
|
|
|
|
input_charset = input_charset.lower()
|
2002-04-10 18:01:31 -03:00
|
|
|
|
# Set the input charset after filtering through the aliases
|
|
|
|
|
self.input_charset = ALIASES.get(input_charset, input_charset)
|
|
|
|
|
# We can try to guess which encoding and conversion to use by the
|
|
|
|
|
# charset_map dictionary. Try that first, but let the user override
|
|
|
|
|
# it.
|
|
|
|
|
henc, benc, conv = CHARSETS.get(self.input_charset,
|
2002-10-10 12:11:20 -03:00
|
|
|
|
(SHORTEST, BASE64, None))
|
2002-04-10 18:01:31 -03:00
|
|
|
|
# Set the attributes, allowing the arguments to override the default.
|
|
|
|
|
self.header_encoding = henc
|
|
|
|
|
self.body_encoding = benc
|
|
|
|
|
self.output_charset = ALIASES.get(conv, conv)
|
|
|
|
|
# Now set the codecs. If one isn't defined for input_charset,
|
|
|
|
|
# guess and try a Unicode codec with the same name as input_codec.
|
|
|
|
|
self.input_codec = CODEC_MAP.get(self.input_charset,
|
|
|
|
|
self.input_charset)
|
|
|
|
|
self.output_codec = CODEC_MAP.get(self.output_charset,
|
|
|
|
|
self.input_codec)
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.input_charset.lower()
|
|
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
|
return str(self) == str(other).lower()
|
|
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
|
|
def get_body_encoding(self):
|
|
|
|
|
"""Return the content-transfer-encoding used for body encoding.
|
|
|
|
|
|
|
|
|
|
This is either the string `quoted-printable' or `base64' depending on
|
|
|
|
|
the encoding used, or it is a function in which case you should call
|
|
|
|
|
the function with a single argument, the Message object being
|
2002-09-30 21:05:24 -03:00
|
|
|
|
encoded. The function should then set the Content-Transfer-Encoding
|
2002-04-10 18:01:31 -03:00
|
|
|
|
header itself to whatever is appropriate.
|
|
|
|
|
|
|
|
|
|
Returns "quoted-printable" if self.body_encoding is QP.
|
|
|
|
|
Returns "base64" if self.body_encoding is BASE64.
|
|
|
|
|
Returns "7bit" otherwise.
|
|
|
|
|
"""
|
2002-09-28 14:47:56 -03:00
|
|
|
|
assert self.body_encoding <> SHORTEST
|
2002-04-10 18:01:31 -03:00
|
|
|
|
if self.body_encoding == QP:
|
|
|
|
|
return 'quoted-printable'
|
|
|
|
|
elif self.body_encoding == BASE64:
|
|
|
|
|
return 'base64'
|
|
|
|
|
else:
|
|
|
|
|
return encode_7or8bit
|
|
|
|
|
|
|
|
|
|
def convert(self, s):
|
|
|
|
|
"""Convert a string from the input_codec to the output_codec."""
|
|
|
|
|
if self.input_codec <> self.output_codec:
|
|
|
|
|
return unicode(s, self.input_codec).encode(self.output_codec)
|
|
|
|
|
else:
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
def to_splittable(self, s):
|
|
|
|
|
"""Convert a possibly multibyte string to a safely splittable format.
|
|
|
|
|
|
|
|
|
|
Uses the input_codec to try and convert the string to Unicode, so it
|
2002-09-30 21:05:24 -03:00
|
|
|
|
can be safely split on character boundaries (even for multibyte
|
2002-04-10 18:01:31 -03:00
|
|
|
|
characters).
|
|
|
|
|
|
2002-09-30 21:05:24 -03:00
|
|
|
|
Returns the string as-is if it isn't known how to convert it to
|
2002-04-10 18:01:31 -03:00
|
|
|
|
Unicode with the input_charset.
|
|
|
|
|
|
|
|
|
|
Characters that could not be converted to Unicode will be replaced
|
|
|
|
|
with the Unicode replacement character U+FFFD.
|
|
|
|
|
"""
|
2002-09-28 14:47:56 -03:00
|
|
|
|
if _isunicode(s) or self.input_codec is None:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
return s
|
|
|
|
|
try:
|
|
|
|
|
return unicode(s, self.input_codec, 'replace')
|
|
|
|
|
except LookupError:
|
|
|
|
|
# Input codec not installed on system, so return the original
|
|
|
|
|
# string unchanged.
|
|
|
|
|
return s
|
|
|
|
|
|
2002-09-28 14:47:56 -03:00
|
|
|
|
def from_splittable(self, ustr, to_output=True):
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""Convert a splittable string back into an encoded string.
|
|
|
|
|
|
2002-09-30 21:05:24 -03:00
|
|
|
|
Uses the proper codec to try and convert the string from Unicode back
|
|
|
|
|
into an encoded format. Return the string as-is if it is not Unicode,
|
|
|
|
|
or if it could not be converted from Unicode.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
Characters that could not be converted from Unicode will be replaced
|
|
|
|
|
with an appropriate character (usually '?').
|
|
|
|
|
|
2002-09-28 14:47:56 -03:00
|
|
|
|
If to_output is True (the default), uses output_codec to convert to an
|
|
|
|
|
encoded format. If to_output is False, uses input_codec.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""
|
|
|
|
|
if to_output:
|
|
|
|
|
codec = self.output_codec
|
|
|
|
|
else:
|
|
|
|
|
codec = self.input_codec
|
2002-09-28 14:47:56 -03:00
|
|
|
|
if not _isunicode(ustr) or codec is None:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
return ustr
|
|
|
|
|
try:
|
|
|
|
|
return ustr.encode(codec, 'replace')
|
|
|
|
|
except LookupError:
|
|
|
|
|
# Output codec not installed
|
|
|
|
|
return ustr
|
|
|
|
|
|
|
|
|
|
def get_output_charset(self):
|
|
|
|
|
"""Return the output character set.
|
|
|
|
|
|
2002-09-30 21:05:24 -03:00
|
|
|
|
This is self.output_charset if that is not None, otherwise it is
|
2002-04-10 18:01:31 -03:00
|
|
|
|
self.input_charset.
|
|
|
|
|
"""
|
|
|
|
|
return self.output_charset or self.input_charset
|
|
|
|
|
|
|
|
|
|
def encoded_header_len(self, s):
|
|
|
|
|
"""Return the length of the encoded header string."""
|
|
|
|
|
cset = self.get_output_charset()
|
|
|
|
|
# The len(s) of a 7bit encoding is len(s)
|
2002-09-28 14:47:56 -03:00
|
|
|
|
if self.header_encoding == BASE64:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
return email.base64MIME.base64_len(s) + len(cset) + MISC_LEN
|
2002-09-28 14:47:56 -03:00
|
|
|
|
elif self.header_encoding == QP:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
return email.quopriMIME.header_quopri_len(s) + len(cset) + MISC_LEN
|
2002-09-28 14:47:56 -03:00
|
|
|
|
elif self.header_encoding == SHORTEST:
|
|
|
|
|
lenb64 = email.base64MIME.base64_len(s)
|
|
|
|
|
lenqp = email.quopriMIME.header_quopri_len(s)
|
|
|
|
|
return min(lenb64, lenqp) + len(cset) + MISC_LEN
|
2002-04-10 18:01:31 -03:00
|
|
|
|
else:
|
|
|
|
|
return len(s)
|
|
|
|
|
|
2002-09-28 14:47:56 -03:00
|
|
|
|
def header_encode(self, s, convert=False):
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""Header-encode a string, optionally converting it to output_charset.
|
|
|
|
|
|
2002-09-28 14:47:56 -03:00
|
|
|
|
If convert is True, the string will be converted from the input
|
2002-04-10 18:01:31 -03:00
|
|
|
|
charset to the output charset automatically. This is not useful for
|
|
|
|
|
multibyte character sets, which have line length issues (multibyte
|
|
|
|
|
characters must be split on a character, not a byte boundary); use the
|
|
|
|
|
high-level Header class to deal with these issues. convert defaults
|
2002-09-28 14:47:56 -03:00
|
|
|
|
to False.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
The type of encoding (base64 or quoted-printable) will be based on
|
|
|
|
|
self.header_encoding.
|
|
|
|
|
"""
|
|
|
|
|
cset = self.get_output_charset()
|
|
|
|
|
if convert:
|
|
|
|
|
s = self.convert(s)
|
|
|
|
|
# 7bit/8bit encodings return the string unchanged (modulo conversions)
|
2002-09-28 14:47:56 -03:00
|
|
|
|
if self.header_encoding == BASE64:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
return email.base64MIME.header_encode(s, cset)
|
2002-09-28 14:47:56 -03:00
|
|
|
|
elif self.header_encoding == QP:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
return email.quopriMIME.header_encode(s, cset)
|
2002-09-28 14:47:56 -03:00
|
|
|
|
elif self.header_encoding == SHORTEST:
|
|
|
|
|
lenb64 = email.base64MIME.base64_len(s)
|
|
|
|
|
lenqp = email.quopriMIME.header_quopri_len(s)
|
|
|
|
|
if lenb64 < lenqp:
|
|
|
|
|
return email.base64MIME.header_encode(s, cset)
|
|
|
|
|
else:
|
|
|
|
|
return email.quopriMIME.header_encode(s, cset)
|
2002-04-10 18:01:31 -03:00
|
|
|
|
else:
|
|
|
|
|
return s
|
|
|
|
|
|
2002-09-28 14:47:56 -03:00
|
|
|
|
def body_encode(self, s, convert=True):
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""Body-encode a string and convert it to output_charset.
|
|
|
|
|
|
2002-09-28 14:47:56 -03:00
|
|
|
|
If convert is True (the default), the string will be converted from
|
2002-04-10 18:01:31 -03:00
|
|
|
|
the input charset to output charset automatically. Unlike
|
|
|
|
|
header_encode(), there are no issues with byte boundaries and
|
|
|
|
|
multibyte charsets in email bodies, so this is usually pretty safe.
|
|
|
|
|
|
|
|
|
|
The type of encoding (base64 or quoted-printable) will be based on
|
|
|
|
|
self.body_encoding.
|
|
|
|
|
"""
|
|
|
|
|
if convert:
|
|
|
|
|
s = self.convert(s)
|
|
|
|
|
# 7bit/8bit encodings return the string unchanged (module conversions)
|
|
|
|
|
if self.body_encoding is BASE64:
|
|
|
|
|
return email.base64MIME.body_encode(s)
|
2002-10-21 02:29:53 -03:00
|
|
|
|
elif self.body_encoding is QP:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
return email.quopriMIME.body_encode(s)
|
|
|
|
|
else:
|
|
|
|
|
return s
|