gpt4 book ai didi

python - 附加对于具有非多部分负载的消息无效

转载 作者:行者123 更新时间:2023-12-01 02:50:08 24 4
gpt4 key购买 nike

我目前正在编写一个应用程序,需要将电子邮件加载到内存中,添加附件并将同一封电子邮件发送回用户。这在过去工作得很好,但我目前面临一个问题,即电子邮件以 base64Content-Transfer-Encoding 发送。

我在网上找到了一个脚本,可以将内置的 Python 电子邮件消息对象转换为多部分,但是每当我执行此操作时,原始电子邮件都不会以 Base64 形式发送,现在每当我重新发送电子邮件时都会以纯文本形式显示。

有人知道我该如何解决吗?已添加(大部分经过编辑的)电子邮件以及我用于将电子邮件转换为多部分的代码。感谢您提前提供的帮助。

电子邮件

# Before conversion
From: ██████████ <█████@██████.com>
To: ████████ <███████@██████.com>
Subject: █████████
Date: Fri, ██ ███ 2017 00:18:17 +0200
Content-Language: nl-NL
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64
MIME-Version: 1.0

cmVkYWN0ZWRyZWRhY3RlZHJlZGFjdGVkcmVkYWN0ZWRyZWRhY3RlZHJlZGFjdGVkcmVkYWN0ZWRy
ZWRhY3RlZHJlZGFjdGVkcmVkYWN0ZWRyZWRhY3RlZHJlZGFjdGVkcmVkYWN0ZWRyZWRhY3RlZHJl
ZGFjdGVkcmVkYWN0ZWRyZWRhY3RlZHJlZGFjdGVkcmVkYWN0ZWRyZWRhY3RlZA0K


# After conversion
Content-Type: multipart/mixed; boundary="===============0883378942=="
MIME-Version: 1.0
From: ██████████ <█████@██████.com>
To: ████████ <███████@██████.com>
Subject: █████████
Date: Fri, ██ ███ 2017 00:18:17 +0200
Content-Language: nl-NL
Content-Transfer-Encoding: base64
MIME-Version: 1.0
--===============0883378942==
Content-Type: text/html; charset="utf-8"
cmVkYWN0ZWRyZWRhY3RlZHJlZGFjdGVkcmVkYWN0ZWRyZWRhY3RlZHJlZGFjdGVkcmVkYWN0ZWRy
ZWRhY3RlZHJlZGFjdGVkcmVkYWN0ZWRyZWRhY3RlZHJlZGFjdGVkcmVkYWN0ZWRyZWRhY3RlZHJl
ZGFjdGVkcmVkYWN0ZWRyZWRhY3RlZHJlZGFjdGVkcmVkYWN0ZWRyZWRhY3RlZA0K
--===============0883378942==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="foo.txt"
Hello world
--===============0883378942==--

简单到多部分代码

# If this method is not used on an email object
# A `TypeError` is raised with the message "Attach is not valid on a message with a non-multipart payload"

def mail_to_multipart(mail):
"""
Convert an email to a multipart email
:param mail: Email object
:return: None
"""
if mail.is_multipart():
return mail

mail_new = MIMEMultipart("mixed")
headers = list((k, v) for (k, v) in mail.items() if k != "Content-Type")

for k, v in headers:
mail_new[k] = v

for k, v in headers:
del mail[k]

mail_new.attach(mail)
return mail_new

最佳答案

显然,该问题与 Content-Transfer-Encoding header 未保留在旧的多部分块中有关,通过更改以下行:

headers = list((k, v) for (k, v) in mail.items() if k != "Content-Type")

对此:

headers = list((k, v) for (k, v) in mail.items() if k not in ("Content-Type", "Content-Transfer-Encoding"))

已解决问题

关于python - 附加对于具有非多部分负载的消息无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44884484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com