# coding:utf-8
# @Date : 2016-09-03
# @Author : 丰申
# @Version : 0.0.1
# @Desc :Custom Email module
# @example:
# temp_email = CustomEmail('smtp.126.com', 'XXXXX@126.com', 'XXXXX')
# temp_email.AddMainContent('text/plain', '中文啊!')
# temp_email.AddMainContent('text/html', '我是')
# temp_email.AddMainContent('file/html', 'E:\\RFS\\Logs\\20160902\\1110385186\\mailrpt.html')
# temp_email.AddAttachment('E:\\RFS\\Logs\\20160902\\1110385186.zip')
# temp_email.AddToMSG()
# temp_email.SendEmail('xxxxxxxxxxx@qq.com;xxxxxx@qq.com', 'XX自动化测试报告')
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.header import Header
import codecs
class CustomEmail(object):
def __init__(self, host, host_user, host_password):
'''
:param host: 服务器地址
:param host_user:发信人账户
:param host_password:发信人密码
:return:无
'''
self.host = host
self.host_user = host_user
self.host_password = host_password
self.send_port = 25
self.msg = MIMEMultipart()
self.msg['From'] = self.host_user
self.listAttach = []
self.listMainContent = []
self.flagAttach = None
self.flagContent = None
self.mail_client = smtplib.SMTP()
self.mail_client.connect(host)
self.mail_client.login(host_user, host_password)
def AddToMSG(self):
'''
:param to_text: 写入邮件主内容
:param to_attach: 写入附件
:return: 无
'''
try:
# 下面是文本部分 ,这里分为了plain与HTML两种格式
if len(self.listMainContent) != 0:
for to_text in self.listMainContent:
self.msg.attach(to_text)
# 下面是附件部分 ,因判断获取的文件类型来附带类型。
if len(self.listAttach) != 0:
for to_attach in self.listAttach:
self.msg.attach(to_attach)
except BaseException, e:
print u'邮件内容生成失败!',e
def AddMainContent(self, contenttype, data):
'''
:param contenttype:
1.text/plain
2.text/html
3.file/html
4.file/plain
:param data:
:return:无
'''
try:
maintype, subtype = contenttype.split('/')
if maintype == 'text':
content = MIMEText(data, subtype, 'utf-8')
else:
tmp_data = codecs.open(data, 'r').read()
content = MIMEText(tmp_data, subtype, 'utf-8')
self.listMainContent.append(content)
self.flagContent = True
except BaseException, e:
self.flagContent = False
print u'生成邮件正文失败!',e
def AddAttachment(self, filepath):
'''
:param filepath: 附件地址
:return:无
'''
try:
print filepath
tmp_filename = filepath[filepath.rfind('\\')+1:]
content = MIMEApplication(open(filepath, 'rb').read())
content.add_header('Content-Disposition', 'attachment', filename=tmp_filename)
self.listAttach.append(content)
self.flagAttach = True
except BaseException, e:
self.flagAttach = False
print u'添加邮件附件文件失败!',e
def SendEmail(self, to_mail, to_subject=None):
'''
:param to_mail:发送人地址,格式为字符串,以;分割
:return: True/False
'''
try:
list_tomail = to_mail.split(';')
if (self.flagContent == False) or (self.flagAttach == False):
print u'邮件正文或附件文件添加失败,发送邮件失败!'
return False
else:
if to_subject == None:
to_subject = '自动化测试报告'
self.msg['Subject'] = Header(to_subject, 'utf-8')
self.mail_client.sendmail(self.msg['From'], list_tomail, self.msg.as_string())
self.mail_client.quit()
print u'邮件发送成功!'
return True
except smtplib.SMTPException, e:
print u'邮件发送失败!', e.message
return False