12306/config/emailConf.py

48 lines
1.5 KiB
Python
Raw Normal View History

# -*- coding: utf8 -*-
2019-01-16 03:19:32 +00:00
import socket
__author__ = 'MR.wen'
2019-09-01 04:41:25 +00:00
import TickerConfig
from email.header import Header
from email.mime.text import MIMEText
import smtplib
def sendEmail(msg):
"""
邮件通知
:param str: email content
:return:
"""
2019-09-01 04:41:25 +00:00
try:
2019-10-09 05:46:48 +00:00
if TickerConfig.EMAIL_CONF["IS_MAIL"]:
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"]
2019-11-08 05:20:20 +00:00
port = TickerConfig.EMAIL_CONF["port"]
2019-10-09 05:46:48 +00:00
s = "{0}".format(msg)
2019-10-09 05:46:48 +00:00
msg = MIMEText(s, 'plain', 'utf-8') # 中文需参数utf-8单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = sender
msg['To'] = receiver
2019-10-09 05:46:48 +00:00
try:
2019-11-08 05:20:20 +00:00
smtp = smtplib.SMTP_SSL(host, port)
smtp.connect(host, port)
2019-10-09 05:46:48 +00:00
except socket.error:
smtp = smtplib.SMTP()
2019-11-08 05:20:20 +00:00
smtp.connect(host, port)
smtp.connect(host, port)
2019-10-09 05:46:48 +00:00
smtp.login(username, password)
smtp.sendmail(sender, receiver.split(","), msg.as_string())
smtp.quit()
print(u"邮件已通知, 请查收")
2019-09-01 04:41:25 +00:00
except Exception as e:
print(u"邮件配置有误{}".format(e))
if __name__ == '__main__':
sendEmail(1)