python smptl attachment filename messy code tcmime.xxx.bin email 附件名称 .bin乱码
文件名称也要转为 bytes,使用对应编码 encode
或者
from email.header import Header
att2["Content-Disposition"] = 'attachment; filename="%s"'% \
str(Header(filename,'utf-8'))
# encoding=utf-8
import os
import smtplib import mimetypes from email.mime.audio import MIMEAudio from email.mime.multipart import MIMEMultipart from email.header import Header _user = "xy@" _pwd = "y" _to = "00000@qq.com" msg_ = MIMEMultipart() msg_["Subject"] = "don't panic" msg_["From"] = "_user" msg_["To"] = "xy@dois.top" path = ur"F:\media\音乐\Springloll - 랄라라.mp3" att2 = MIMEAudio(open(path, 'rb').read(),"mp3") att2["Content-Type"] = mimetypes.guess_type(path)[0] # or 'application/octet-stream' att2["Content-Disposition"] = 'attachment; filename="%s"'%str(Header(os.path.basename(path), 'UTF-8')) msg_.attach(att2) try: s = smtplib.SMTP_SSL("smtp.qq.com", 465) s.login(_user, _pwd) s.sendmail(_user, [_to], msg_.as_string()) s.quit() print "Success!" except smtplib.SMTPException, e: print "Falied,", e, type(e)