其他分享
首页 > 其他分享> > 发邮件,附件变成二进制文件

发邮件,附件变成二进制文件

作者:互联网

发送的是带附件的邮件,发送完成后,去检查发现,附件变成二进制文件了

 

出错的代码主要是这一句:

att1["Content-Disposition"] = 'attachment; filename="test.txt"'

只要改正这里就可以了

 

 

问题代码

att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="test.txt"'
message.attach(att1)

 

 

改正后的代码

att1 = MIMEText(open('txt.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1.add_header('Content-Disposition', 'attachment', filename='txt.txt')
msg.attach(att1)

 

标签:发邮件,att1,二进制,filename,Content,attachment,Disposition,附件,txt
来源: https://www.cnblogs.com/shiyixirui/p/14692151.html