#!/usr/bin/env python3

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib


class FastMailSMTP(smtplib.SMTP_SSL):
    """A wrapper for handling SMTP connections to FastMail."""

    def __init__(self, username, password):
        super().__init__("mail.messagingengine.com", port=465)
        self._username = username
        self._password = password
        self._has_logged_in = False

    def login(self):
        if not self._has_logged_in:
            super().login(self._username, self._password)
            self._has_logged_in = True

    def send_message(self, *, from_addr, to_addrs, msg, subject, attachments=None):
        self.login()

        msg_root = MIMEMultipart()
        msg_root["Subject"] = subject
        msg_root["From"] = from_addr
        msg_root["To"] = ", ".join(to_addrs)

        msg_alternative = MIMEMultipart("alternative")
        msg_root.attach(msg_alternative)
        msg_alternative.attach(MIMEText(msg, "html"))

        if attachments:
            for attachment in attachments:
                prt = MIMEBase("application", "octet-stream")
                prt.set_payload(open(attachment, "rb").read())
                encoders.encode_base64(prt)
                prt.add_header(
                    "Content-Disposition",
                    'attachment; filename="%s"' % attachment.replace('"', ""),
                )
                msg_root.attach(prt)

        self.sendmail(from_addr, to_addrs, msg_root.as_string())
