gpt4 book ai didi

python - 在使用 python 的电子邮件和 smtplib 包发送的电子邮件中,EmailMessage 无法正确显示

转载 作者:行者123 更新时间:2023-12-04 13:24:07 24 4
gpt4 key购买 nike

我的电子邮件可以正确发送,但在接收方邮件中显示不正确。它看起来像这样:

To: =?utf-8?b?..?= <....com> MIME-Version: 1.0Content-Type: multipart/mixed;boundary="===============5404281335870522242=="

--===============5404281335870522242== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: base64

5bCK5pWs55qE5a2U6LaF5YW...

--===============5404281335870522242== Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: attachment;filename="user.png" MIME-Version: 1.0

iVBORw0KGgo...


除了 Subject 外,直接显示 MIME 字符串和 From行(显示在 To 之后)以及纯文本中的所有正文。
这是我的代码:
import smtplib
import ssl
import mimetypes
from pathlib import Path
from email.message import EmailMessage
from email.utils import formataddr
import time

class EmailSender:
PORT = 465
CONTEXT = ssl.create_default_context()

def __init__(
self,
username,
password,
host,
):
self.username = username
self.password = password
self.host = host
self.mails = []

def _add_name_header(self, name="", mail_addr=""):
if name:
return formataddr((name, mail_addr))
else:
return mail_addr

def add_mail(
self,
from_email="",
from_name="",
to_email="",
to_name="",
subject="",
message_txt="",
files=None,
):
msg = EmailMessage()
msg["Subject"] = subject
msg["From"] = self._add_name_header(from_name, from_email)
msg["To"] = self._add_name_header(to_name, to_email)
msg.set_content(message_txt)

if not files is None:
for file_obj in files:
if file_obj.exists():
file = str(file_obj)
ctype, encoding = mimetypes.guess_type(file)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compressed), so use a generic bag-of-bits type.
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
with file_obj.open("rb") as fp:
msg.add_attachment(
fp.read(),
maintype=maintype,
subtype=subtype,
filename=file_obj.name,
)

self.mails.append(msg)

def send(self, time_interval=1):
with smtplib.SMTP_SSL(
host=self.host, port=self.PORT, context=self.CONTEXT
) as server:
try:
server.login(user=self.username, password=self.password)
except Exception as e:
# Need process errors
raise e
for msg in self.mails:
server.send_message(msg)
time.sleep(time_interval)
我只是这样做:
sender = EmailSender(
username, password, host="smtp.163.com"
)

files = list(Path("D:/").glob("*.pdf"))

sender.add_mail(
from_email, from_name, to_email, to_name, subject, message_txt, files=None
)
sender.send(time_interval=10)

最佳答案

我是问题的 OP。我只是自己解决了这个问题,我会分享解决方案。
TLNR:我的邮件中使用了非 Ascii 字符,因此请使用 msg = EmailMessage(EmailPolicy(utf8=True))而不是 msg = EmailMessage() .
我在 SMTP.send_message 的文档中误解了这些句子:

If any of the addresses in from_addr and to_addrs contain non-ASCIIcharacters and the server does not advertise SMTPUTF8 support, anSMTPNotSupported error is raised. Otherwise the Message is serializedwith a clone of its policy with the utf8 attribute set to True, andSMTPUTF8 and BODY=8BITMIME are added to mail_options.


由于我在地址中添加了非 ASCII header ,因此我相信 smtplib将自动为我使用 utf8 策略。但是在文件 smtplib.py我看见了这个:
if from_addr is None:
# Some code
from_addr = email.utils.getaddresses([from_addr])[0][1]
if to_addrs is None:
# Some code
to_addrs = [a[1] for a in email.utils.getaddresses(addr_fields)]
# Some code
international = False
try:
"".join([from_addr, *to_addrs]).encode("ascii")
except UnicodeEncodeError:
# Some code
international = True
也就是说,该函数仅检查地址部分是否具有非 ASCII 字符,但不检查标题名称。
之后,消息作为纯ASCII内容处理,这可能没问题,我不知道为什么,但是somohow, 许多额外/rTo:xxx 前后插入字符线路 , 这让 smtp 服务器认为这是一个分隔符吗?并最终导致了问题。

关于python - 在使用 python 的电子邮件和 smtplib 包发送的电子邮件中,EmailMessage 无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69695719/

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