-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmailer.py
58 lines (50 loc) · 1.85 KB
/
mailer.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Program of Court Reservation of University of Chinese Academy of Sciences (PCRA)
Copyright © 2022 Alonzo S. LAU . All rights reserved .
@author: Alonzo S. LAU
@email: [email protected]
@info: mailer.py 2022-03-23
"""
import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sendemail(user, pwd, smtp_server, receiver, result):
username = "同学"
if result:
title = "场地预约成功通知"
content = "同学,你好!<br>" \
"预约已完成,请登录中国科学院大学网上办事大厅" \
" https://ehall.ucas.ac.cn/v2/site/ucenter?showroute=myAppointment " \
"提交申请 <br>" \
"祝好"
else:
title = "场地预约失败通知"
content = "同学,你好!<br>" \
"预约失败,请改天再次尝试 <br>" \
"祝好"
receivers = [receiver]
server = smtplib.SMTP_SSL(smtp_server, port=465)
message = MIMEMultipart()
message['From'] = Header('中国科学院大学运动场地预约程序', 'utf-8')
message['To'] = Header(username, 'utf-8')
subject = title
message['Subject'] = Header(subject, 'utf-8')
text = MIMEText(content, 'html', 'utf-8')
message.attach(text)
try:
server.login(user, pwd)
server.sendmail(user, receivers, message.as_string())
server.close()
return True
except smtplib.SMTPRecipientsRefused as e:
print('Recipient refused')
except smtplib.SMTPAuthenticationError as e:
print('Auth error')
except smtplib.SMTPSenderRefused as e:
print('Sender refused')
except smtplib.SMTPException as e:
print(repr(e))
return False