first commit

This commit is contained in:
zmq
2026-05-26 14:16:16 +08:00
committed by Jerry Yan
commit 535d7f4ac5
40 changed files with 4571 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
from src.notifications.channels.email import EmailNotificationChannel
__all__ = ["EmailNotificationChannel"]
+54
View File
@@ -0,0 +1,54 @@
import asyncio
import smtplib
from email.message import EmailMessage
from email.utils import formataddr
from src.config.settings import Settings
from src.notifications.base import NotificationChannel
from src.notifications.models import NotificationEvent
class EmailNotificationChannel(NotificationChannel):
channel_name = "email"
def __init__(self, settings: Settings) -> None:
self.settings = settings
async def send(self, event: NotificationEvent) -> None:
await asyncio.to_thread(self._send_sync, event)
def _send_sync(self, event: NotificationEvent) -> None:
message = EmailMessage()
from_display = formataddr(
(
self.settings.mail_from_name,
self.settings.mail_from_address or "",
)
)
message["Subject"] = event.title
message["From"] = from_display
message["To"] = ", ".join(self.settings.mail_to_addresses)
message.set_content(event.content_text)
host = self.settings.mail_smtp_host or ""
port = self.settings.mail_smtp_port
timeout = self.settings.mail_timeout_seconds
if self.settings.mail_smtp_use_ssl:
with smtplib.SMTP_SSL(host, port, timeout=timeout) as server:
self._login_and_send(server, message)
return
with smtplib.SMTP(host, port, timeout=timeout) as server:
if self.settings.mail_smtp_starttls:
server.starttls()
self._login_and_send(server, message)
def _login_and_send(self, server: smtplib.SMTP, message: EmailMessage) -> None:
username = self.settings.mail_username or ""
password = self.settings.mail_password or ""
if username:
server.login(username, password)
server.send_message(message)