如何使用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?

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

问题描述

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

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.

特别 b'\xd7\x90'.decode utf-8')导致原始的Unicode字符。因此, quoted-printable 编码正确呈现了 utf-8 。我知道这是一个令人难以置信的丑陋的黑客。但它的工作原理。

In particular b'\xd7\x90'.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.

这是Python 3.文本字符串应该总是unicode。我不应该解码到utf-8。然后通过 .decode('iso8859-1')将字节转回 str 是一个可怕的黑客,我不应该这样做。

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.

code>模块只是打破了编码?我没有得到什么?

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

我试图只是设置它,没有字符集。这留给我一个unicode电子邮件,这是不正确的。我也试过离开 encode decode 步骤。如果我把它们都关闭,它会抱怨当你尝试决定是否需要在引用的可打印编码中引用该字符时, \\\א 超出范围。如果我只是在 encode 步骤中离开,它抱怨我如何传递一个字节 str

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.

推荐答案

是哪个(编码的Unicode或内容传输编码的二进制数据),但是文档并不清楚,因为许多文档起源于编码意味着内容传输的时代,编码。我们正在开发一个更好的API,使所有这些更容易grok(和更好的文档)。

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

实际上有一种方法来获取电子邮件包使用QP对于utf-8的机构,但它没有很好的文件。你这样做:

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"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nThis is utf-8 text: =E1'

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

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