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

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

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

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)
原文地址:https://www.cnblogs.com/shiyixirui/p/14692151.html