相关疑难解决方法(0)

如何使用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
查看次数

如何使用quoted-printable transfer-encoding和utf-8内容编码发送电子邮件?

python email.mime倾向于使用编码base647bitus-ascii.我想用quoted-printableutf-8.

目前,我的电子邮件看起来像

--===============6135350048414329636==
Content-Type: text/plain
MIME-Version: 1.0
Content-Transfer-Encoding: base64

IyEvYmluL2Jhc2gKCmZvciBpIGluIHs4Mjg4Li44N
Run Code Online (Sandbox Code Playgroud)

或者,在某些情况下,

--===============0756888342500148236==
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

foobar
Run Code Online (Sandbox Code Playgroud)

我希望原始电子邮件更易于人类阅读.我也更喜欢utf-8编码.

python email smtp

2
推荐指数
1
解决办法
4614
查看次数

标签 统计

email ×2

python ×2

character-encoding ×1

mime ×1

python-3.x ×1

smtp ×1