-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathemail_util.py
65 lines (56 loc) · 2.28 KB
/
email_util.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
import os
import sys
import logging
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
class SmtpLogger(object):
def __init__(self, logger):
self.logger = logger
def write(self, message):
if not message.strip():
return
self.logger.debug(message)
class EmailSender(object):
"""generate and send emails"""
def __init__(self):
email_params = os.environ['ipreporter'].split(',')
self.from_email, \
self.from_smtp, \
self.password = email_params[:3]
self.to_emails = email_params[3:]
self.logger = self._get_logger()
def _get_logger(self):
logger = logging.getLogger('EmailSender')
logger.setLevel(logging.DEBUG)
log_file = './log/email.log'
handler = logging.FileHandler(log_file, encoding='utf8')
formatter = logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def _format_addr(self, s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
def make_email(self, msg_str):
mail = MIMEText(msg_str, 'plain', 'utf-8')
mail['From'] = self._format_addr('IP Reporter <%s>' %self.from_email)
mail['To'] = self._format_addr(', '.join(['<%s>' %te for te in self.to_emails]))
mail['Subject'] = Header('Remote IP Address has Changed', 'utf-8').encode()
return mail
def _send_email(self, mail):
orig_std = (sys.stdout, sys.stderr)
sys.stdout = sys.stderr = SmtpLogger(self.logger)
try:
self.logger.info('========================= email begin =========================')
server = smtplib.SMTP(self.from_smtp, 25)
server.set_debuglevel(1)
server.login(self.from_email, self.password)
server.sendmail(self.from_email, self.to_emails, mail.as_string())
server.quit()
self.logger.info('========================= email over =========================\n\n')
finally:
sys.stdout, sys.stderr = orig_std
def send_email(self, msg_str):
self._send_email(self.make_email(msg_str))