Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')

- 一般情况下第一次都是可以发送成功的,但是有的情况下,可能会被当做垃圾邮件处理,这个就需要用户对邮箱自行进行设置。




```

## 一些说明
```
Expand Down
9 changes: 8 additions & 1 deletion hack12306.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
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
import argparse
import os
import time


class hackTickets(object):
"""docstring for hackTickets"""

Expand Down Expand Up @@ -254,13 +256,18 @@ def confirmSeat(self):
# 若提交订单异常,请适当加大sleep的时间
sleep(1)
print(u"确认选座...")
if self.driver.find_by_text(u"硬座余票<strong>0</strong>张") == None:
if self.driver.find_by_text(u"硬座余票<strong>0</strong>张") != 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()
Expand Down
43 changes: 43 additions & 0 deletions sendEmail.py
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]" # 用户名
mail_pass = "xxx" # 授权密码(非登录密码)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方只支持授权密码吗?如果使用非163邮箱这个授权码就无效了,通用性是不是差了点?


sender = "[email protected]" # 发件人邮箱
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可否支持其他类型邮箱邮件?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该也可以,我只是拿163邮箱举了个栗子。晚些时候我可以试一试。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

晚些时候又注册了一些新的邮箱测试了一下,现总结如下:
(1)使用163,126,QQ邮箱作为发件人邮箱是需要设置授权密码的,而收件人邮箱可以是163,126,QQ,sina,139邮箱中的任何一个;
(2)使用sina邮箱作为发件人邮箱是不需要设置授权密码的,只需要输入邮箱的登录密码即可。而收件人邮箱可以是163,126,QQ,sina,139邮箱中的任何一个,但是QQ邮箱会将sina邮箱发送的邮件当做垃圾邮件接收;
(3)使用139邮箱作为发件人邮箱也是不需要设置授权密码的,但是163,126,QQ,sina邮箱会将拒绝接收邮件,所以会发生退件的情况。
PS:测试完这些邮箱后,我在想完全可以利用python给手机发短信来提醒我们啊!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本人测试得到QQ邮箱不需要授权密码,直接用QQ密码就能登录

receivers = "[email protected]" # 接收邮件

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)