相关疑难解决方法(0)

将MIMEText编码为引用的printables

Python支持一个相当的功能MIME图书馆email.mime.

我想要实现的是获取包含普通UTF-8文本的MIME部分,将其编码为带引号的printables而不是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
Run Code Online (Sandbox Code Playgroud)

最后一个命令导致一个奇怪的消息:

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Transfer-Encoding: quoted-printable

GhpcyBpcyB0aGUgdGV4dCBjb250YWluaW5nIMO8bmljw7ZkZQ=3D=3D
Run Code Online (Sandbox Code Playgroud)

这显然不是编码为引用的printables,双头transfer-encoding最后是奇怪的(如果不是非法).

如何在mime-message中将我的文本编码为带引号的printables?

python encoding quoted-printable

12
推荐指数
3
解决办法
6028
查看次数

如何使用Python 3.2电子邮件模块发送带有quoted-printable的utf-8编码的unicode消息?

我想在Python 3.2程序中发送具有任意unicode主体的电子邮件.但实际上,这些消息主要由7位ASCII文本组成.所以我想使用quoted-printable在utf-8中编码的消息.到目前为止,我发现这有效,但似乎错了:

c = email.charset.Charset('utf-8')
c.body_encoding = email.charset.QP
m = email.message.Message()
m.set_payload("My message with an '\u05d0' in it.".encode('utf-8').decode('iso8859-1'), c)
Run Code Online (Sandbox Code Playgroud)

这会生成包含完全正确内容的电子邮件:

To: someone@example.com
From: someone_else@example.com
Subject: This is a subjective subject.
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

My message with an '=D7=90' in it.
Run Code Online (Sandbox Code Playgroud)

特别b'\xd7\x90'.decode('utf-8')是原始Unicode字符的结果.所以quoted-printable编码正确地渲染了utf-8.我很清楚这是一个令人难以置信的丑陋黑客.但它的确有效.

这是Python 3.文本字符串应始终是unicode.我不应该将其解码为utf-8.然后从将其bytesstr.decode('iso8859-1')是一个可怕的黑客,我不应该做,要么.

email关于编码,模块刚刚破解?我没有得到什么吗?

我试图只是简单地设置它,没有字符集.这留给我一个unicode电子邮件消息,这根本不对.我也尝试过离开encodedecode步骤.如果我将它们都关闭,它会\u05d0在尝试确定是否需要在quoted-printable编码中引用该字符时抱怨它超出范围.如果我离开这encode一步,它会痛苦地抱怨我是如何传入的bytes,它想要一个str.

python email mime character-encoding python-3.x

7
推荐指数
1
解决办法
2536
查看次数