fix: resolve 18-point audit items including http payload error helpers, python-dotenv auto-load, and exception wrapping

This commit is contained in:
juanjuanjuanidea
2026-07-24 14:04:39 +08:00
parent e5e1ef3a2d
commit 0e521dc931
5 changed files with 69 additions and 49 deletions
+40 -36
View File
@@ -18,22 +18,27 @@ def get_emails():
proxy_url = os.getenv("PROXY_URL")
messages = []
if acct.method == "graph":
token_res = graph.get_access_token_graph_result(
client_id=acct.client_id,
refresh_token=acct.refresh_token,
proxy_url=proxy_url
)
if isinstance(token_res, dict) and "access_token" in token_res:
token = token_res["access_token"]
messages = graph.fetch_messages(token, top=15, proxy_url=proxy_url)
else:
messages = imap.fetch_inbox_messages(
email=acct.email,
password=acct.password or acct.refresh_token,
limit=15,
proxy_url=proxy_url
)
try:
if acct.method == "graph":
token_res = graph.get_access_token_graph_result(
client_id=acct.client_id,
refresh_token=acct.refresh_token,
proxy_url=proxy_url
)
if isinstance(token_res, dict) and "access_token" in token_res:
token = token_res["access_token"]
messages = graph.fetch_messages(token, top=15, proxy_url=proxy_url)
elif isinstance(token_res, dict) and "error" in token_res:
return jsonify({"error": token_res["error"]}), 400
else:
messages = imap.fetch_inbox_messages(
email=acct.email,
password=acct.password or acct.refresh_token,
limit=15,
proxy_url=proxy_url
)
except Exception as e:
return jsonify({"error": f"Failed to fetch emails: {str(e)}"}), 500
results = []
for msg in messages:
@@ -42,7 +47,7 @@ def get_emails():
results.append({
"id": msg.get("id"),
"subject": msg.get("subject"),
"from": msg.get("from", {}).get("emailAddress", {}).get("address", ""),
"from": msg.get("from", {}).get("emailAddress", {}).get("address", "") if isinstance(msg.get("from"), dict) else str(msg.get("from", "")),
"received_at": msg.get("receivedDateTime"),
"otp_code": code,
"link": link,
@@ -68,21 +73,21 @@ def send_email():
proxy_url = os.getenv("PROXY_URL")
if acct.method == "graph":
token_res = graph.get_access_token_graph_result(
client_id=acct.client_id,
refresh_token=acct.refresh_token,
proxy_url=proxy_url
)
if isinstance(token_res, dict) and "access_token" in token_res:
token = token_res["access_token"]
success = graph.send_mail_graph(token, to_email, subject, body, proxy_url=proxy_url)
if success:
return jsonify({"success": True, "message": "Email sent via Graph API"})
return jsonify({"error": "Graph API send mail failed"}), 500
return jsonify({"error": "Token refresh failed"}), 400
else:
try:
try:
if acct.method == "graph":
token_res = graph.get_access_token_graph_result(
client_id=acct.client_id,
refresh_token=acct.refresh_token,
proxy_url=proxy_url
)
if isinstance(token_res, dict) and "access_token" in token_res:
token = token_res["access_token"]
success = graph.send_mail_graph(token, to_email, subject, body, proxy_url=proxy_url)
if success:
return jsonify({"success": True, "message": "Email sent via Graph API"})
return jsonify({"error": "Graph API send mail failed"}), 500
return jsonify({"error": "Token refresh failed"}), 400
else:
success = smtp.send_mail_smtp(
host=os.getenv("SMTP_HOST", "smtp.office365.com"),
port=int(os.getenv("SMTP_PORT", "587")),
@@ -95,7 +100,6 @@ def send_email():
)
if success:
return jsonify({"success": True, "message": "Email sent via SMTP"})
except Exception as e:
return jsonify({"error": f"SMTP send mail failed: {str(e)}"}), 500
return jsonify({"error": "Send mail failed"}), 500
return jsonify({"error": "SMTP send mail failed"}), 500
except Exception as e:
return jsonify({"error": f"Send mail failed: {str(e)}"}), 500