#! /usr/bin/python

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


def send_mail(
    from_mail: str,
    smtp_server: str,
    to_mail: list = [],
    subject: str = "",
    msg_html: str = "",
    files_path=None,
):
    # Create message container - the correct MIME type is multipart/alternative.
    msg = MIMEMultipart("alternative")
    msg["Subject"] = subject
    msg["From"] = from_mail
    msg["To"] = ", ".join(to_mail)

    part1 = MIMEText(msg_html, "html")
    msg.attach(part1)

    for file_path in files_path or []:
        with open(file_path, "rb") as fp:
            part = MIMEBase("application", "octet-stream")
            part.set_payload((fp).read())
            # Encoding payload is necessary if encoded (compressed) file has to be attached.
            encoders.encode_base64(part)
            part.add_header(
                "Content-Disposition",
                "attachment; filename= %s" % os.path.basename(file_path),
            )
            msg.attach(part)

    s = smtplib.SMTP(smtp_server)
    s.sendmail(from_mail, to_mail, msg.as_string())
    s.quit()
