gpt4 book ai didi

Python向电子邮件附件名称添加转义字符

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

所以,我对 python 比较陌生,我正在尝试发送带有附件的电子邮件。我正在使用 StackOverflow 上描述的指南:How to send email attachments with Python

我正在使用以下代码附加文件...

for f in glob.glob('./*/*'):
path = f.split('/')

with open(f, "rb") as fil:
msg.attach(MIMEApplication(
fil.read(),
Content_Disposition = 'attachment; filename="' + path[-1] + '"'
))
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(emailFrom, emailTo, msg.as_string())
server.quit()

当我收到电子邮件时,附件都被称为“noname”。如果我在 Gmail 中打开原始电子邮件,它会显示附件,如下所示:

Content-Type: application/octet-stream; Content-Disposition="attachment;
filename=\"Daily Report.txt\""

为什么名称不能正确显示?

最佳答案

由于我完全不明白的原因,以下代码可以工作。不过,似乎它在做同样的事情。有人知道这是怎么回事吗?

for f in glob.glob('./*/*'):
path = f.split('/')

with open(f, 'rb') as fil:
submsg = MIMEApplication(fil.read())
submsg.add_header('Content-Disposition', 'attachment', filename = path[-1])

msg.attach(submsg)

关于Python向电子邮件附件名称添加转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30060222/

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