This commit is contained in:
huangzhenpc
2025-03-05 13:48:45 +08:00
parent 8855475bbb
commit 1ba7434348
2 changed files with 46 additions and 4 deletions

View File

@@ -1,6 +1,9 @@
from flask import Flask, request, jsonify, render_template
from .config import Config
from .utils import get_latest_emails, get_latest_email_with_code, add_allowed_domain, remove_allowed_domain, get_allowed_domains, get_allowed_domains_with_time
from .utils import (get_latest_emails, get_latest_email_with_code,
add_allowed_domain, remove_allowed_domain,
get_allowed_domains, get_allowed_domains_with_time,
get_all_emails)
import redis
def create_app():
@@ -55,8 +58,7 @@ def create_app():
@app.route('/web/list')
def list_emails_page():
try:
# 不指定 recipient获取所有邮件
emails = get_latest_emails(recipient=None, count=50)
emails = get_all_emails(count=50)
return render_template('email_list.html', emails=emails)
except Exception as e:
app.logger.error(f"Error in list_emails_page: {str(e)}")

View File

@@ -302,4 +302,44 @@ def get_allowed_domains_with_time():
return domain_info
except Exception as e:
logger.error(f'Error fetching allowed domains with time: {e}')
return {}
return {}
def get_all_emails(count=50):
"""
获取所有邮件
:param count: 返回的邮件数量限制
:return: 邮件列表
"""
try:
# 获取所有邮件键
pattern = 'email:*'
email_keys = redis_client.keys(pattern)
# 获取每个邮件的详细信息
emails = []
for key in email_keys:
email_data = redis_client.hgetall(key)
if email_data:
# 转换字节数据为字符串
email = {k.decode(): v.decode() for k, v in email_data.items()}
# 解析 JSON 字符串字段
if 'recipients' in email:
email['recipients'] = json.loads(email['recipients'])
if 'attachments' in email:
email['attachments'] = json.loads(email['attachments'])
if 'headers' in email:
email['headers'] = json.loads(email['headers'])
if 'peer' in email:
email['peer'] = json.loads(email['peer'])
emails.append(email)
# 按时间戳排序
emails.sort(key=lambda x: x.get('timestamp', ''), reverse=True)
# 限制返回数量
return emails[:count]
except Exception as e:
logger.error(f"Error getting all emails: {str(e)}")
return []