mail config 적용 .

This commit is contained in:
HyeongBaeYang committed 2026-06-11 09:21:10 +09:00
1 parent dc50fc6d8c
commit 7185cfbb72
4 files changed
+56 -10

No files matched your search

+33 -8
View File
@@ -1,6 +1,8 @@
"""결과 이메일 발송 — SMTP 실연동 (aiosmtplib).
"""결과 이메일 발송.
SMTP 설정이 없으면 EmailUnavailable 을 던진다(조용한 실패 0).
1순위: Azure Communication Services(ACS) Email (endpoint + accesskey).
2순위(폴백): SMTP (aiosmtplib).
둘 다 미설정이면 EmailUnavailable 을 던진다(조용한 실패 0).
경기 종료 후 워커가 구독자(notify=True + email)에게 개인화 결과 메일 발송.
"""
from __future__ import annotations
@@ -17,13 +19,23 @@ class EmailUnavailable(RuntimeError):
pass
def _ensure_configured() -> None:
if not settings.smtp_host:
raise EmailUnavailable("SMTP_HOST 미설정")
async def _send_acs(to: str, subject: str, html: str, text: str) -> None:
"""Azure Communication Services Email — 키(accesskey) 인증."""
from azure.communication.email.aio import EmailClient
conn = f"endpoint={settings.azure_acs_endpoint};accesskey={settings.azure_acs_accesskey}"
message = {
"senderAddress": settings.azure_acs_sender,
"recipients": {"to": [{"address": to}]},
"content": {"subject": subject, "plainText": text, "html": html},
}
async with EmailClient.from_connection_string(conn) as client:
poller = await client.begin_send(message)
await poller.result()
log.info("email sent via ACS → %s (%s)", to, subject)
async def send_email(to: str, subject: str, html: str, text: str) -> None:
_ensure_configured()
async def _send_smtp(to: str, subject: str, html: str, text: str) -> None:
import aiosmtplib
msg = EmailMessage()
@@ -41,7 +53,20 @@ async def send_email(to: str, subject: str, html: str, text: str) -> None:
password=settings.smtp_password or None,
start_tls=settings.smtp_starttls,
)
log.info("email sent → %s (%s)", to, subject)
log.info("email sent via SMTP → %s (%s)", to, subject)
async def send_email(to: str, subject: str, html: str, text: str) -> None:
if settings.acs_configured:
await _send_acs(to, subject, html, text)
return
if settings.smtp_host:
await _send_smtp(to, subject, html, text)
return
# ACS endpoint/accesskey 만 있고 sender 누락 시 명확히 안내
if settings.azure_acs_endpoint and not settings.azure_acs_sender:
raise EmailUnavailable("AZURE_ACS_SENDER(검증된 MailFrom 주소) 미설정")
raise EmailUnavailable("이메일 미설정 — ACS(endpoint+accesskey+sender) 또는 SMTP 필요")
def build_result_email(