test
This commit is contained in:
29
app/utils.py
29
app/utils.py
@@ -227,4 +227,31 @@ def get_latest_emails(recipient, limit=10):
|
||||
return emails
|
||||
except Exception as e:
|
||||
logger.error(f'Error fetching emails: {e}')
|
||||
return []
|
||||
return []
|
||||
|
||||
|
||||
def get_latest_email_with_code(recipient):
|
||||
"""获取指定收件人的最新邮件并提取验证码"""
|
||||
try:
|
||||
recipient_key = f'recipient:{recipient}'
|
||||
email_key = redis_client.lindex(recipient_key, 0) # 获取最新邮件的键
|
||||
if email_key:
|
||||
email_data = redis_client.hgetall(email_key.decode())
|
||||
if email_data:
|
||||
email_data = {k.decode(): v.decode() for k, v in email_data.items()}
|
||||
body = email_data.get('body', '')
|
||||
# 假设验证码是以某种格式存在于邮件正文中,例如 "验证码: 123456"
|
||||
code = extract_code_from_body(body)
|
||||
email_data['code'] = code # 将验证码添加到返回数据中
|
||||
return email_data
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f'Error fetching latest email with code: {e}')
|
||||
return None
|
||||
|
||||
|
||||
def extract_code_from_body(body):
|
||||
"""从邮件正文中提取验证码"""
|
||||
import re
|
||||
match = re.search(r'\b(\d{6})\b', body)
|
||||
return match.group(1) if match else None
|
||||
Reference in New Issue
Block a user