forked from Yasil/carewebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailalert.py
46 lines (40 loc) · 1.33 KB
/
mailalert.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
# -*- coding: utf-8 -*-
from smtplib import SMTP_SSL
import email.utils
from email.mime.text import MIMEText
import log
from tool import read_config
logger = log.logging.getLogger("mail")
def send_email(title, msg, receivers):
dmail = read_config()
sender = dmail.get('mail_sender')
#smtp server
host_server = dmail.get('mail_server')
#smtp server pw
pwd = dmail.get('mail_server_pw')
message = MIMEText(msg)
# message['To'] = email.utils.formataddr(('Recipient','[email protected]'))
message['From'] = email.utils.formataddr(('websiteadmin',sender))
message['Subject'] = title
try:
smtp = SMTP_SSL(host_server)
#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp.set_debuglevel(0)
smtp.ehlo(host_server)
smtp.login(sender, pwd)
smtp.sendmail(sender,
receivers,
message.as_string())
smtp.quit()
except Exception as e:
logger.error("send email fail:{}".format(e))
return False, e
else:
logger.debug("send email success")
return True, ''
finally:
pass
# print("结束")
if __name__ == '__main__':
# send_email('测试错误', '网站访问错误site can\'t vists', ['[email protected]'])
pass