diff --git a/README.md b/README.md index 4a73793..cc4cb61 100644 --- a/README.md +++ b/README.md @@ -230,9 +230,25 @@ driver_name = chrome ### windows路径例如:C:\Users\sanshunfeng\Downloads\chromedriver.exe executable_path = C:\Users\sanshunfeng\Downloads\chromedriver.exe +``` + +## 邮箱配置说明: + +- 发送邮件的邮箱有很多,在本测试中,使用的是163.com的邮箱作为发信人邮箱,并且使用SMTP协议发送邮件。 +- 首先需要在邮箱设置中开启SMTP协议,如下图所示: + +![](./resources/images/协议截图.JPG) + +- 发送邮件成功的前提是已经开通客户端授权,开通后会让你设置密码,那个密码就是下面的passwd接收的授权密码。否则会出现下面的错误提示: + + raise SMTPAuthenticationError(code, resp) + smtplib.SMTPAuthenticationError:(535, b'Error:authentication failed') + +- 一般情况下第一次都是可以发送成功的,但是有的情况下,可能会被当做垃圾邮件处理,这个就需要用户对邮箱自行进行设置。 + + -``` ## 一些说明 ``` diff --git a/hack12306.py b/hack12306.py index 9b2fd2a..e934829 100644 --- a/hack12306.py +++ b/hack12306.py @@ -19,6 +19,7 @@ from splinter.browser import Browser from configparser import ConfigParser from time import sleep +from .sendEmail import send_email import traceback import time, sys import codecs @@ -26,6 +27,7 @@ import os import time + class hackTickets(object): """docstring for hackTickets""" @@ -254,13 +256,18 @@ def confirmSeat(self): # 若提交订单异常,请适当加大sleep的时间 sleep(1) print(u"确认选座...") - if self.driver.find_by_text(u"硬座余票0张") == None: + if self.driver.find_by_text(u"硬座余票0张") != None: self.driver.find_by_id('qr_submit_id').click() + print("预定成功,请及时支付!") + send_email() + else: if self.noseat_allow == 0: self.driver.find_by_id('back_edit_id').click() elif self.noseat_allow == 1: self.driver.find_by_id('qr_submit_id').click() + print("预定成功,请及时支付!") + send_email() def buyTickets(self): t = time.clock() diff --git a/sendEmail.py b/sendEmail.py new file mode 100644 index 0000000..1242efb --- /dev/null +++ b/sendEmail.py @@ -0,0 +1,43 @@ +# -*- coding:utf-8 -*- + +""" +@time: 2018-01-17 +@author: Skyge +""" + +""" +Function: + If book tickets successfully,then send an email to notice you! +""" + +import smtplib +from email.header import Header +from email.mime.text import MIMEText + +mail_host = "smtp.163.com" # SMTP服务器 +mail_user = "xxx@163.com" # 用户名 +mail_pass = "xxx" # 授权密码(非登录密码) + +sender = "xxx@163.com" # 发件人邮箱 +receivers = "xxx@qq.com" # 接收邮件 + +content = "您已成功预定车票,请及时支付完成订单!" # 邮件内容 +title = "12306订票通知" # 邮件主题 + +def send_email(): + + message = MIMEText(content, "plain", "utf-8") # 内容, 格式, 编码 + message["From"] = "{}".format(sender) + message["To"] = ",".join(receivers) + message["Subject"] = title + + try: + smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465 + smtpObj.login(mail_user, mail_pass) # 登录验证 + smtpObj.sendmail(sender, receivers, message.as_string()) # 发送 + print("mail has been send successfully.") + except smtplib.SMTPException as e: + print(e) + + +