-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail.py
41 lines (41 loc) · 1.45 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
#!/usr/bin/python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from log import Log
from config import mail_host,mail_user,mail_passwd,use_ssl,ssl_port,receivers
def send_email(new_grade):
logger=Log(__name__).getlog()
try:
if(mail_host=='' or mail_user=='' or mail_passwd == ''):
raise Exception('Please input sender information!!!')
if(receivers ==[]):
raise Exception('Please input receiver information!!!')
except Exception as e:
logger.info('Exception: '+str(e))
return
if use_ssl:
ssl_address=mail_host+':'+str(ssl_port)
grade_len=len(new_grade)
text='新成绩:\n'
for i in range(grade_len):
text+=str(new_grade[i][0])+' : '+str(new_grade[i][1])+'\n'
message = MIMEText(text,'plain','utf-8')
subject = '您有新成绩!'
message['Subject']=Header(subject,'utf-8')
message['From']=Header(mail_user,'utf-8')
message['To']=Header(",".join(receivers),'utf-8')
try:
if use_ssl:
smtp=smtplib.SMTP_SSL(ssl_address)
else:
smtp=smtplib.SMTP()
smtp.connect(mail_host,25)
smtp.login(mail_user,mail_passwd)
smtp.sendmail(mail_user,receivers,message.as_string())
smtp.quit()
except smtplib.SMTPException:
logger.info("Error: 无法发送邮件")
return
logger.info('Email has been sent successfully!')