import os

from anymail.message import attach_inline_image_file
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string


def send_mail(template_html_name=None, template_text_name=None, context=None, subject=None, from_email=None, to=None):
    # Plain text part
    text_content = render_to_string(template_text_name, context=context)

    if from_email is None:
        from_email = settings.APP_SETTINGS['site']['noreply_email']

    msg = EmailMultiAlternatives(
        subject=subject,
        body=text_content,
        from_email=from_email,
        to=[to])

    # Making context
    logo_cid = attach_inline_image_file(msg, os.path.join(settings.BASE_DIR, "assets/img/logo_mail.png"))
    context.update({
        'logo_cid': logo_cid,
        'app': settings.APP_SETTINGS
    })

    # HTML part
    html_content = render_to_string(template_html_name, context=context)

    msg.attach_alternative(html_content, "text/html")
    msg.send()
