diff --git a/app/__init__.py b/app/__init__.py index c0c1e24..6e84235 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,7 +1,18 @@ -from flask import Flask +from flask import Flask, request, jsonify from .config import Config +from .utils import get_latest_emails def create_app(): app = Flask(__name__) app.config.from_object(Config) + + @app.route('/emails', methods=['GET']) + def get_emails(): + recipient = request.args.get('recipient') + limit = request.args.get('limit', default=10, type=int) + if not recipient: + return jsonify({'error': 'Recipient email is required'}), 400 + emails = get_latest_emails(recipient, limit) + return jsonify(emails) + return app \ No newline at end of file diff --git a/app/utils.py b/app/utils.py index 08aad61..a85fc4d 100644 --- a/app/utils.py +++ b/app/utils.py @@ -158,9 +158,9 @@ class CustomSMTPServer(smtpd.SMTPServer): redis_client.set(time_key, email_key) logger.debug(f"Created time index: {time_key}") - # 设置过期时间(可选,这里设置为30天) - redis_client.expire(email_key, 30 * 24 * 60 * 60) - logger.debug("Set expiration time: 30 days") + # 设置过期时间(可选,这里设置为10分钟) + redis_client.expire(email_key, 10 * 60) + logger.debug("Set expiration time: 10 minutes") except Exception as e: logger.error(f"Error storing email: {str(e)}") @@ -210,4 +210,21 @@ def get_attachment(email_key, attachment_index): return None except Exception as e: print(f'Error fetching attachment: {e}') - return None \ No newline at end of file + return None + + +def get_latest_emails(recipient, limit=10): + """获取指定收件人的最新邮件""" + try: + recipient_key = f'recipient:{recipient}' + email_keys = redis_client.lrange(recipient_key, 0, limit - 1) + emails = [] + for key in email_keys: + email_data = redis_client.hgetall(key.decode()) + if email_data: + email_data = {k.decode(): v.decode() for k, v in email_data.items()} + emails.append(email_data) + return emails + except Exception as e: + logger.error(f'Error fetching emails: {e}') + return [] \ No newline at end of file