所以,我对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\""
为什么这个名字没有正确通过?
答案 0 :(得分:2)
由于完全失去我的原因,以下代码有效。看起来它正在做同样的事情。有谁知道发生了什么事?
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)