Body-encode a string by converting it first to bytes. The type of encoding (base64 or quoted-printable) will be based on self.body_encoding. If body_encoding is None, we assume the output charset is a 7bit encoding, so re-encoding the decoded string using the ascii
(self, string)
| 367 | return None |
| 368 | |
| 369 | def body_encode(self, string): |
| 370 | """Body-encode a string by converting it first to bytes. |
| 371 | |
| 372 | The type of encoding (base64 or quoted-printable) will be based on |
| 373 | self.body_encoding. If body_encoding is None, we assume the |
| 374 | output charset is a 7bit encoding, so re-encoding the decoded |
| 375 | string using the ascii codec produces the correct string version |
| 376 | of the content. |
| 377 | """ |
| 378 | if not string: |
| 379 | return string |
| 380 | if self.body_encoding is BASE64: |
| 381 | if isinstance(string, str): |
| 382 | string = string.encode(self.output_charset) |
| 383 | return email.base64mime.body_encode(string) |
| 384 | elif self.body_encoding is QP: |
| 385 | # quopromime.body_encode takes a string, but operates on it as if |
| 386 | # it were a list of byte codes. For a (minimal) history on why |
| 387 | # this is so, see changeset 0cf700464177. To correctly encode a |
| 388 | # character set, then, we must turn it into pseudo bytes via the |
| 389 | # latin1 charset, which will encode any byte as a single code point |
| 390 | # between 0 and 255, which is what body_encode is expecting. |
| 391 | if isinstance(string, str): |
| 392 | string = string.encode(self.output_charset) |
| 393 | string = string.decode('latin1') |
| 394 | return email.quoprimime.body_encode(string) |
| 395 | else: |
| 396 | if isinstance(string, str): |
| 397 | string = string.encode(self.output_charset).decode('ascii') |
| 398 | return string |