Quantcast
Channel: iT邦幫忙 - 系統管理
Viewing all articles
Browse latest Browse all 392

求解:Python Email附件為中文檔名檔案出現AT00001 or noname

$
0
0
小弟有一個email函式如下:
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('xxx', 25)
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.quit()

在附檔的部分使用英文檔名沒有問題, 但如果換成中文檔名, 寄給exchange 就會出現at00001.pdf這樣的檔案,gmail則出現noname , 請問這要如何修正 ? 真心感謝每個抽空回答的人

Viewing all articles
Browse latest Browse all 392

Trending Articles