-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.py
116 lines (98 loc) · 3.05 KB
/
test.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/11/6 17:59
# @Author : Fred Yang
# @File : test.py
# @Role : 邮件测试脚本示例
import json
import requests
api = 'http://172.16.0.101:9001/sendmail'
def simple_mail():
"""plain格式邮件,最简单的模式,没有格式"""
payload = {
"to_list": "yanghongfei@shinezone.com",
"subject": "这里是标题",
"content": "这里是内容",
"subtype": "plain",
"att": "None" # 没有附件就写None
}
r = requests.post(api, data=json.dumps(payload))
print(r.text)
def att_html():
"""带附件邮件示例"""
payload = {
"to_list": "yanghongfei@shinezone.com,1923671815@qq.com",
"subject": "这里是标题",
"content": "这里是内容",
"subtype": "plain",
"att": "README.md" # 脚本当前路径下查找,找不到则报错
}
r = requests.post(api, data=json.dumps(payload))
data = json.loads(r.text)
print(data)
def html_mail():
"""构建HTML邮件示例"""
content = """
<!DOCTYPE html><html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
p {
width: 100%;
margin: 30px 0 30px 0;
height: 30px;
line-height: 30px;
text-align: center;
}
table {
width: 100%;
text-align: center;
border-collapse: collapse;
}
tr.desc {
background-color: gray;
height: 30px;
}
tr.desc td {
border-color: #ffffff;
}
td {
height: 30px;
border: 1px solid gray;
}
</style>
</head>
<body>"""
content += """
<table>
<p>HTML邮件示例 </p>
<tr class='desc'>
<td>事件名称</td>
<td>事件内容</td>
<td>过期时间</td>
</tr>"""
content += """
<tr>
<td>{}</td>
<td>{}</td>
<td>{}</td>
</tr>""".format('event.name', 'event.content', 'event.expire_at')
content += """
</table>
</body>
</html>"""
payload = {
"to_list": "yanghongfei@shinezone.com,1923671815@qq.com",
"subject": "这里是标题",
"content": content, # HTML构建出来的内容
"subtype": "html",
"att": "None" # 脚本当前路径下查找,找不到则报错
}
r = requests.post(api, data=json.dumps(payload))
data = json.loads(r.text)
print(data)
if __name__ == '__main__':
simple_mail()
html_mail()
att_html()