Python 发邮件遇到邮件客户端附件名显示不对的问题

2018-10-27 22:13:49 +08:00
 nightstart

message['Subject'] = Header(subject, 'utf-8') message.attach(MIMEText(contents, 'plain', 'utf-8'))

if filename is not None and path is not None:
    attach_file = MIMEText(open(path, 'rb').read(), 'base64', 'utf-8')
    attach_file["Content-Type"] = 'application/octet-stream'
    attach_file.add_header('Content-Disposition', 'attachment', filename=('gb2312', '', filename))
    message.attach(attach_file)
try:
    result = True
    errors = ''
    if int(usessl):
        smtpObj = smtplib.SMTP_SSL(mail_host, port)
    else:
        smtpObj = smtplib.SMTP(mail_host, port)
    smtpObj.login(mail_user, mail_pass)
    smtpObj.sendmail(sender, receivers, message.as_string())

这是我邮件主要的代码、目前用各个邮箱的网页端接收邮件和附件都是正常的。但是用 windows 端的 outlook 客户端接收邮件的时候就会出现问题 附件的名称会被改成 Attachment_1.txt 而不是原来的名称和后缀 跟各位大佬请教一下 这个是要在哪里加什么东西吗 = = 我被困扰好久了

Python3 MacOS 和 Linux7 环境下

1689 次点击
所在节点    问与答
2 条回复
nightstart
2018-10-27 22:26:02 +08:00
上浮
nightstart
2018-10-28 19:33:20 +08:00
上浮 1

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/501784

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX