feat: 8.0.2.1版本支持QQ邮箱获取验证码
This commit is contained in:
@@ -9,11 +9,12 @@ import json
|
||||
|
||||
|
||||
class EmailVerificationHandler:
|
||||
def __init__(self):
|
||||
def __init__(self, account=None):
|
||||
config = Config()
|
||||
self.imap = config.get_imap()
|
||||
self.username = config.get_temp_mail()
|
||||
self.epin = config.get_temp_mail_epin()
|
||||
self.account = account # 添加account属性,用于存储随机生成的邮箱
|
||||
|
||||
# 创建新的session并设置基本配置
|
||||
self.session = requests.Session()
|
||||
@@ -56,25 +57,38 @@ class EmailVerificationHandler:
|
||||
mail.login(self.imap['imap_user'], self.imap['imap_pass'])
|
||||
mail.select(self.imap['imap_dir'])
|
||||
|
||||
status, messages = mail.search(None, 'FROM', '"no-reply@cursor.sh"')
|
||||
# 使用分开的参数方式搜索,更准确
|
||||
status, messages = mail.search(None, 'TO', f'"{self.account}"', 'FROM', '"no-reply@cursor.sh"')
|
||||
|
||||
if status != 'OK':
|
||||
return None
|
||||
|
||||
mail_ids = messages[0].split()
|
||||
if not mail_ids:
|
||||
# 没有获取到,就在获取一次
|
||||
# 没有获取到,就再获取一次
|
||||
return self._get_mail_code_by_imap(retry=retry + 1)
|
||||
|
||||
latest_mail_id = mail_ids[-1]
|
||||
latest_id = mail_ids[-1]
|
||||
|
||||
# 获取邮件内容
|
||||
status, msg_data = mail.fetch(latest_mail_id, '(RFC822)')
|
||||
status, msg_data = mail.fetch(latest_id, '(RFC822)')
|
||||
if status != 'OK':
|
||||
return None
|
||||
|
||||
raw_email = msg_data[0][1]
|
||||
email_message = email.message_from_bytes(raw_email)
|
||||
|
||||
# 严格验证收件人
|
||||
to_addresses = email_message.get_all('to', [])
|
||||
found_match = False
|
||||
for addr in to_addresses:
|
||||
if self.account.lower() in addr.lower():
|
||||
found_match = True
|
||||
break
|
||||
|
||||
if not found_match:
|
||||
return self._get_mail_code_by_imap(retry=retry + 1)
|
||||
|
||||
# 提取邮件正文
|
||||
body = self._extract_imap_body(email_message)
|
||||
if body:
|
||||
@@ -83,16 +97,13 @@ class EmailVerificationHandler:
|
||||
if code_match:
|
||||
code = code_match.group()
|
||||
# 删除邮件
|
||||
mail.store(latest_mail_id, '+FLAGS', '\\Deleted')
|
||||
mail.store(latest_id, '+FLAGS', '\\Deleted')
|
||||
mail.expunge()
|
||||
mail.logout()
|
||||
# print(f"找到的验证码: {code}")
|
||||
return code
|
||||
# print("未找到验证码")
|
||||
mail.logout()
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"发生错误: {e}")
|
||||
return None
|
||||
|
||||
def _extract_imap_body(self, email_message):
|
||||
@@ -177,6 +188,4 @@ class EmailVerificationHandler:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
email_handler = EmailVerificationHandler()
|
||||
code = email_handler.get_verification_code()
|
||||
print(code)
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user