在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ Python 郵件帶Excel附件,但是接收到的附件名和后綴名都不對

Python 郵件帶Excel附件,但是接收到的附件名和后綴名都不對

如圖,收到郵件之后,附件標(biāo)題不是我設(shè)置的,而且后綴名變成了bin,應(yīng)該是xls
clipboard.png

代碼片段:

file = MIMEText(open(filepath,'rb').read(),'xls','gb2312')
        file["Content-Type"] = 'application/octet-stream'
        file["Content-Disposition"] = 'attachment;filename=%s' % filename
        #這我也不知道干啥的
        file.add_header('Content-1','%s' % filename)
        msg.attach(file)

也測試了MIMEBase,結(jié)果是一樣的...

回答
編輯回答
情殺

附件是帶中文嗎如果是的話,下面是自己以前的一個代碼片段,用make_header

from email.header import  make_header
file_msg = MIMEText(open(file,'rb').read(), 'base64', 'UTF-8')
file_msg["Content-Type"] = 'application/octet-stream;name="%s"'% make_header([(file,'UTF-8')]).encode('UTF-8')
file_msg["Content-Disposition"] = 'attachment;filename= "%s"'% make_header([(file, 'UTF-8')]).encode('UTF-8')
msg.attach(file_msg)
2018年5月9日 15:23