码迷,mamicode.com
首页 > 编程语言 > 详细

python邮件之附件

时间:2016-06-05 06:29:10      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

 接上文,python发附件

#!/usr/local/bin/python3.5
from email.mime.text import MIMEText
from smtplib import SMTP_SSL
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

msg = MIMEText(hello,send by Python...,plain,utf-8)

mail_info = {
        From:xxxxxx@xxx.xxxx.xx,
        Password:xxx,
        To:xxx@xxx.xxx,
        Mail_server:smtp.exmail.qq.com,
        Mail_text:‘‘‘
        <html><body><h1>myblog</h1>  <p>send by <a href="http://www.cnblogs.com/changbo">Myblog</a>...</p>  
        </body></html>
‘‘‘
}
if __name__ == __main__:
        smtp = SMTP_SSL(mail_info[Mail_server])

        smtp.ehlo(mail_info[Mail_server])
        smtp.login(mail_info[From],mail_info[Password])

        msg = MIMEMultipart()
        msg[Subject] = hello 白水寺
        msg[From] = mail_info[From]
        msg[To] = mail_info[To]

        msg.attach(MIMEText(mail_info[Mail_text],html,utf-8))
        with open(/root/2013.jpg,rb) as f:
                mime = MIMEBase(image,jpg,filename=2013.jpg)
                mime.add_header(Content-Disposition, attachment, filename=2013.jpg)
                mime.add_header(Content-ID,<0>)
                mime.add_header(X-Attachment-Id, 0)
                mime.set_payload(f.read())
                encoders.encode_base64(mime)
                msg.attach(mime)

        smtp.sendmail(mail_info[From],mail_info[To],msg.as_string())
        smtp.quit()

 

注意: mime.add_header(Content-Disposition‘, attachment‘, filename=2013.jpg)

         mime.add_header(Content-ID‘,<0>)

    mime.add_header(X-Attachment-Id‘, 0)

这三行代码是必要的头信息,不要的话附件后缀会是bin,需要手动更改附件后缀

如图,未加效果如下,该后缀文件不可直接在browser中预览:

技术分享

 

加上附件相关必要的头信息后,效果如下,可直接在browser中预览:

技术分享

 

预览效果图:

技术分享

 

END!

 

python邮件之附件

标签:

原文地址:http://www.cnblogs.com/changbo/p/5372932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!