python給多個發送郵件附件,參考於《python自動化運維》



#
!/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)
相關文章
相關標籤/搜索