将MIMEText编码为引用打印格式
Python有一个非常实用的库,叫做 email.mime
,它可以处理MIME(多用途互联网邮件扩展)相关的内容。
我想做的是把一段普通的UTF-8文本以“quoted-printable”格式编码,而不是用“base64”格式。虽然这个库里有很多功能,但我还是没能成功使用它:
示例:
import email.mime.text, email.encoders
m=email.mime.text.MIMEText(u'This is the text containing ünicöde', _charset='utf-8')
m.as_string()
# => Leads to a base64-encoded message, as base64 is the default.
email.encoders.encode_quopri(m)
m.as_string()
# => Leads to a strange message
最后这个命令产生了一个奇怪的消息:
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Transfer-Encoding: quoted-printable
GhpcyBpcyB0aGUgdGV4dCBjb250YWluaW5nIMO8bmljw7ZkZQ=3D=3D
显然,这个消息并不是以“quoted-printable”格式编码的,而且双重的 transfer-encoding
头部看起来也很奇怪(如果不是非法的话)。
我该如何才能让我的文本在mime消息中以“quoted-printable”格式编码呢?
3 个回答
5
这段内容改编自问题 1525919,并在 Python 2.7 上进行了测试:
from email.Message import Message
from email.Charset import Charset, QP
text = "\xc3\xa1 = \xc3\xa9"
msg = Message()
charset = Charset('utf-8')
charset.header_encoding = QP
charset.body_encoding = QP
msg.set_charset(charset)
msg.set_payload(msg._charset.body_encode(text))
print msg.as_string()
这段代码会给你输出:
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
=C3=A1 =3D =C3=A9
另外,可以看看这个回答,是来自一位 Python 开发者的。
8
在Python 3中,你不需要使用你的那个小技巧:
import email
# Construct a new charset which uses Quoted Printables (base64 is default)
cs = email.charset.Charset('utf-8')
cs.body_encoding = email.charset.QP
m = email.mime.text.MIMEText(u'This is the text containing ünicöde', 'plain', _charset=cs)
print(m.as_string())
14
好的,我找到了一种解决办法,虽然有点绕,但至少能指引我们往前走:MIMEText
默认使用 base64 编码,我不知道怎么改这个设置。为了这个原因,我用了 MIMENonMultipart
:
import email.mime, email.mime.nonmultipart, email.charset
m=email.mime.nonmultipart.MIMENonMultipart('text', 'plain', charset='utf-8')
#Construct a new charset which uses Quoted Printables (base64 is default)
cs=email.charset.Charset('utf-8')
cs.body_encoding = email.charset.QP
#Now set the content using the new charset
m.set_payload(u'This is the text containing ünicöde', charset=cs)
现在消息看起来编码得正确了:
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
This is the text containing =C3=BCnic=C3=B6de
我们甚至可以创建一个新类,把复杂的东西隐藏起来:
class MIMEUTF8QPText(email.mime.nonmultipart.MIMENonMultipart):
def __init__(self, payload):
email.mime.nonmultipart.MIMENonMultipart.__init__(self, 'text', 'plain',
charset='utf-8')
utf8qp=email.charset.Charset('utf-8')
utf8qp.body_encoding=email.charset.QP
self.set_payload(payload, charset=utf8qp)
然后可以这样使用它:
m = MIMEUTF8QPText(u'This is the text containing ünicöde')
m.as_string()