Re: [问题] python email 附件中文档名出现at00001

楼主: cphsu (kent)   2015-07-18 23:54:58
>>> 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()

Links booklink

Contact Us: admin [ a t ] ucptt.com