[問題] python email 附件中文檔名出現at00001

看板Python作者 (Yes We Can !)時間10年前 (2015/07/17 15:14), 10年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/3 (看更多)
小弟有一個python3 email函式如下: 在附檔的部分使用英文檔名沒有問題, 但如果換成中文檔名, 寄給exchange 就會出現 at00001.pdf這樣的檔案,gmail則出現noname , 請問這要如何修正 ? 有試著在這行 part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(f))) 加上 format(os.path.basename(f).encode('utf-8').decode('big5') 但檔名出現一串編碼... 真心感謝每個抽空回答的人 import smtplib, os from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email.utils import COMMASPACE, formatdate from email import encoders import csv def send_mail( send_from, send_to, subject, html, files=[]): msg = MIMEMultipart() msg['From'] = send_from msg['To'] = COMMASPACE.join(send_to) msg['Date'] = formatdate(localtime = True) msg['Subject'] = subject #本文 msg.attach( MIMEText(html,'html') ) #附檔 for f in files: part = MIMEBase('application', 'pdf') #根據檔案型式置換 part.set_payload( open(f,"rb").read() ) encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(f))) msg.attach(part) smtp = smtplib.SMTP('x.x.x', 25) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.quit() -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.69.75.17 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1437117262.A.F4E.html ※ 編輯: lalelee (210.69.75.17), 07/17/2015 15:24:57 ※ 編輯: lalelee (61.219.108.23), 07/18/2015 08:14:46 ※ 編輯: lalelee (61.219.108.23), 07/18/2015 16:45:42
文章代碼(AID): #1LgAjEzE (Python)
文章代碼(AID): #1LgAjEzE (Python)