#!/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)