You've already forked pruchase_jhw
first commit
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from src.notifications.channels.email import EmailNotificationChannel
|
||||
|
||||
__all__ = ["EmailNotificationChannel"]
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user