forked from testerSunshine/12306
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailConf.py
executable file
·46 lines (40 loc) · 1.3 KB
/
emailConf.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
# -*- coding: utf8 -*-
import socket
__author__ = 'MR.wen'
import TickerConfig
from email.header import Header
from email.mime.text import MIMEText
import smtplib
def sendEmail(msg):
"""
邮件通知
:param str: email content
:return:
"""
try:
sender = TickerConfig.EMAIL_CONF["email"]
receiver = TickerConfig.EMAIL_CONF["notice_email_list"]
subject = '恭喜,您已订票成功'
username = TickerConfig.EMAIL_CONF["username"]
password = TickerConfig.EMAIL_CONF["password"]
host = TickerConfig.EMAIL_CONF["host"]
s = "{0}".format(msg)
msg = MIMEText(s, 'plain', 'utf-8') # 中文需参数‘utf-8’,单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = sender
msg['To'] = receiver
try:
smtp = smtplib.SMTP_SSL(host)
smtp.connect(host)
except socket.error:
smtp = smtplib.SMTP()
smtp.connect(host)
smtp.connect(host)
smtp.login(username, password)
smtp.sendmail(sender, receiver.split(","), msg.as_string())
smtp.quit()
print(u"邮件已通知, 请查收")
except Exception as e:
print(u"邮件配置有误{}".format(e))
if __name__ == '__main__':
sendEmail(1)