-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendMail.py
90 lines (66 loc) · 3.21 KB
/
SendMail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#coding=utf-8
import os
import smtplib
from JiraReport.ReadConfig import readconfig
from JiraReport.GetVersion import get_version
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from JiraReport.LogAndExcept import logger
class SendMali(object):
def __init__(self):
self.msg_from = readconfig("MAIL", "MSG_FROM")
self.passwd = readconfig("MAIL", "PASSWD")
self.msg_to = readconfig("MAIL", "MSG_TO")
#img_name type:tuple
def get_img_path(self,img_names):
nowpath = os.path.dirname(os.path.abspath(__file__))
try:
path = os.path.join(nowpath, "picture/%s.png"%img_names)
with open(path, "rb") as of:
img_data = of.read()
return img_data
except:
logger().error("文件不存在!")
#编辑邮件内容添加图片
def mail_content(self,mail_title="测试报告", total="0", mail_img_name1="Bug类型",mail_img_name2="经办人",mail_img_name3="模块类型"):
subject = mail_title
msg = MIMEMultipart('related')
#iso_version,android_version = get_version()
iso_version, android_version = "111","222"
jv = "APP_" + readconfig().split("_")[0]
content = MIMEText('<html lang="utf-8">'
'<head>'
'<meta http-equiv="Content-Type" content="text/html; charset=gb2312">'
'<title>%s</title>'
'</head>'
'<body><b>Dear all:</b><br>'
'<br>  %s版本测试结束,如下情况<br>'
'<br>测试结果:<font color="green"> 测试通过</font><br>'
'<br>bug情况 合计提交 <font color="red">%s个bug</font>,分布情况请查看饼图(包含模块分类、bug类型、经办人)<br>'
'<br>IOS:%s Android:%s<br>'
'<img src="cid:mail_img_name1" alt="imageid">'
'<img src="cid:mail_img_name2" alt="imageid">'
'<img src="cid:mail_img_name3" alt="imageid">'
'</body></html>'%(mail_title,jv,total,iso_version,android_version), 'html', 'utf-8') #
msg.attach(content)
msg['Subject'] = subject
msg['From'] = self.msg_from
msg['To'] = self.msg_to
img_data1 = self.get_img_path(mail_img_name1)
img_data2 = self.get_img_path(mail_img_name2)
img_data3 = self.get_img_path(mail_img_name3)
img1 = MIMEImage(img_data1)
img1.add_header('Content-ID', 'mail_img_name1')
msg.attach(img1)
img2 = MIMEImage(img_data2)
img2.add_header('Content-ID', 'mail_img_name2')
msg.attach(img2)
img3 = MIMEImage(img_data3)
img3.add_header('Content-ID', 'mail_img_name3')
msg.attach(img3)
return msg
def main(self,total):
s = smtplib.SMTP_SSL("smtp.exmail.qq.com", 465) # 邮件服务器及端口号
s.login(self.msg_from, self.passwd)
s.sendmail(self.msg_from, self.msg_to, self.mail_content(total=total).as_string())