"""The mailer service sends notification emails, as part of workflows that require interaction with external parties."""

import smtplib
from email.message import EmailMessage
from ssl import create_default_context

from gso.settings import load_oss_params


def send_mail(recipient: str, subject: str, body: str) -> None:
    """Send an email message to the given address.

    Only supports STARTTLS, not SSL.

    :param str recipient: The destination address.
    :param str subject: The email subject.
    :param str body: The contents of the email message.
    """
    email_params = load_oss_params().EMAIL
    msg = EmailMessage()
    msg["From"] = email_params.from_address
    msg["To"] = recipient
    msg["Subject"] = subject
    msg.set_content(body)

    with smtplib.SMTP(email_params.smtp_host, email_params.smtp_port) as s:
        if email_params.starttls_enabled:
            tls_context = create_default_context()
            s.starttls(context=tls_context)
        if email_params.smtp_username and email_params.smtp_password:
            s.login(email_params.smtp_username, email_params.smtp_password)
        s.send_message(msg)