Add mailer.py
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
"""Optional SMTP delivery of tenant reports."""
|
||||
import asyncio
|
||||
import smtplib
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
|
||||
from config import Settings
|
||||
|
||||
|
||||
class MailNotConfigured(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
def _send_sync(s: Settings, to: str, subject: str, html: str):
|
||||
msg = MIMEMultipart("alternative")
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = s.smtp_from
|
||||
msg["To"] = to
|
||||
msg.attach(MIMEText(html, "html"))
|
||||
|
||||
with smtplib.SMTP(s.smtp_host, s.smtp_port, timeout=30) as server:
|
||||
if s.smtp_tls:
|
||||
server.starttls()
|
||||
if s.smtp_user:
|
||||
server.login(s.smtp_user, s.smtp_password)
|
||||
server.send_message(msg)
|
||||
|
||||
|
||||
async def send_report(s: Settings, to: str, subject: str, html: str):
|
||||
if not s.smtp_host:
|
||||
raise MailNotConfigured(
|
||||
"SMTP not configured — set BILLING_SMTP_HOST etc. in .env"
|
||||
)
|
||||
await asyncio.to_thread(_send_sync, s, to, subject, html)
|
||||
Reference in New Issue
Block a user