chore: include authentication services

This commit is contained in:
2026-08-03 18:49:16 +08:00
parent f2209a0e00
commit 630252e2ff
2 changed files with 61 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
from __future__ import annotations
import logging
from typing import Any
import resend
from flask import current_app
class EmailConfigurationError(RuntimeError):
"""Raised when transactional email has not been configured."""
class EmailDeliveryError(RuntimeError):
"""Raised when Resend rejects or cannot deliver an email request."""
def send_transactional_email(*, to: str, subject: str, html: str) -> dict[str, Any]:
"""Send one application-generated email through Resend."""
api_key = current_app.config["RESEND_API_KEY"]
from_email = current_app.config["RESEND_FROM_EMAIL"]
if not api_key or not from_email:
raise EmailConfigurationError(
"邮件服务尚未配置,请设置 RESEND_API_KEY 和 RESEND_FROM_EMAIL。"
)
resend.api_key = api_key
try:
return resend.Emails.send(
{
"from": from_email,
"to": [to],
"subject": subject,
"html": html,
}
)
except Exception as exc:
logging.exception("Resend 邮件发送失败: %s", exc)
raise EmailDeliveryError("邮件发送失败,请稍后重试。") from exc