Source code for fmn.sender.email
# SPDX-FileCopyrightText: Contributors to the Fedora Project
#
# SPDX-License-Identifier: MIT
import logging
from email.message import EmailMessage
from aiosmtplib import SMTP, SMTPServerDisconnected
from .handler import Handler
log = logging.getLogger(__name__)
[docs]class EmailHandler(Handler):
[docs] async def setup(self):
self._smtp = SMTP(
self._config.get("smtp_host", "localhost"), self._config.get("smtp_port", 25)
)
await self._smtp.connect()
[docs] async def stop(self):
await self._smtp.quit()
[docs] async def handle(self, message):
# Test with `python -m smtpd -c DebuggingServer -n`
notif = EmailMessage()
notif["From"] = self._config["from"]
for name, value in message["headers"].items():
notif[name] = value
notif.set_content(message["body"])
log.info("Sending email to %s with subject %s", notif["To"], notif["Subject"])
try:
await self._smtp.send_message(notif)
except SMTPServerDisconnected:
# Reconnect
await self._smtp.connect()
await self._smtp.send_message(notif)