This commit is contained in:
huangzhenpc
2025-03-05 14:15:13 +08:00
parent 8b29dd701b
commit 3ff56cb5c9

View File

@@ -6,6 +6,7 @@ from .utils import (get_latest_emails, get_latest_email_with_code,
get_all_emails) get_all_emails)
import redis import redis
import json import json
import logging
def create_app(): def create_app():
app = Flask(__name__) app = Flask(__name__)
@@ -60,6 +61,9 @@ def create_app():
def list_emails_page(): def list_emails_page():
try: try:
emails = get_all_emails(count=50) emails = get_all_emails(count=50)
# 检查每个邮件的 message_id
for email in emails:
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)}") app.logger.error(f"Error in list_emails_page: {str(e)}")
@@ -68,23 +72,36 @@ def create_app():
@app.route('/web/email/<message_id>') @app.route('/web/email/<message_id>')
def view_email_page(message_id): def view_email_page(message_id):
try: try:
logger.debug(f"Accessing email with message_id: {message_id}")
# 直接从 Redis 获取邮件数据 # 直接从 Redis 获取邮件数据
email_key = f"email:{message_id}" email_key = f"email:{message_id}"
logger.debug(f"Looking up Redis key: {email_key}")
email_data = redis_client.hgetall(email_key) email_data = redis_client.hgetall(email_key)
if email_data: if not email_data:
# 转换字节数据为字符串 logger.warning(f"No data found for key: {email_key}")
email = {k.decode(): v.decode() for k, v in email_data.items()} return "邮件不存在", 404
# 解析 JSON 字符串字段
for field in ['recipients', 'attachments', 'headers', 'peer']: logger.debug(f"Found email data with fields: {list(email_data.keys())}")
if field in email:
try: # 转换字节数据为字符串
email[field] = json.loads(email[field]) email = {k.decode(): v.decode() for k, v in email_data.items()}
except: logger.debug(f"Decoded email data: {email.keys()}")
pass
return render_template('email_detail.html', email=email) # 解析 JSON 字符串字段
return "邮件不存在", 404 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)
except Exception as e: except Exception as e:
app.logger.error(f"Error in view_email_page: {str(e)}") logger.error(f"Error in view_email_page: {str(e)}", exc_info=True)
return "获取邮件详情失败", 500 return "获取邮件详情失败", 500
return app return app