mirror of
https://github.com/mskatoni/ni-mail.git
synced 2026-08-29 12:07:24 +08:00
102 lines
3.6 KiB
Python
102 lines
3.6 KiB
Python
from flask import Blueprint, jsonify, request
|
|
from app.models import Account
|
|
from app.services import graph, imap, smtp, verification
|
|
import os
|
|
|
|
bp = Blueprint("emails", __name__, url_prefix="/api/emails")
|
|
|
|
@bp.get("/")
|
|
def get_emails():
|
|
email = request.args.get("email")
|
|
if not email:
|
|
return jsonify({"error": "email query param required"}), 400
|
|
|
|
acct = Account.query.filter_by(email=email).first()
|
|
if not acct:
|
|
return jsonify({"error": "account not found"}), 404
|
|
|
|
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
|
|
)
|
|
|
|
results = []
|
|
for msg in messages:
|
|
body = msg.get("body", {}).get("content", "") or msg.get("body_text", "") or ""
|
|
code, link = verification.extract_code_and_link(body)
|
|
results.append({
|
|
"id": msg.get("id"),
|
|
"subject": msg.get("subject"),
|
|
"from": msg.get("from", {}).get("emailAddress", {}).get("address", ""),
|
|
"received_at": msg.get("receivedDateTime"),
|
|
"otp_code": code,
|
|
"link": link,
|
|
"body_preview": body[:300]
|
|
})
|
|
|
|
return jsonify(results)
|
|
|
|
@bp.post("/send")
|
|
def send_email():
|
|
data = request.json or {}
|
|
email = data.get("email")
|
|
to_email = data.get("to_email")
|
|
subject = data.get("subject", "No Subject")
|
|
body = data.get("body", "")
|
|
|
|
if not email or not to_email:
|
|
return jsonify({"error": "email and to_email required"}), 400
|
|
|
|
acct = Account.query.filter_by(email=email).first()
|
|
if not acct:
|
|
return jsonify({"error": "account not found"}), 404
|
|
|
|
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:
|
|
success = smtp.send_mail_smtp(
|
|
host=os.getenv("SMTP_HOST", "smtp.office365.com"),
|
|
port=int(os.getenv("SMTP_PORT", "587")),
|
|
username=acct.email,
|
|
password=acct.password or acct.refresh_token,
|
|
to_email=to_email,
|
|
subject=subject,
|
|
body_text=body,
|
|
use_tls=True
|
|
)
|
|
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
|