forked from lyhue1991/torchkeras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.py
61 lines (55 loc) · 1.59 KB
/
email.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
# -*- coding: utf-8 -*-
import smtplib
import traceback
from email.mime.text import MIMEText
def send_msg(receivers,subject,msg=""):
"""
Examples:
---------
>> subject = "info@train_model.py" #邮件主题
>> msg = "auc=0.98" #邮件内容
>> receivers = ["265011xxxx@qq.com"] #收件人
>> send_msg(receivers,subject,msg)
"""
#设置服务器所需信息
mail_host = 'smtp.yeah.net'
mail_user = 'bugrobot'
mail_pass = 'NPWPJBSIVXRTYUOB' #密码(部分邮箱为授权码)
sender = 'bugrobot@yeah.net'
#构造邮件内容
message = MIMEText(msg,'plain','utf-8')
message['Subject'] = subject
message['From'] = sender
message['To'] = receivers[0]
#登录并发送邮件
try:
smtpObj = smtplib.SMTP()
#连接到服务器
smtpObj.connect(mail_host,25)
#登录到服务器
smtpObj.login(mail_user,mail_pass)
#发送
smtpObj.sendmail(
sender,receivers,message.as_string())
#退出
smtpObj.quit()
return 'send_msg success'
except smtplib.SMTPException as e:
error = 'send_msg error : '+str(e)
print(error)
return error
def monitor_run(function,receivers):
"""
Examples:
---------
>> receivers = ["265011xxxx@qq.com"] #收件人
>> def f():
>> return 1/0
>> monitor_run(f,receivers)
"""
try:
function()
except Exception as e:
error_msg = traceback.format_exc()
send_msg(receivers,"error@"+function.__name__,error_msg)
raise e