'''

usage:

    m = Mail("mail-auto@bas-rhin.fr", ["destinataire@bas-rhin.fr"], "mon mail", "mon message")
    m.send()

'''
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib


class Mail():
    HOST = ""

    def __init__(self, fromaddr, toaddrs, subject, msg):
        self.mailport = None
        self.fromaddr = fromaddr
        self.toaddrs = toaddrs
        self.subject = subject
        self.msg = msg

        self.mail = MIMEMultipart('alternative')
        self.mail['Subject'] = self.subject
        self.mail['From'] = self.fromaddr
        self.mail['To'] = ",".join(self.toaddrs)

        self.mail.attach(MIMEText(self.msg, 'html', _charset='utf-8'))

    def send(self):
        if not self.HOST:
            raise ValueError("Mail.HOST has to be defined before sending any mail")
        
        port = self.mailport if self.mailport else smtplib.SMTP_PORT

        with smtplib.SMTP(self.HOST, port) as smtp:
            smtp.sendmail(self.fromaddr, self.toaddrs, self.mail.as_string())


if __name__ == "__main__":
    mail = Mail("",
                ["olinox14@yahoo.fr"],
                "test",
                "test")
    mail.send()
