-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathnotifier.py
48 lines (38 loc) · 1.42 KB
/
notifier.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/python
# -*- coding: UTF-8 -*-
import smtplib
import traceback
from email.mime.text import MIMEText
from email.header import Header
class Notifier:
def send(self, subject: str, content: str):
pass
class MailNotifier(Notifier):
_from: Header = Header("自动打卡小助手", 'utf-8')
_to: Header = Header("小主", 'utf-8')
def __init__(self, host: str, username: str, password: str, receiver: str):
self._host: str = host
self._user: str = username
self._pass: str = password
self._receiver: str = receiver
def _make_content(self, subject: str, content: str) -> MIMEText:
message = MIMEText(content, 'plain', 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
message['From'] = self._from
message['To'] = self._to
return message
def send(self, subject: str, content: str):
try:
svr = smtplib.SMTP()
svr.connect(self._host)
svr.login(self._user, self._pass)
message = self._make_content(subject, content)
svr.sendmail(self._user, self._receiver, message.as_string())
print("send mail successfully")
except Exception as e:
print("fail to send mail")
print(e)
traceback.print_exc()
class PrintNotifier(Notifier):
def send(self, subject: str, content: str):
print(f'{subject}:\n{content}')