将MIMEText编码为quoted-printable

13

Python支持一个相当实用的MIME-Library叫做email.mime

我想要实现的是将包含纯文本UTF-8的MIME部件编码为引用打印而不是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

很明显这不是被编码为可引用打印字符,双重transfer-encoding头在最后很奇怪(如果不是非法的话)。

我该怎么才能让我的文本在mime-message中被编码为可引用打印字符呢?


1
请参见https://dev59.com/2mDVa4cB1Zd3GeqPgL_b#9509718--该问题是关于Python 3的,但我也在Python 2中使用过它。 - tripleee
1
对于 Python 3.6+,还可以参考现在的 https://stackoverflow.com/questions/66039715/python3-email-message-to-disable-base64-and-remove-mime-version/66041936#66041936 - tripleee
3个回答

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()

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())

2
公平地说,在Python 2中需要这个hack。你的答案只适用于Python 3。因此,基本上可以说通过切换到Python 3来解决原始问题。 - Felix Schwarz

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开发者的这篇回答


我一开始没有注意到 body_encode 的输入必须已经是 utf-8 编码,并且它不会为你进行 utf-8 编码。在这里记录下来,以防其他人犯同样的误解而浪费时间。 - new name

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接