"""
This module provides auxiliary functions for sending e-mails.

Roundabout - git hosting for everyone <https://roundabout-host.com>
Copyright (C) 2023-2025 Roundabout developers <root@roundabout-host.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

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()
