1 # Copyright (C) 2001-2006 Python Software Foundation
3 # Contact: email-sig@python.org
5 """Encodings and related functions."""
16 from quopri
import encodestring
as _encodestring
21 enc
= _encodestring(s
, quotetabs
=True)
22 # Must encode spaces, which quopri.encodestring() doesn't do
23 return enc
.replace(' ', '=20')
27 # We can't quite use base64.encodestring() since it tacks on a "courtesy
31 hasnewline
= (s
[-1] == '\n')
32 value
= base64
.encodestring(s
)
33 if not hasnewline
and value
[-1] == '\n':
39 def encode_base64(msg
):
40 """Encode the message's payload in Base64.
42 Also, add an appropriate Content-Transfer-Encoding header.
44 orig
= msg
.get_payload()
45 encdata
= _bencode(orig
)
46 msg
.set_payload(encdata
)
47 msg
['Content-Transfer-Encoding'] = 'base64'
51 def encode_quopri(msg
):
52 """Encode the message's payload in quoted-printable.
54 Also, add an appropriate Content-Transfer-Encoding header.
56 orig
= msg
.get_payload()
57 encdata
= _qencode(orig
)
58 msg
.set_payload(encdata
)
59 msg
['Content-Transfer-Encoding'] = 'quoted-printable'
63 def encode_7or8bit(msg
):
64 """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
65 orig
= msg
.get_payload()
67 # There's no payload. For backwards compatibility we use 7bit
68 msg
['Content-Transfer-Encoding'] = '7bit'
70 # We play a trick to make this go fast. If encoding to ASCII succeeds, we
71 # know the data must be 7bit, otherwise treat it as 8bit.
75 # iso-2022-* is non-ASCII but still 7-bit
76 charset
= msg
.get_charset()
77 output_cset
= charset
and charset
.output_charset
78 if output_cset
and output_cset
.lower().startswith('iso-2202-'):
79 msg
['Content-Transfer-Encoding'] = '7bit'
81 msg
['Content-Transfer-Encoding'] = '8bit'
83 msg
['Content-Transfer-Encoding'] = '7bit'