Skip to content

Commit b607d93

Browse files
committed
自动登陆邮箱
1 parent b59d476 commit b607d93

5 files changed

Lines changed: 314 additions & 0 deletions

File tree

10.8 MB
Binary file not shown.
10.6 MB
Binary file not shown.
10.7 MB
Binary file not shown.
10.7 MB
Binary file not shown.

自动登陆邮箱/login.py

Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
自动登陆账号邮箱
6+
author: gxcuizy
7+
date: 2021-09-06
8+
"""
9+
10+
from selenium import webdriver
11+
import pymysql
12+
import time
13+
import os
14+
import re
15+
16+
17+
class LoginMail(object):
18+
"""邮箱账号自动登陆"""
19+
20+
def __init__(self):
21+
"""定义实例属性,初始化"""
22+
# 初始化浏览器驱动
23+
self.chrome_option = webdriver.ChromeOptions()
24+
# 关闭左上角的监控提示
25+
self.chrome_option.add_argument("""--no-sandbox""")
26+
self.chrome_option.add_argument("""--disable-gpu""")
27+
self.chrome_option.add_experimental_option('useAutomationExtension', False)
28+
self.chrome_option.add_experimental_option("excludeSwitches", ['enable-automation'])
29+
self.driver = None
30+
# 浏览器驱动
31+
self.chrome_driver = 'driver/chromedriver93.exe'
32+
# 邮箱登陆网站入口
33+
self.gmail_url = 'https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin'
34+
self.ex_mail_url = 'https://exmail.qq.com/login'
35+
self.outlook_url = 'https://login.microsoftonline.com/common/oauth2/authorize?client_id=00000002-0000-0ff1-ce00-000000000000'
36+
self.yahoo_url = 'https://login.yahoo.com/?display=login'
37+
self.qq_url = 'https://mail.qq.com/cgi-bin/loginpage'
38+
self.aliyun_qiye_url = 'https://qiye.aliyun.com/'
39+
self.wangyi_url = 'https://mail.163.com/'
40+
self.wangyi_qiye_url = 'https://qiye.163.com/login/'
41+
self.aliyun_url = 'https://mail.aliyun.com/'
42+
self.kuaiyun_url = 'https://mail.kuaiyunec.com/mail/'
43+
# Mysql数据库链接参数
44+
self.db_host = '127.0.0.1'
45+
self.db_name = 'name'
46+
self.db_user = 'root'
47+
self.db_pw = 'root'
48+
self.db_port = 2701
49+
# 登陆输出信息
50+
self.login_msg = ''
51+
# 输入账号
52+
self.site_code = ''
53+
54+
def get_mail_config(self):
55+
"""获取邮箱配置数据"""
56+
# 创建一个连接
57+
db = pymysql.connect(host=self.db_host, user=self.db_user, password=self.db_pw, db=self.db_name,
58+
port=self.db_port)
59+
# 用cursor()创建一个游标对象
60+
cursor = db.cursor(cursor=pymysql.cursors.DictCursor)
61+
# 查询邮箱配置列表
62+
sql = 'SELECT site_code,cmc_mail_account,cmc_mail_login_pwd,imap_id FROM crm_mail_config WHERE site_code = "' + str(
63+
self.site_code) + '" AND cmc_mail_imap_status = 1 AND mail_type = 1'
64+
cursor.execute(sql)
65+
return cursor.fetchone()
66+
67+
def handle_exception(self, error_msg):
68+
"""异常信息处理"""
69+
try:
70+
error_msg = str(error_msg)
71+
print(error_msg)
72+
match_res = re.match(r'[\s\S]*?Current browser version is (.*) with binary path[\s\S]*?', error_msg)
73+
if match_res:
74+
# 版本问题切换版本重试
75+
version_str = match_res.group(1)
76+
# 分割出版本
77+
version_list = version_str.split(".")
78+
version_num = version_list[0]
79+
self.chrome_driver = 'driver/chromedriver' + str(version_num) + '.exe'
80+
self.run(self.site_code)
81+
except Exception as error_info:
82+
# 异常处理
83+
print(error_info)
84+
85+
def login_gmail(self, account, pwd):
86+
"""登陆谷歌Gmail邮箱"""
87+
try:
88+
self.driver = webdriver.Chrome(executable_path=self.chrome_driver, chrome_options=self.chrome_option)
89+
self.driver.maximize_window()
90+
self.driver.get(self.gmail_url)
91+
time.sleep(1)
92+
# 输入邮箱
93+
self.driver.find_element_by_id("identifierId").send_keys(account)
94+
# 点击下一步
95+
self.driver.find_element_by_class_name("VfPpkd-vQzf8d").click()
96+
time.sleep(3)
97+
# 输入密码
98+
self.driver.find_element_by_xpath("//div[@class='Xb9hP']/input[1]").send_keys(pwd)
99+
# 下一步登陆
100+
self.driver.find_element_by_class_name("VfPpkd-vQzf8d").click()
101+
except Exception as error_info:
102+
# 异常处理
103+
self.handle_exception(error_info)
104+
105+
def login_outlook(self, account, pwd):
106+
"""登陆微软Outlook邮箱"""
107+
try:
108+
self.driver = webdriver.Chrome(executable_path=self.chrome_driver, chrome_options=self.chrome_option)
109+
self.driver.maximize_window()
110+
self.driver.get(self.outlook_url)
111+
time.sleep(1)
112+
# 输入邮箱
113+
self.driver.find_element_by_id("i0116").send_keys(account)
114+
# 点击下一步
115+
self.driver.find_element_by_id("idSIButton9").click()
116+
time.sleep(3)
117+
# 输入密码
118+
self.driver.find_element_by_id("i0118").send_keys(pwd)
119+
# 点击登陆
120+
self.driver.find_element_by_id("idSIButton9").click()
121+
except Exception as error_info:
122+
# 异常处理
123+
self.handle_exception(error_info)
124+
125+
def login_ex_mail(self, account, pwd):
126+
"""登陆腾讯企业邮箱"""
127+
try:
128+
self.driver = webdriver.Chrome(executable_path=self.chrome_driver, chrome_options=self.chrome_option)
129+
self.driver.maximize_window()
130+
self.driver.get(self.ex_mail_url)
131+
time.sleep(1)
132+
# 输入邮箱和密码
133+
self.driver.find_element_by_class_name("js_show_pwd_panel").click()
134+
self.driver.find_element_by_id("inputuin").send_keys(account)
135+
self.driver.find_element_by_id("pp").send_keys(pwd)
136+
# 点击登陆
137+
self.driver.find_element_by_id("btlogin").click()
138+
except Exception as error_info:
139+
# 异常处理
140+
self.handle_exception(error_info)
141+
142+
def login_yahoo(self, account, pwd):
143+
"""登陆雅虎邮箱"""
144+
try:
145+
self.driver = webdriver.Chrome(executable_path=self.chrome_driver, chrome_options=self.chrome_option)
146+
self.driver.maximize_window()
147+
self.driver.get(self.yahoo_url)
148+
time.sleep(1)
149+
# 输入邮箱
150+
self.driver.find_element_by_id("login-username").send_keys(account)
151+
# 点击下一步
152+
self.driver.find_element_by_id("login-signin").click()
153+
except Exception as error_info:
154+
# 异常处理
155+
self.handle_exception(error_info)
156+
157+
def login_qq(self, account, pwd):
158+
"""登陆腾讯企业邮箱"""
159+
try:
160+
self.driver = webdriver.Chrome(executable_path=self.chrome_driver, chrome_options=self.chrome_option)
161+
self.driver.maximize_window()
162+
self.driver.get(self.qq_url)
163+
time.sleep(1)
164+
# 切换到登录框并输入账号密码
165+
self.driver.switch_to.frame('login_frame')
166+
self.driver.find_element_by_name('u').send_keys(account)
167+
self.driver.find_element_by_name('p').send_keys(pwd)
168+
# 点击登陆
169+
self.driver.find_element_by_id('login_button').click()
170+
except Exception as error_info:
171+
# 异常处理
172+
self.handle_exception(error_info)
173+
174+
def login_aliyun_qiye(self, account, pwd):
175+
"""登陆阿里云企业邮箱"""
176+
try:
177+
self.driver = webdriver.Chrome(executable_path=self.chrome_driver, chrome_options=self.chrome_option)
178+
self.driver.maximize_window()
179+
self.driver.get(self.aliyun_qiye_url)
180+
time.sleep(1)
181+
# 切换到登录框并输入账号密码
182+
self.driver.switch_to.frame(self.driver.find_element_by_class_name("login_panel_iframe"))
183+
self.driver.find_element_by_class_name('dingding-mail-login-option-m').click()
184+
self.driver.switch_to.frame('ding-login-iframe')
185+
self.driver.find_element_by_id("username").send_keys(account)
186+
self.driver.find_element_by_id("password").send_keys(pwd)
187+
# 点击登陆
188+
self.driver.find_element_by_id("login_submit_btn").click()
189+
except Exception as error_info:
190+
# 异常处理
191+
self.handle_exception(error_info)
192+
193+
def login_wangyi(self, account, pwd):
194+
"""登陆网易个人邮箱"""
195+
try:
196+
self.driver = webdriver.Chrome(executable_path=self.chrome_driver, chrome_options=self.chrome_option)
197+
self.driver.maximize_window()
198+
self.driver.get(self.wangyi_url)
199+
time.sleep(1)
200+
# 分割出账号
201+
account_list = account.split("@")
202+
account_name = account_list[0]
203+
# 切换到登录框并输入账号密码
204+
self.driver.switch_to.frame(self.driver.find_element_by_xpath("//div[@id='loginDiv']/iframe"))
205+
self.driver.find_element_by_name("email").send_keys(account_name)
206+
self.driver.find_element_by_name("password").send_keys(pwd)
207+
# 点击登陆
208+
self.driver.find_element_by_id("dologin").click()
209+
except Exception as error_info:
210+
# 异常处理
211+
self.handle_exception(error_info)
212+
213+
def login_wangyi_qiye(self, account, pwd):
214+
"""登陆网易企业邮箱"""
215+
try:
216+
self.driver = webdriver.Chrome(executable_path=self.chrome_driver, chrome_options=self.chrome_option)
217+
self.driver.maximize_window()
218+
self.driver.get(self.wangyi_qiye_url)
219+
time.sleep(1)
220+
# 切换到登录框并输入账号密码
221+
self.driver.find_element_by_id("accname").send_keys(account)
222+
self.driver.find_element_by_id("accpwd").send_keys(pwd)
223+
# 点击登陆
224+
self.driver.find_element_by_class_name("w-button-account").click()
225+
except Exception as error_info:
226+
# 异常处理
227+
self.handle_exception(error_info)
228+
229+
def login_aliyun(self, account, pwd):
230+
"""登陆阿里云个人邮箱"""
231+
try:
232+
self.driver = webdriver.Chrome(executable_path=self.chrome_driver, chrome_options=self.chrome_option)
233+
self.driver.maximize_window()
234+
self.driver.get(self.aliyun_url)
235+
time.sleep(1)
236+
# 分割出账号
237+
account_list = account.split("@")
238+
account_name = account_list[0]
239+
# 切换到登录框并输入账号密码
240+
self.driver.switch_to.frame('alibaba-login-box')
241+
self.driver.find_element_by_name("loginId").send_keys(account_name)
242+
self.driver.find_element_by_name("password").send_keys(pwd)
243+
# 点击登陆
244+
self.driver.find_element_by_id("fm-login-submit").click()
245+
except Exception as error_info:
246+
# 异常处理
247+
self.handle_exception(error_info)
248+
249+
def login_kuaiyun(self, account, pwd):
250+
"""登陆快云邮箱"""
251+
try:
252+
self.driver = webdriver.Chrome(executable_path=self.chrome_driver, chrome_options=self.chrome_option)
253+
self.driver.maximize_window()
254+
self.driver.get(self.kuaiyun_url)
255+
time.sleep(1)
256+
# 输入账号密码
257+
self.driver.find_element_by_id("rcmloginuser").send_keys(account)
258+
self.driver.find_element_by_id("rcmloginpwd").send_keys(pwd)
259+
# 点击登陆
260+
self.driver.find_element_by_id("rcmloginsubmit").click()
261+
except Exception as error_info:
262+
# 异常处理
263+
self.handle_exception(error_info)
264+
265+
def run(self, site_code):
266+
"""脚本执行方法"""
267+
self.site_code = site_code.strip()
268+
mail_config = self.get_mail_config()
269+
if mail_config == None:
270+
print('当前账号邮箱配置不存在')
271+
return
272+
self.login_msg = '\n开始登陆邮箱,站点:[' + str(mail_config['site_code']) + '],邮箱:[' + str(
273+
mail_config['cmc_mail_account']) + '],密码:[' + str(mail_config['cmc_mail_login_pwd'] + ']\n')
274+
print(self.login_msg)
275+
if mail_config['imap_id'] == 1:
276+
# Gmail邮箱登陆
277+
self.login_gmail(mail_config['cmc_mail_account'], mail_config['cmc_mail_login_pwd'])
278+
elif mail_config['imap_id'] == 2:
279+
# 腾讯企业邮箱
280+
self.login_ex_mail(mail_config['cmc_mail_account'], mail_config['cmc_mail_login_pwd'])
281+
elif mail_config['imap_id'] == 3:
282+
# 微软邮箱
283+
self.login_outlook(mail_config['cmc_mail_account'], mail_config['cmc_mail_login_pwd'])
284+
elif mail_config['imap_id'] == 4:
285+
# 雅虎邮箱
286+
self.login_yahoo(mail_config['cmc_mail_account'], mail_config['cmc_mail_login_pwd'])
287+
elif mail_config['imap_id'] == 6:
288+
# QQ邮箱
289+
self.login_qq(mail_config['cmc_mail_account'], mail_config['cmc_mail_login_pwd'])
290+
elif mail_config['imap_id'] == 7:
291+
# 阿里云企业邮箱
292+
self.login_aliyun_qiye(mail_config['cmc_mail_account'], mail_config['cmc_mail_login_pwd'])
293+
elif mail_config['imap_id'] == 8:
294+
# 网易个人邮箱
295+
self.login_wangyi(mail_config['cmc_mail_account'], mail_config['cmc_mail_login_pwd'])
296+
elif mail_config['imap_id'] == 9:
297+
# 网易企业邮箱
298+
self.login_wangyi_qiye(mail_config['cmc_mail_account'], mail_config['cmc_mail_login_pwd'])
299+
elif mail_config['imap_id'] == 10:
300+
# 阿里云个人邮箱
301+
self.login_aliyun(mail_config['cmc_mail_account'], mail_config['cmc_mail_login_pwd'])
302+
elif mail_config['imap_id'] == 11:
303+
# 快云邮箱
304+
self.login_kuaiyun(mail_config['cmc_mail_account'], mail_config['cmc_mail_login_pwd'])
305+
else:
306+
print('暂不支持该类型邮箱登陆')
307+
308+
309+
# 程序主入口
310+
if __name__ == "__main__":
311+
obj = LoginMail()
312+
input_code = input('请输入您需要登陆的站点(然后回车执行):')
313+
obj.run(input_code)
314+
os.system('pause')

0 commit comments

Comments
 (0)