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

python给多个发送邮件附件,参考于《python自动化运维》

时间:2015-02-12 12:00:44      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:



#
!/usr/bin/env python #coding: utf-8 #author:luodi date:2015/02/12 #description:this is a send mail script import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText #定义SMTP服务器及相关信息 HOST = "smtp.exmail.qq.com" SUBJECT = u"发送附件邮件" TO = "923401910@qq.com,user2@qq.com" FROM = "yunwei@test.com" #定义发送附件 msg = MIMEMultipart(related) attach = MIMEText(open("./nginxlogfile.tar.gz", "rb").read(), "base64", "utf-8") attach["Content-Type"] = "application/octet-stream" #对文件名进行转码,否则会出现乱码,发出去的文件无法识别 attach["Content-Disposition"] = "attachment; filename=\"nginxlogfile.tar.gz\"".decode("utf8").encode("gb18030") msg.attach(attach) msg[Subject] = SUBJECT msg[From]=FROM msg[To]=TO try: server = smtplib.SMTP() server.connect(HOST,"25") server.starttls() server.login("yunwei@test.com","passwd") server.sendmail(FROM, TO, msg.as_string()) server.quit() print "邮件发送成功!" except Exception, e: print "失败:"+str(e)

 

python给多个发送邮件附件,参考于《python自动化运维》

标签:

原文地址:http://www.cnblogs.com/LD-linux/p/4287487.html

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