foerderbarometer/input/utils/mail/__init__.py

35 lines
1000 B
Python
Raw Normal View History

from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
2025-10-17 10:06:23 +00:00
2025-10-17 13:14:16 +00:00
from input.models import Project
2025-10-17 10:06:23 +00:00
2025-10-17 15:31:51 +00:00
from .attachments import collect_and_attach
2025-10-17 10:06:23 +00:00
__all__ = [
2025-10-17 12:51:18 +00:00
'build_email',
'send_email',
2025-10-17 15:31:51 +00:00
'collect_and_attach',
2025-10-17 10:06:23 +00:00
]
2025-10-17 12:51:18 +00:00
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)