forked from cet4meiguo/AndroidTestScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendEmail.py
160 lines (134 loc) · 4.86 KB
/
sendEmail.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# coding=utf-8
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
from email.header import Header
import base64
from device_tools import getVersionName
class sendEmail:
def __init__(self,senderAcount,senderPassword,receiver,attachFilePath):
self._senderAcount=senderAcount
self._senderPassword=base64.b64decode(senderPassword)
self._receiver=receiver
self._attachFilePath,self._attachFileName=self.compressFile(attachFilePath)
self._cpu_svg="cpu_info_"+getVersionName("com.zego.livedemo5")+".svg"
self._mem_svg="mem_info_"+getVersionName("com.zego.livedemo5")+".svg"
# self.outputHtmlFile()
print("self._attachFilePath="+self._attachFilePath)
print("self._attachFileName="+self._attachFileName)
# print(self._senderAcount)
# print(self._senderPassword)
# print(self._receiver)
def compressFile(self,filePath):
if filePath.endswith(".zip"):
fileName=filePath[filePath.rfind("/")+1:]
return filePath,fileName+".zip"
fileName=filePath[filePath.rfind("/")+1:]
# os.popen("rm %s.zip" % fileName)
print("compress file")
os.popen("zip -q -r %s.zip %s" % (fileName,fileName))
filePath=filePath
return filePath,fileName+".zip"
def outputHtmlFile(self):
htmlHead="""<!DOCTYPE html>
<html lang="en">
<body>"""
htmlTail="""</body>
</html>"""
# 输出cpu的html
file=open(os.path.join(self._attachFilePath,self._cpu_svg),"rb")
fileContent=htmlHead
for line in file:
fileContent+=line
fileContent+=htmlTail
file.close()
html=open(os.path.join(self._attachFilePath,"cpu_info.html"),"w")
html.write(fileContent)
html.close()
# 输出内存的html
file=open(os.path.join(self._attachFilePath,self._mem_svg),"rb")
fileContent=htmlHead
for line in file:
fileContent+=line
fileContent+=htmlTail
file.close()
html=open(os.path.join(self._attachFilePath,"mem_info.html"),"w")
html.write(fileContent)
html.close()
def send(self):
message=MIMEMultipart()
message["From"]=formataddr(["测试程序",self._senderAcount])
message["To"]=",".join(self._receiver)
message["Subject"]="Android版LiveDemo5测试报告"
# msgContent="""
# <p>邮件发送html格式测试</p>
# <p><a href="http://www.baidu.com">baidu</a></p>
# """
msgContent="""
<!DOCTYPE html>
<html lang="en">
<body>
"""
msgContent=msgContent+"<p>Android版的LiveDemo5的测试报告</p><p>以下两张分别为cpu和内存的变化图,浏览器无法显示该图,邮件客户端可以显示</p><p>在邮件客户端下双击附件中的svg图像即可正常打开</p><p>需要更详细的数据请下载附件</p>"
# 添加cpu的svg图至邮件内容
file=open(os.path.join(self._attachFilePath,self._cpu_svg),"rb")
for line in file:
msgContent+=line
file.close()
msgContent+="<p></p>"
msgContent+="<p></p>"
# 添加mem的svg图至邮件内容
file=open(os.path.join(self._attachFilePath,self._mem_svg),"rb")
for line in file:
msgContent+=line
file.close()
msgContent+="""
</body>
</html>
"""
# print(msgContent)
message.attach(MIMEText(msgContent,"html","utf-8"))
# 附件 压缩文件
file=MIMEText(open(self._attachFilePath+".zip","rb").read(),"base64","utf-8")
file["Content-Type"]="application/octet-stream"
file["Charset"]="utf-8"
file["Content-Disposition"]="attachment;filename=%s" % Header(self._attachFileName,"utf-8")
message.attach(file)
# 附件 cpu_info.svg
file=MIMEText(open(os.path.join(self._attachFilePath,self._cpu_svg),"rb").read(),"base64","utf-8")
file["Content-Type"]="application/octet-stream"
file["Charset"]="utf-8"
file["Content-Disposition"]="attachment;filename=%s" % Header(self._cpu_svg,"utf-8")
message.attach(file)
# 附件 mem_info.svg
file=MIMEText(open(os.path.join(self._attachFilePath,self._mem_svg),"rb").read(),"base64","utf-8")
file["Content-Type"]="application/octet-stream"
file["Charset"]="utf-8"
file["Content-Disposition"]="attachment;filename=%s" % Header(self._mem_svg,"utf-8")
message.attach(file)
# try:
# os.remove(os.path.join(self._attachFilePath,"cpu_info.html"))
# os.remove(os.path.join(self._attachFilePath,"mem_info.html"))
# except Exception,e:
# print("sendEmail->*.html not exist")
# print(e)
try:
smtpObj=smtplib.SMTP_SSL("smtp.exmail.qq.com",465)
smtpObj.ehlo()
smtpObj.login(self._senderAcount,self._senderPassword)
smtpObj.sendmail(self._senderAcount,self._receiver,message.as_string())
print("send email success")
except Exception,e:
print("sendEmail.py->send()")
print(e)
finally:
smtpObj.close()
if __name__=="__main__":
print("sendEmail test")
senderAcount="[email protected]"
senderPassword="V3VqaW55b25nMTIz"
receiver=["[email protected]","[email protected]"]
send=sendEmail(senderAcount,senderPassword,receiver,"/Users/cier/zegolivedemo/autotest/android/20170726_110224")
send.send()