Encode a single header line with quoted-printable (like) encoding. Defined in RFC 2045, this 'Q' encoding is similar to quoted-printable, but used specifically for email header fields to allow charsets with mostly 7 bit characters (and some 8 bit) to remain more or less readable in non-
(header_bytes, charset='iso-8859-1')
| 125 | |
| 126 | |
| 127 | def header_encode(header_bytes, charset='iso-8859-1'): |
| 128 | """Encode a single header line with quoted-printable (like) encoding. |
| 129 | |
| 130 | Defined in RFC 2045, this 'Q' encoding is similar to quoted-printable, but |
| 131 | used specifically for email header fields to allow charsets with mostly 7 |
| 132 | bit characters (and some 8 bit) to remain more or less readable in non-RFC |
| 133 | 2045 aware mail clients. |
| 134 | |
| 135 | charset names the character set to use in the RFC 2046 header. It |
| 136 | defaults to iso-8859-1. |
| 137 | """ |
| 138 | # Return empty headers as an empty string. |
| 139 | if not header_bytes: |
| 140 | return '' |
| 141 | # Iterate over every byte, encoding if necessary. |
| 142 | encoded = header_bytes.decode('latin1').translate(_QUOPRI_HEADER_MAP) |
| 143 | # Now add the RFC chrome to each encoded chunk and glue the chunks |
| 144 | # together. |
| 145 | return '=?%s?q?%s?=' % (charset, encoded) |
| 146 | |
| 147 | |
| 148 | _QUOPRI_BODY_ENCODE_MAP = _QUOPRI_BODY_MAP[:] |