38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
import logging
|
|
from email.message import EmailMessage
|
|
from asyncio import sleep
|
|
|
|
import aiosmtplib
|
|
|
|
from src.ez_lan_manager.services.ConfigurationService import ConfigurationService
|
|
|
|
logger = logging.getLogger(__name__.split(".")[-1])
|
|
|
|
class MailingService:
|
|
def __init__(self, configuration_service: ConfigurationService):
|
|
self._configuration_service = configuration_service
|
|
self._config = self._configuration_service.get_mailing_service_configuration()
|
|
|
|
async def send_email(self, subject: str, body: str, receiver: str) -> None:
|
|
if self._configuration_service.DEV_MODE_ACTIVE:
|
|
logger.info(f"Skipped sending mail to {receiver} because demo mode is active.")
|
|
await sleep(1)
|
|
return
|
|
|
|
try:
|
|
message = EmailMessage()
|
|
message["From"] = self._config.sender
|
|
message["To"] = receiver
|
|
message["Subject"] = subject
|
|
message.set_content(body)
|
|
|
|
await aiosmtplib.send(
|
|
message,
|
|
hostname=self._config.smtp_server,
|
|
port=self._config.smtp_port,
|
|
username=self._config.username,
|
|
password=self._config.password
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"Failed to send email: {e}")
|