Skip to content

Mailer

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

send_mail(subject, body, *, destination=None)

Send an email message to the given addresses.

Only supports STARTTLS, not SSL.

Parameters:

Name Type Description Default
subject str

The email subject.

required
body str

The contents of the email message.

required
destination str | None

The destination of the email, optional.

None
Source code in gso/services/mailer.py
def send_mail(subject: str, body: str, *, destination: str | None = None) -> None:
    """Send an email message to the given addresses.

    Only supports STARTTLS, not SSL.

    Args:
        subject: The email subject.
        body: The contents of the email message.
        destination: The destination of the email, optional.
    """
    email_params = load_oss_params().EMAIL
    msg = EmailMessage()
    msg["From"] = email_params.from_address
    msg["To"] = destination or email_params.notification_email_destinations
    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)

        logger.info({
            "event": "Sent an email",
            "from": msg["From"],
            "to": msg["To"],
            "subject": msg["Subject"],
            "body": body,
        })