-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_alert.py
138 lines (127 loc) · 4.78 KB
/
email_alert.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
import json
import smtplib
# 发送字符串的邮件
import time
from email.mime.text import MIMEText
# 需要 MIMEMultipart 类
from email.mime.multipart import MIMEMultipart
import os
# 设置服务器所需信息
def send_mail_success(toEmails):
path = os.path.dirname(os.path.realpath(__file__)) + '/'
if len(toEmails) == 0:
return
with open(path + "sendemail_config.json", 'r', encoding='utf-8') as fw:
injson = json.load(fw)
fromEmailAddr = injson["EmailAddress"] # 邮件发送方邮箱地址
password = injson["Authorization_code"] # (注意不是邮箱密码,而是为授权码)
# 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发
toEmailAddrs = toEmails
print('正在制作邮件...')
now_time = time.strftime("%Y-%m-%d %H:%M", time.localtime())
# 设置email信息
# ---------------------------发送带附件邮件-----------------------------
# 邮件内容设置
message = MIMEMultipart()
# 邮件主题
message['Subject'] = '健康上报(成功)'+now_time
# 发送方信息
message['From'] = fromEmailAddr
# 接受方信息
message['To'] = 'dear'
# 邮件正文内容
message.attach(MIMEText(now_time + '\n你的自动健康上报已成功,谢谢!'
, 'plain', 'utf-8'))
# 构造附件
# ---------------------------------------------------------------------
# 登录并发送邮件
try:
server = smtplib.SMTP('smtp.qq.com') # qq邮箱服务器地址,端口默认为25
server.login(fromEmailAddr, password)
server.sendmail(fromEmailAddr, toEmailAddrs, message.as_string())
print('成功邮件发送成功!')
server.quit()
except smtplib.SMTPException as e:
print("error:", e)
def send_mail_fail(toEmails):
path = os.path.dirname(os.path.realpath(__file__)) + '/'
if len(toEmails) == 0:
return
with open(path + "sendemail_config.json", 'r', encoding='utf-8') as fw:
injson = json.load(fw)
fromEmailAddr = injson["EmailAddress"] # 邮件发送方邮箱地址
password = injson["Authorization_code"] # (注意不是邮箱密码,而是为授权码)
# 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发
toEmailAddrs = toEmails
print('正在制作邮件...')
now_time = time.strftime("%Y-%m-%d %H:%M", time.localtime())
# 设置email信息
# ---------------------------发送带附件邮件-----------------------------
# 邮件内容设置
message = MIMEMultipart()
# 邮件主题
message['Subject'] = '健康上报(失败)' + now_time
# 发送方信息
message['From'] = fromEmailAddr
# 接受方信息
message['To'] = 'dear'
# 邮件正文内容
message.attach(MIMEText(now_time+'\n你的自动健康上报已出错,请手动上报',
'plain', 'utf-8'))
# 构造附件
# ---------------------------------------------------------------------
# 登录并发送邮件
try:
server = smtplib.SMTP('smtp.qq.com') # qq邮箱服务器地址,端口默认为25
server.login(fromEmailAddr, password)
server.sendmail(fromEmailAddr, toEmailAddrs, message.as_string())
print('失败邮件发送成功!')
server.quit()
except smtplib.SMTPException as e:
print("error:", e)
# send_mail()
def if_send(d):
path = os.path.dirname(os.path.realpath(__file__)) + '/'
need_send_success = False
with open(path + "option.json", 'r', encoding='utf-8') as fw:
injson = json.load(fw)
if injson["emailReminder"] == "close":
return
if injson["emailSucessFail"] == "true":
need_send_success = True
ids_success = []
ids_fail = []
dic = d
# 遍历字典列表
for key, values in dic.items():
# print (key, values)
if need_send_success:
if values == '失败':
ids_fail.append(key)
else:
ids_success.append(key)
else:
if values == '失败':
ids_fail.append(key)
# print(ids)
emails_fail = []
emails_success=[]
need_mail = False
with open(path + "id_to_emails.json", 'r', encoding='utf-8') as fw:
dic_info = json.load(fw)
for j in ids_fail:
need_mail = False
if j in dic_info:
need_mail = True
if need_mail:
emails_fail.append(dic_info[j])
for j in ids_success:
need_mail = False
if j in dic_info:
need_mail = True
if need_mail:
emails_success.append(dic_info[j])
# print(emails)
send_mail_fail(emails_fail)
if need_send_success:
send_mail_success(emails_success)