foerderbarometer/input/utils/mail/__init__.py

35 lines
1000 B
Python

from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from input.models import Project
from .attachments import collect_and_attach
__all__ = [
'build_email',
'send_email',
'collect_and_attach',
]
def build_email(template_name: str, context: dict, subject: str, *recipients: str, **kwargs):
body = get_template(f'mails/{template_name}.txt').render(context)
html = get_template(f'mails/{template_name}.html').render(context)
kwargs.setdefault('from_email', settings.IF_EMAIL)
kwargs['subject'] = subject
kwargs['body'] = body
kwargs['to'] = recipients
email = EmailMultiAlternatives(**kwargs)
email.attach_alternative(html, 'text/html')
return email
def send_email(template_name: str, context: dict, subject: str, *recipients: str, fail_silently=False, **kwargs):
return build_email(template_name, context, subject, *recipients, **kwargs).send(fail_silently)