email_delivery.py 1.1 KB

123456789101112131415161718192021222324252627282930
  1. """Small SMTP binding for work-center email delivery."""
  2. from __future__ import annotations
  3. import os
  4. import smtplib
  5. from email.message import EmailMessage
  6. def smtp_sender(notification: dict) -> None:
  7. """Deliver one notification; configuration failures remain retryable evidence."""
  8. host = os.getenv("DATAOPS_SMTP_HOST")
  9. sender = os.getenv("DATAOPS_SMTP_FROM")
  10. recipient = notification.get("recipient_email")
  11. if not host or not sender or not recipient:
  12. raise RuntimeError("SMTP binding is incomplete")
  13. port = int(os.getenv("DATAOPS_SMTP_PORT", "587"))
  14. message = EmailMessage()
  15. message["From"] = sender
  16. message["To"] = recipient
  17. message["Subject"] = notification["subject"]
  18. message.set_content(notification["body"])
  19. with smtplib.SMTP(host, port, timeout=10) as client:
  20. if os.getenv("DATAOPS_SMTP_STARTTLS", "true").lower() == "true":
  21. client.starttls()
  22. username = os.getenv("DATAOPS_SMTP_USERNAME")
  23. password = os.getenv("DATAOPS_SMTP_PASSWORD")
  24. if username and password:
  25. client.login(username, password)
  26. client.send_message(message)