如何使用 Python 3.2 电子邮件模块发送以 utf-8 编码的 unicode 消息,并带有quoted-printable? [英] How do I use Python 3.2 email module to send unicode messages encoded in utf-8 with quoted-printable?

查看:25
本文介绍了如何使用 Python 3.2 电子邮件模块发送以 utf-8 编码的 unicode 消息,并带有quoted-printable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

c = email.charset.Charset('utf-8')c.body_encoding = email.charset.QPm = email.message.Message()m.set_payload("我的消息中包含 'u05d0'.".encode('utf-8').decode('iso8859-1'), c)

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

致:someone@example.com来自:someone_else@example.com主题:这是一个主观主题.MIME 版本:1.0内容类型:文本/纯文本;字符集=utf-8"内容传输编码:引用打印我的消息中包含 '=D7=90'.

特别是 b'xd7x90'.decode('utf-8') 导致原始 Unicode 字符.所以 quoted-printable 编码正确地呈现 utf-8.我很清楚这是一个非常丑陋的 hack.但它有效.

这是 Python 3.文本字符串应始终为 unicode.我不应该将它解码为 utf-8.然后通过 .decode('iso8859-1') 将它从 bytes 转回 str 是一个可怕的黑客,我不应该也必须这样做.

email 模块是否在编码方面被破坏了?我没有得到什么吗?

我试图简单地设置它,没有字符集.这给我留下了一封 unicode 电子邮件,这完全不对.我也试过省略 encodedecode 步骤.如果我将它们都关闭,它会抱怨 u05d0 在尝试决定是否需要在带引号的可打印编码中引用该字符时超出范围.如果我只留下 encode 步骤,它会抱怨我如何传入 bytes 并且它想要一个 str.>

解决方案

那个电子邮件包并没有混淆哪个是哪个(编码的 unicode 与内容传输编码的二进制数据),但文档并没有说得很清楚,因为大部分文档都可以追溯到编码"意味着内容传输编码的时代.我们正在开发一个更好的 API,让这一切更容易理解(以及更好的文档).

实际上有一种方法可以让电子邮件包对 utf-8 正文使用 QP,但没有很好的文档记录.你这样做:

<预><代码>>>>charset.add_charset('utf-8', charset.QP, charset.QP)>>>m = MIMEText("这是 utf-8 文本:á", _charset='utf-8')>>>长度(米)'内容类型:文本/纯文本;charset="utf-8" MIME 版本:1.0 Content-Transfer-Encoding:quoted-printable 这是 utf-8 文本:=E1'

I want to send email messages that have arbitrary unicode bodies in a Python 3.2 program. But, in reality, these messages will consist largely of 7bit ASCII text. So I would like the messages encoded in utf-8 using quoted-printable. So far, I've found this works, but it seems wrong:

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)

This results in an email message with exactly the right content:

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.

In particular b'xd7x90'.decode('utf-8') results in the original Unicode character. So the quoted-printable encoding is properly rendering the utf-8. I'm well-aware that this is an incredibly ugly hack. But it works.

This is Python 3. Text strings are expected to always be unicode. I shouldn't have to decode it to utf-8. And then turning it from bytes back into str by .decode('iso8859-1') is a horrible hack, and I shouldn't have to do that either.

It the email module just broken with respect to encodings? Am I not getting something?

I've attempted to just plain old set it, with no character set. That leaves me with a unicode email message, and that's not right at all. I've also tried leaving off the encode and decode steps. If I leave them both off, it complains that the u05d0 is out-of-range when trying to decide if that character needs to be quoted in the quoted-printable encoding. If I leave in just the encode step, it complains bitterly about how I'm passing in a bytes and it wants a str.

解决方案

That email package isn't confused about which is which (encoded unicode versus content-transfer-encoded binary data), but the documentation does not make it very clear, since much of the documentation dates from an era when "encoding" meant content-transfer-encoding. We're working on a better API that will make all this easier to grok (and better docs).

There actually is a way to get the email package to use QP for utf-8 bodies, but it isn't very well documented. You do it like this:

>>> charset.add_charset('utf-8', charset.QP, charset.QP)
>>> m = MIMEText("This is utf-8 text: á", _charset='utf-8')
>>> str(m)
'Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable

This is utf-8 text: =E1'

这篇关于如何使用 Python 3.2 电子邮件模块发送以 utf-8 编码的 unicode 消息,并带有quoted-printable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆