简体   繁体   English

Python将转义符添加到电子邮件附件名称

[英]Python adding escape characters to email attachment name

So, I'm relatively new to python and I'm trying to send email with attachments. 因此,我是python的新手,我正尝试发送带有附件的电子邮件。 I'm using the guide described on StackOverflow here: How to send email attachments with Python 我在这里使用StackOverflow上描述的指南: 如何使用Python发送电子邮件附件

I'm using the following code to attach files... 我正在使用以下代码附加文件...

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

When I receive the email, the attachments are all called "noname". 当我收到电子邮件时,所有附件都称为“ noname”。 If I open the original email in Gmail, it shows the attachments as follows: 如果我在Gmail中打开原始电子邮件,则会显示附件,如下所示:

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

Why is the name not coming through properly? 为什么名称不正确?

For reasons completely lost on me, the following code works. 由于完全不了解我的原因,以下代码有效。 Seems like it's doing the same thing, though. 不过,似乎它在做相同的事情。 Anybody know what's going on? 有人知道发生了什么吗?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM