-
Notifications
You must be signed in to change notification settings - Fork 7
/
mail.py
48 lines (36 loc) · 1.17 KB
/
mail.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
from logging.handlers import SMTPHandler
from sender import Mail, Message
import config
logger = logging.getLogger(__name__)
sender = config.mail['sender']
host = config.mail['host']
password = config.mail['password']
receivers = config.mail['receivers']
def add_error_log_mail_handler(logger, system):
mail_handler = SMTPHandler(
mailhost=host,
fromaddr=sender,
toaddrs=receivers,
subject='[{system}] 发生错误'.format(system=system),
credentials=(sender, password),
timeout=30
)
mail_handler.setLevel(logging.ERROR)
logger.addHandler(mail_handler)
def send_mail(subject, to, content, cc=None, type='plain', system='自动'):
if cc is None:
cc = []
html, body = None, None
if type == 'html':
html = content
else:
body = content
subject = subject.replace('\n', ' ')
subject = '[{}]{}'.format(system, subject)
mail = Mail(host=host, port=25, username=sender, password=password)
msg = Message(subject=subject, to=to, cc=cc, html=html, body=body,
fromaddr=sender)
mail.send(msg)