import config
from jinja2 import Environment, FileSystemLoader, select_autoescape
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


def make_multipart_message(subject, sender, receiver, template, **kwargs):
    env_html = Environment(
        loader=FileSystemLoader("email_templates"),
        autoescape=select_autoescape(["html", "xml"])
    )
    env_plain = Environment(loader=FileSystemLoader("email_templates"))

    message = MIMEMultipart("alternative")
    message["Subject"] = subject
    message["From"] = sender
    message["To"] = receiver

    text = MIMEText(env_html.get_template(template+".txt").render(**kwargs, config=config), "plain")
    html = MIMEText(env_html.get_template(template+".html").render(**kwargs, config=config), "html")

    message.attach(text)
    message.attach(html)

    return message.as_string()
