test
This commit is contained in:
@@ -1,7 +1,18 @@
|
|||||||
from flask import Flask
|
from flask import Flask, request, jsonify
|
||||||
from .config import Config
|
from .config import Config
|
||||||
|
from .utils import get_latest_emails
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config.from_object(Config)
|
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
|
return app
|
||||||
23
app/utils.py
23
app/utils.py
@@ -158,9 +158,9 @@ class CustomSMTPServer(smtpd.SMTPServer):
|
|||||||
redis_client.set(time_key, email_key)
|
redis_client.set(time_key, email_key)
|
||||||
logger.debug(f"Created time index: {time_key}")
|
logger.debug(f"Created time index: {time_key}")
|
||||||
|
|
||||||
# 设置过期时间(可选,这里设置为30天)
|
# 设置过期时间(可选,这里设置为10分钟)
|
||||||
redis_client.expire(email_key, 30 * 24 * 60 * 60)
|
redis_client.expire(email_key, 10 * 60)
|
||||||
logger.debug("Set expiration time: 30 days")
|
logger.debug("Set expiration time: 10 minutes")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error storing email: {str(e)}")
|
logger.error(f"Error storing email: {str(e)}")
|
||||||
@@ -211,3 +211,20 @@ def get_attachment(email_key, attachment_index):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'Error fetching attachment: {e}')
|
print(f'Error fetching attachment: {e}')
|
||||||
return None
|
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 []
|
||||||
Reference in New Issue
Block a user