python3电子邮件以禁用base64并删除MIME版本 [英] python3 email message to disable base64 and remove MIME-Version

查看:84
本文介绍了python3电子邮件以禁用base64并删除MIME版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from email.message import EmailMessage
from email.headerregistry import Address
msg = EmailMessage()

msg['From'] = Address("Pepé Le Pew", "pepe", "example.com")
msg['To'] = (
        Address("Penelope Pussycat", "penelope", "example.com")
        , Address("Fabrette Pussycat", "fabrette", "example.com")
        )
msg['Subject'] = 'This email sent from Python code'
msg.set_content("""\
        Salut!

        Cela ressemble à un excellent recipie[1] déjeuner.

        [1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718

        --Pepé
        """)
print(msg)

上面的代码生成使用base64编码的电子邮件.如何禁用它?如何删除MIME版本的字段?

The above code produces an email message that uses base64 encoding. How to disable it? How to remove the field of MIME-Version?

将"pep"编码为被收件人正确解释?如果没有,确保接收者正确解释其编码的正确方法是什么?

Will the encoding of "Pepé" be correctly interpreted by the recipient? If not, what is the correct way to ensure its encoding is interpreted by the recipient correctly?

From: Pepé Le Pew <pepe@example.com>
To: Penelope Pussycat <penelope@example.com>,
 Fabrette Pussycat <fabrette@example.com>
Subject: This email sent from Python code
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
MIME-Version: 1.0

CQlTYWx1dCEKCgkJQ2VsYSByZXNzZW1ibGUgw6AgdW4gZXhjZWxsZW50IHJlY2lwaWVbMV0gZMOp
amV1bmVyLgoKCQlbMV0gaHR0cDovL3d3dy55dW1tbHkuY29tL3JlY2lwZS9Sb2FzdGVkLUFzcGFy
YWd1cy1FcGljdXJpb3VzLTIwMzcxOAoKCQktLVBlcMOpCgkJCg==

推荐答案

您绝对不能删除 MIME-Version:标头;这就是将其标识为MIME消息的原因.

You absolutely must not remove the MIME-Version: header; it's what identifies this as a MIME message.

From:标头确实应该是RFC2047编码的,并且文档建议它将是消息序列化时".当您 print(msg)时,您没有正确地对其进行序列化;您想要 print(msg.as_string())确实显示所需的序列化.

The From: header should indeed be RFC2047-encoded, and the documentation suggests that it will be "when the message is serialized". When you print(msg) you are not properly serializing it; you want print(msg.as_string()) which does exhibit the required serialization.

在传输编码方面,Python的 email 库对于将 base64 用作可以很好地编码为带引号可打印内容的内容没有吸引力.您无法真正可靠地发送完全未经编码的内容(尽管您愿意,但MIME 8bit binary 编码将能够容纳该内容;但是为了向后兼容,SMTP要求将所有内容都编码为7位表示形式.

When it comes to the transfer encoding, Python's email library has an unattractive penchant for using base64 for content which could very well be encoded as quoted-printable instead. You can't really reliably send the content completely unencoded (though if you wanted to, the MIME 8bit or binary encodings would be able to accommodate that; but for backwards compatibility, SMTP requires everything to be encoded into a 7-bit representation).

在旧的 email 库中,需要各种提示来执行此操作,但是在Python 3.6中引入的新的 EmailMessage API中,您实际上只需要添加 cte ='quoted-printable set_content 调用.

In the old email library, various shenanigans were required to do this, but in the new EmailMessage API introduced in Python 3.6, you really only have to add cte='quoted-printable to the set_content call.

from email.message import EmailMessage
from email.headerregistry import Address

msg = EmailMessage()

msg['From'] = Address("Pepé Le Pew", "pepe", "example.com")
msg['To'] = (
        Address("Penelope Pussycat", "penelope", "example.com")
        , Address("Fabrette Pussycat", "fabrette", "example.com")
        )
msg['Subject'] = 'This email sent from Python code'
msg.set_content("""\
        Salut!

        Cela ressemble à un excellent recipie[1] déjeuner.

        [1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718

        --Pepé
        """, cte="quoted-printable")   # <-- notice added parameter
print(msg.as_string())                 # <-- properly serialize

不幸的是,从文档中弄清楚这一点几乎是不可能的. set_content 基本上只是遵从 policy 会模糊地指向

Unfortunately, figuring this out from the documentation is next to impossible. The documentation for set_content basically just defers to the policy which obscurely points to the raw_data_manager (if you even notice the link) ... where finally you hopefully notice the presence of the cte keyword argument.

演示: https://ideone.com/eLAt11

(顺便说一句,您可能还想在正文中 replace('\ n','\ n').)

(As an aside, you might also want to replace('\n ', '\n') in the body text.)

如果您要使用 8bit binary 内容传输编码,则两者之间的区别在于,前者具有行长限制(最多900个字符),而后者是完全不受约束的.但是,您需要确保整个SMTP传输路径都是8位整洁的(此时,您最好还是转到

If you go for an 8bit or binary content transfer encoding, the difference between them is that the former has a line length limit (max 900-something characters) whereas the latter is completely unconstrained. But you need to be sure that the entire SMTP transfer path is 8-bit clean (at which point you might as well pivot to Unicode email / ESMTP SMTPUTF8 entirely).

为了娱乐,这里有一些关于黑客

For your possible entertainment, here are some old questions with crazy hacks for Python 3.5 and earlier.

这篇关于python3电子邮件以禁用base64并删除MIME版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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