Python3.4 郵件發送(含帶中文附件)

pe6y 10年前發布 | 2K 次閱讀 Python

    import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email import encoders
user = '***@qq.com'
pwd = '***'
to = ['**@139.com', '**@qq.com']
msg = MIMEMultipart()
msg['Subject'] = '這里是主題...'
content1 = MIMEText('這里是正文!', 'plain', 'utf-8')
msg.attach(content1)
attfile = 'C:\Users\hengli\Pictures\CameraMan\哈哈.doc'
basename = os.path.basename(attfile)
fp = open(attfile, 'rb')
att = MIMEText(fp.read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att.add_header('Content-Disposition', 'attachment',filename=('gbk', '', basename))
encoders.encode_base64(att)
msg.attach(att)

#-----------------------------------------------------------  
s = smtplib.SMTP('smtp.qq.com')  
s.login(user, pwd)  
s.sendmail(user, to, msg.as_string())  
print('發送成功')  
s.close()  </pre> 


 本文由用戶 pe6y 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!