python 发送邮件脚本

#!/usr/bin/env python
# _*_coding=utf-8 _*_
#=================
# File: send.py
#author:test
#=================
import sys, os,time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from creater import createReport
class sendMail():
def __init__(self, config_file=None, mail_host=None, mail_user=None, mail_pwd=None, mail_postfix="deyuanyun.com",re_report=None,re_log=None):
self.mail_host = 'smtp.qiye.163.com'
self.mail_user = 'test001'
self.port='25'
self.mail_pwd = '1111g7grr75P8gA'
self.mail_postfix = 'xx.com'
self.mail_send_flag = '1' # 1: send mail, 0: not send mail
self.mail_to_list = 'xx@xx.com'
self.re_report='F:/jekens/workspace/auto_test_regression/default/report.html'
self.re_rlog='F:/jekens/workspace/auto_test_regression/default/log.html'
def send_mail_login_html(self, sub,contentsType=None, to_list=None):
s=createReport(self.re_report)
contents=s.createReportContent(self.re_report)
msg=MIMEMultipart()
if self.mail_send_flag != '1':
return (False, "don't need send mail")
if contentsType == 'html':
#msg = MIMEText(contents,contentsType, 'utf-8')
msg.attach(MIMEText(contents, _subtype='html', _charset='utf-8'))
elif contentsType == 'plain':
#msg = MIMEText(contents, _subtype='plain', _charset='gb18030')
msg.attach(MIMEText(contents, _subtype='plain', _charset='gb18030'))
me ="%s@%s" % (self.mail_user, self.mail_postfix)
msg['subject'] =sub
msg['from'] =me
msg['to'] =",".join(to_list)
att = MIMEText(open(self.re_rlog, 'rb').read(), 'base64', 'gb2312')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="log.html"'
msg.attach(att)
try:
SMTP = smtplib.SMTP(self.mail_host, self.port)
SMTP.login(me, self.mail_pwd)
SMTP.sendmail(me,to_list,msg.as_string())
SMTP.quit()
return (True, "")
except Exception as e:
error_description = (str(e))
return (False,"")

if __name__=="__main__":
sub="%s-完整性管理系统-自动化用例回归结果" % time.strftime('%Y%m%d',time.localtime(time.time()))
contentsType='html'
to_list='xx@qq.com','xx@xx.com',
s=sendMail()
s.send_mail_login_html(sub,contentsType,to_list)
原文地址:https://www.cnblogs.com/colin2012/p/7366328.html