Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001,2002 Python Software Foundation |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 2 | # Author: che@debian.org (Ben Gertzfield), barry@zope.com (Barry Warsaw) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 3 | |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 4 | from types import UnicodeType |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 5 | from email.Encoders import encode_7or8bit |
| 6 | import email.base64MIME |
| 7 | import email.quopriMIME |
| 8 | |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 9 | def _isunicode(s): |
| 10 | return isinstance(s, UnicodeType) |
| 11 | |
| 12 | # Python 2.2.1 and beyond has these symbols |
| 13 | try: |
| 14 | True, False |
| 15 | except NameError: |
| 16 | True = 1 |
| 17 | False = 0 |
| 18 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | # Flags for types of header encodings |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 22 | QP = 1 # Quoted-Printable |
| 23 | BASE64 = 2 # Base64 |
| 24 | SHORTEST = 3 # the shorter of QP and base64, but only for headers |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 25 | |
| 26 | # In "=?charset?q?hello_world?=", the =?, ?q?, and ?= add up to 7 |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 27 | MISC_LEN = 7 |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 28 | |
| 29 | DEFAULT_CHARSET = 'us-ascii' |
| 30 | |
| 31 | |
| 32 | |
| 33 | # Defaults |
| 34 | CHARSETS = { |
| 35 | # input header enc body enc output conv |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 36 | 'iso-8859-1': (QP, QP, None), |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 37 | 'iso-8859-2': (QP, QP, None), |
| 38 | 'us-ascii': (None, None, None), |
| 39 | 'big5': (BASE64, BASE64, None), |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 40 | 'gb2312': (BASE64, BASE64, None), |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 41 | 'euc-jp': (BASE64, None, 'iso-2022-jp'), |
| 42 | 'shift_jis': (BASE64, None, 'iso-2022-jp'), |
| 43 | 'iso-2022-jp': (BASE64, None, None), |
| 44 | 'koi8-r': (BASE64, BASE64, None), |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 45 | 'utf-8': (SHORTEST, BASE64, 'utf-8'), |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | # Aliases for other commonly-used names for character sets. Map |
| 49 | # them to the real ones used in email. |
| 50 | ALIASES = { |
| 51 | 'latin_1': 'iso-8859-1', |
| 52 | 'latin-1': 'iso-8859-1', |
| 53 | 'ascii': 'us-ascii', |
| 54 | } |
| 55 | |
Barry Warsaw | ab9439f | 2002-10-13 04:00:45 +0000 | [diff] [blame] | 56 | # Map charsets to their Unicode codec strings. Note that Python doesn't come |
| 57 | # with any Asian codecs by default. Here's where to get them: |
| 58 | # |
| 59 | # Japanese -- http://www.asahi-net.or.jp/~rd6t-kjym/python |
| 60 | # Korean -- http://sf.net/projects/koco |
| 61 | # Chinese -- http://sf.net/projects/python-codecs |
| 62 | # |
| 63 | # Note that these codecs have their own lifecycle and may be in varying states |
| 64 | # of stability and useability. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 65 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 66 | CODEC_MAP = { |
| 67 | 'euc-jp': 'japanese.euc-jp', |
| 68 | 'iso-2022-jp': 'japanese.iso-2022-jp', |
| 69 | 'shift_jis': 'japanese.shift_jis', |
| 70 | 'gb2132': 'eucgb2312_cn', |
| 71 | 'big5': 'big5_tw', |
| 72 | 'utf-8': 'utf-8', |
| 73 | # Hack: We don't want *any* conversion for stuff marked us-ascii, as all |
| 74 | # sorts of garbage might be sent to us in the guise of 7-bit us-ascii. |
| 75 | # Let that stuff pass through without conversion to/from Unicode. |
| 76 | 'us-ascii': None, |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | # Convenience functions for extending the above mappings |
| 82 | def add_charset(charset, header_enc=None, body_enc=None, output_charset=None): |
Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 83 | """Add character set properties to the global registry. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 84 | |
| 85 | charset is the input character set, and must be the canonical name of a |
| 86 | character set. |
| 87 | |
| 88 | Optional header_enc and body_enc is either Charset.QP for |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 89 | quoted-printable, Charset.BASE64 for base64 encoding, Charset.SHORTEST for |
| 90 | the shortest of qp or base64 encoding, or None for no encoding. SHORTEST |
| 91 | is only valid for header_enc. It describes how message headers and |
| 92 | message bodies in the input charset are to be encoded. Default is no |
| 93 | encoding. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 94 | |
| 95 | Optional output_charset is the character set that the output should be |
| 96 | in. Conversions will proceed from input charset, to Unicode, to the |
| 97 | output charset when the method Charset.convert() is called. The default |
| 98 | is to output in the same character set as the input. |
| 99 | |
| 100 | Both input_charset and output_charset must have Unicode codec entries in |
| 101 | the module's charset-to-codec mapping; use add_codec(charset, codecname) |
Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 102 | to add codecs the module does not know about. See the codecs module's |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 103 | documentation for more information. |
| 104 | """ |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 105 | if body_enc == SHORTEST: |
| 106 | raise ValueError, 'SHORTEST not allowed for body_enc' |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 107 | CHARSETS[charset] = (header_enc, body_enc, output_charset) |
| 108 | |
| 109 | |
| 110 | def add_alias(alias, canonical): |
| 111 | """Add a character set alias. |
| 112 | |
| 113 | alias is the alias name, e.g. latin-1 |
| 114 | canonical is the character set's canonical name, e.g. iso-8859-1 |
| 115 | """ |
| 116 | ALIASES[alias] = canonical |
| 117 | |
| 118 | |
| 119 | def add_codec(charset, codecname): |
| 120 | """Add a codec that map characters in the given charset to/from Unicode. |
| 121 | |
| 122 | charset is the canonical name of a character set. codecname is the name |
| 123 | of a Python codec, as appropriate for the second argument to the unicode() |
Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 124 | built-in, or to the encode() method of a Unicode string. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 125 | """ |
| 126 | CODEC_MAP[charset] = codecname |
| 127 | |
| 128 | |
| 129 | |
| 130 | class Charset: |
| 131 | """Map character sets to their email properties. |
| 132 | |
| 133 | This class provides information about the requirements imposed on email |
| 134 | for a specific character set. It also provides convenience routines for |
| 135 | converting between character sets, given the availability of the |
Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 136 | applicable codecs. Given a character set, it will do its best to provide |
| 137 | information on how to use that character set in an email in an |
| 138 | RFC-compliant way. |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 139 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 140 | Certain character sets must be encoded with quoted-printable or base64 |
| 141 | when used in email headers or bodies. Certain character sets must be |
| 142 | converted outright, and are not allowed in email. Instances of this |
| 143 | module expose the following information about a character set: |
| 144 | |
| 145 | input_charset: The initial character set specified. Common aliases |
| 146 | are converted to their `official' email names (e.g. latin_1 |
| 147 | is converted to iso-8859-1). Defaults to 7-bit us-ascii. |
| 148 | |
| 149 | header_encoding: If the character set must be encoded before it can be |
| 150 | used in an email header, this attribute will be set to |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 151 | Charset.QP (for quoted-printable), Charset.BASE64 (for |
| 152 | base64 encoding), or Charset.SHORTEST for the shortest of |
| 153 | QP or BASE64 encoding. Otherwise, it will be None. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 154 | |
| 155 | body_encoding: Same as header_encoding, but describes the encoding for the |
| 156 | mail message's body, which indeed may be different than the |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 157 | header encoding. Charset.SHORTEST is not allowed for |
| 158 | body_encoding. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 159 | |
| 160 | output_charset: Some character sets must be converted before the can be |
| 161 | used in email headers or bodies. If the input_charset is |
| 162 | one of them, this attribute will contain the name of the |
| 163 | charset output will be converted to. Otherwise, it will |
| 164 | be None. |
| 165 | |
| 166 | input_codec: The name of the Python codec used to convert the |
| 167 | input_charset to Unicode. If no conversion codec is |
| 168 | necessary, this attribute will be None. |
| 169 | |
| 170 | output_codec: The name of the Python codec used to convert Unicode |
| 171 | to the output_charset. If no conversion codec is necessary, |
| 172 | this attribute will have the same value as the input_codec. |
| 173 | """ |
| 174 | def __init__(self, input_charset=DEFAULT_CHARSET): |
Barry Warsaw | 14fc464 | 2002-10-10 15:11:20 +0000 | [diff] [blame] | 175 | # RFC 2046, $4.1.2 says charsets are not case sensitive |
| 176 | input_charset = input_charset.lower() |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 177 | # Set the input charset after filtering through the aliases |
| 178 | self.input_charset = ALIASES.get(input_charset, input_charset) |
| 179 | # We can try to guess which encoding and conversion to use by the |
| 180 | # charset_map dictionary. Try that first, but let the user override |
| 181 | # it. |
| 182 | henc, benc, conv = CHARSETS.get(self.input_charset, |
Barry Warsaw | 14fc464 | 2002-10-10 15:11:20 +0000 | [diff] [blame] | 183 | (SHORTEST, BASE64, None)) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 184 | # Set the attributes, allowing the arguments to override the default. |
| 185 | self.header_encoding = henc |
| 186 | self.body_encoding = benc |
| 187 | self.output_charset = ALIASES.get(conv, conv) |
| 188 | # Now set the codecs. If one isn't defined for input_charset, |
| 189 | # guess and try a Unicode codec with the same name as input_codec. |
| 190 | self.input_codec = CODEC_MAP.get(self.input_charset, |
| 191 | self.input_charset) |
| 192 | self.output_codec = CODEC_MAP.get(self.output_charset, |
| 193 | self.input_codec) |
| 194 | |
| 195 | def __str__(self): |
| 196 | return self.input_charset.lower() |
| 197 | |
| 198 | def __eq__(self, other): |
| 199 | return str(self) == str(other).lower() |
| 200 | |
| 201 | def __ne__(self, other): |
| 202 | return not self.__eq__(other) |
| 203 | |
| 204 | def get_body_encoding(self): |
| 205 | """Return the content-transfer-encoding used for body encoding. |
| 206 | |
| 207 | This is either the string `quoted-printable' or `base64' depending on |
| 208 | the encoding used, or it is a function in which case you should call |
| 209 | the function with a single argument, the Message object being |
Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 210 | encoded. The function should then set the Content-Transfer-Encoding |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 211 | header itself to whatever is appropriate. |
| 212 | |
| 213 | Returns "quoted-printable" if self.body_encoding is QP. |
| 214 | Returns "base64" if self.body_encoding is BASE64. |
| 215 | Returns "7bit" otherwise. |
| 216 | """ |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 217 | assert self.body_encoding <> SHORTEST |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 218 | if self.body_encoding == QP: |
| 219 | return 'quoted-printable' |
| 220 | elif self.body_encoding == BASE64: |
| 221 | return 'base64' |
| 222 | else: |
| 223 | return encode_7or8bit |
| 224 | |
| 225 | def convert(self, s): |
| 226 | """Convert a string from the input_codec to the output_codec.""" |
| 227 | if self.input_codec <> self.output_codec: |
| 228 | return unicode(s, self.input_codec).encode(self.output_codec) |
| 229 | else: |
| 230 | return s |
| 231 | |
| 232 | def to_splittable(self, s): |
| 233 | """Convert a possibly multibyte string to a safely splittable format. |
| 234 | |
| 235 | Uses the input_codec to try and convert the string to Unicode, so it |
Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 236 | can be safely split on character boundaries (even for multibyte |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 237 | characters). |
| 238 | |
Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 239 | Returns the string as-is if it isn't known how to convert it to |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 240 | Unicode with the input_charset. |
| 241 | |
| 242 | Characters that could not be converted to Unicode will be replaced |
| 243 | with the Unicode replacement character U+FFFD. |
| 244 | """ |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 245 | if _isunicode(s) or self.input_codec is None: |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 246 | return s |
| 247 | try: |
| 248 | return unicode(s, self.input_codec, 'replace') |
| 249 | except LookupError: |
| 250 | # Input codec not installed on system, so return the original |
| 251 | # string unchanged. |
| 252 | return s |
| 253 | |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 254 | def from_splittable(self, ustr, to_output=True): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 255 | """Convert a splittable string back into an encoded string. |
| 256 | |
Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 257 | Uses the proper codec to try and convert the string from Unicode back |
| 258 | into an encoded format. Return the string as-is if it is not Unicode, |
| 259 | or if it could not be converted from Unicode. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 260 | |
| 261 | Characters that could not be converted from Unicode will be replaced |
| 262 | with an appropriate character (usually '?'). |
| 263 | |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 264 | If to_output is True (the default), uses output_codec to convert to an |
| 265 | encoded format. If to_output is False, uses input_codec. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 266 | """ |
| 267 | if to_output: |
| 268 | codec = self.output_codec |
| 269 | else: |
| 270 | codec = self.input_codec |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 271 | if not _isunicode(ustr) or codec is None: |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 272 | return ustr |
| 273 | try: |
| 274 | return ustr.encode(codec, 'replace') |
| 275 | except LookupError: |
| 276 | # Output codec not installed |
| 277 | return ustr |
| 278 | |
| 279 | def get_output_charset(self): |
| 280 | """Return the output character set. |
| 281 | |
Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 282 | This is self.output_charset if that is not None, otherwise it is |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 283 | self.input_charset. |
| 284 | """ |
| 285 | return self.output_charset or self.input_charset |
| 286 | |
| 287 | def encoded_header_len(self, s): |
| 288 | """Return the length of the encoded header string.""" |
| 289 | cset = self.get_output_charset() |
| 290 | # The len(s) of a 7bit encoding is len(s) |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 291 | if self.header_encoding == BASE64: |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 292 | return email.base64MIME.base64_len(s) + len(cset) + MISC_LEN |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 293 | elif self.header_encoding == QP: |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 294 | return email.quopriMIME.header_quopri_len(s) + len(cset) + MISC_LEN |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 295 | elif self.header_encoding == SHORTEST: |
| 296 | lenb64 = email.base64MIME.base64_len(s) |
| 297 | lenqp = email.quopriMIME.header_quopri_len(s) |
| 298 | return min(lenb64, lenqp) + len(cset) + MISC_LEN |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 299 | else: |
| 300 | return len(s) |
| 301 | |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 302 | def header_encode(self, s, convert=False): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 303 | """Header-encode a string, optionally converting it to output_charset. |
| 304 | |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 305 | If convert is True, the string will be converted from the input |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 306 | charset to the output charset automatically. This is not useful for |
| 307 | multibyte character sets, which have line length issues (multibyte |
| 308 | characters must be split on a character, not a byte boundary); use the |
| 309 | high-level Header class to deal with these issues. convert defaults |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 310 | to False. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 311 | |
| 312 | The type of encoding (base64 or quoted-printable) will be based on |
| 313 | self.header_encoding. |
| 314 | """ |
| 315 | cset = self.get_output_charset() |
| 316 | if convert: |
| 317 | s = self.convert(s) |
| 318 | # 7bit/8bit encodings return the string unchanged (modulo conversions) |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 319 | if self.header_encoding == BASE64: |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 320 | return email.base64MIME.header_encode(s, cset) |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 321 | elif self.header_encoding == QP: |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 322 | return email.quopriMIME.header_encode(s, cset) |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 323 | elif self.header_encoding == SHORTEST: |
| 324 | lenb64 = email.base64MIME.base64_len(s) |
| 325 | lenqp = email.quopriMIME.header_quopri_len(s) |
| 326 | if lenb64 < lenqp: |
| 327 | return email.base64MIME.header_encode(s, cset) |
| 328 | else: |
| 329 | return email.quopriMIME.header_encode(s, cset) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 330 | else: |
| 331 | return s |
| 332 | |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 333 | def body_encode(self, s, convert=True): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 334 | """Body-encode a string and convert it to output_charset. |
| 335 | |
Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 336 | If convert is True (the default), the string will be converted from |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 337 | the input charset to output charset automatically. Unlike |
| 338 | header_encode(), there are no issues with byte boundaries and |
| 339 | multibyte charsets in email bodies, so this is usually pretty safe. |
| 340 | |
| 341 | The type of encoding (base64 or quoted-printable) will be based on |
| 342 | self.body_encoding. |
| 343 | """ |
| 344 | if convert: |
| 345 | s = self.convert(s) |
| 346 | # 7bit/8bit encodings return the string unchanged (module conversions) |
| 347 | if self.body_encoding is BASE64: |
| 348 | return email.base64MIME.body_encode(s) |
| 349 | elif self.header_encoding is QP: |
| 350 | return email.quopriMIME.body_encode(s) |
| 351 | else: |
| 352 | return s |