小弟有一个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()