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

看板Python作者 (kent)時間10年前 (2015/07/18 23:55), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
>>> Please refer to this URL: https://docs.python.org/2/library/email.message.html ... An example with non-ASCII characters: msg.add_header('Content-Disposition', 'attachment', filename=('iso-8859-1', '', 'Fu綌aller.ppt')) Which produces Content-Disposition: attachment; filename*="iso-8859-1''Fu%DFballer.ppt" ... >>> BUT, for better compatibility, you may do this https://tools.ietf.org/html/rfc6266 ... This example is the same as the one above, but adding the "filename" parameter for compatibility with user agents not implementing RFC 5987: Content-Disposition: attachment; filename="EURO rates"; filename*=utf-8''%e2%82%ac%20rates ... ※ 引述《lalelee (Yes We Can !)》之銘言: : 小弟有一個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), 來自: 123.192.39.29 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1437234906.A.BBE.html
文章代碼(AID): #1LgdRQk- (Python)
文章代碼(AID): #1LgdRQk- (Python)