This commit is contained in:
huangzhenpc
2025-03-05 14:22:00 +08:00
parent 3ff56cb5c9
commit d52ad90936
2 changed files with 48 additions and 25 deletions

View File

@@ -3,11 +3,21 @@ from .config import Config
from .utils import (get_latest_emails, get_latest_email_with_code, from .utils import (get_latest_emails, get_latest_email_with_code,
add_allowed_domain, remove_allowed_domain, add_allowed_domain, remove_allowed_domain,
get_allowed_domains, get_allowed_domains_with_time, get_allowed_domains, get_allowed_domains_with_time,
get_all_emails) get_all_emails, get_email_by_id)
import redis import redis
import json import json
import logging import logging
# 配置日志
logger = logging.getLogger('app')
logger.setLevel(logging.DEBUG)
# 添加控制台处理器
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
))
logger.addHandler(handler)
def create_app(): def create_app():
app = Flask(__name__) app = Flask(__name__)
app.config.from_object(Config) app.config.from_object(Config)
@@ -66,7 +76,7 @@ def create_app():
logger.debug(f"Email message_id: {email.get('message_id')}") logger.debug(f"Email message_id: {email.get('message_id')}")
return render_template('email_list.html', emails=emails) return render_template('email_list.html', emails=emails)
except Exception as e: except Exception as e:
app.logger.error(f"Error in list_emails_page: {str(e)}") logger.error(f"Error in list_emails_page: {str(e)}")
return "获取邮件列表失败", 500 return "获取邮件列表失败", 500
@app.route('/web/email/<message_id>') @app.route('/web/email/<message_id>')
@@ -74,30 +84,11 @@ def create_app():
try: try:
logger.debug(f"Accessing email with message_id: {message_id}") logger.debug(f"Accessing email with message_id: {message_id}")
# 直接从 Redis 获取邮件数据 # 使用新函数获取邮件数据
email_key = f"email:{message_id}" email = get_email_by_id(message_id)
logger.debug(f"Looking up Redis key: {email_key}") if not email:
email_data = redis_client.hgetall(email_key)
if not email_data:
logger.warning(f"No data found for key: {email_key}")
return "邮件不存在", 404 return "邮件不存在", 404
logger.debug(f"Found email data with fields: {list(email_data.keys())}")
# 转换字节数据为字符串
email = {k.decode(): v.decode() for k, v in email_data.items()}
logger.debug(f"Decoded email data: {email.keys()}")
# 解析 JSON 字符串字段
for field in ['recipients', 'attachments', 'headers', 'peer']:
if field in email:
try:
email[field] = json.loads(email[field])
logger.debug(f"Successfully parsed {field} field")
except Exception as e:
logger.warning(f"Failed to parse {field} field: {str(e)}")
return render_template('email_detail.html', email=email) return render_template('email_detail.html', email=email)
except Exception as e: except Exception as e:

View File

@@ -355,3 +355,35 @@ def get_all_emails(count=50):
except Exception as e: except Exception as e:
logger.error(f"Error getting all emails: {str(e)}", exc_info=True) logger.error(f"Error getting all emails: {str(e)}", exc_info=True)
return [] return []
def get_email_by_id(message_id):
"""
根据 message_id 获取邮件数据
:param message_id: 邮件的唯一标识
:return: 邮件数据字典或 None
"""
try:
email_key = f"email:{message_id}"
email_data = redis_client.hgetall(email_key)
if not email_data:
logger.warning(f"No data found for key: {email_key}")
return None
# 转换字节数据为字符串
email = {k.decode(): v.decode() for k, v in email_data.items()}
# 解析 JSON 字符串字段
for field in ['recipients', 'attachments', 'headers', 'peer']:
if field in email:
try:
email[field] = json.loads(email[field])
logger.debug(f"Successfully parsed {field} field")
except Exception as e:
logger.warning(f"Failed to parse {field} field: {str(e)}")
return email
except Exception as e:
logger.error(f"Error getting email by ID {message_id}: {str(e)}")
return None