This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
2019-10-08 12:30:17 +00:00
|
|
|
"""email utils"""
|
|
|
|
from django.core.mail import EmailMultiAlternatives
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
from django.utils.html import strip_tags
|
|
|
|
|
|
|
|
|
|
|
|
class TemplateEmailMessage(EmailMultiAlternatives):
|
|
|
|
"""Wrapper around EmailMultiAlternatives with integrated template rendering"""
|
|
|
|
|
|
|
|
# pylint: disable=too-many-arguments
|
2019-12-31 11:51:16 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
subject="",
|
|
|
|
body=None,
|
|
|
|
from_email=None,
|
|
|
|
to=None,
|
|
|
|
bcc=None,
|
|
|
|
connection=None,
|
|
|
|
attachments=None,
|
|
|
|
headers=None,
|
|
|
|
cc=None,
|
|
|
|
reply_to=None,
|
|
|
|
template_name=None,
|
|
|
|
template_context=None,
|
|
|
|
):
|
2019-10-08 12:30:17 +00:00
|
|
|
html_content = render_to_string(template_name, template_context)
|
|
|
|
if not body:
|
|
|
|
body = strip_tags(html_content)
|
|
|
|
super().__init__(
|
|
|
|
subject=subject,
|
|
|
|
body=body,
|
|
|
|
from_email=from_email,
|
|
|
|
to=to,
|
|
|
|
bcc=bcc,
|
|
|
|
connection=connection,
|
|
|
|
attachments=attachments,
|
|
|
|
headers=headers,
|
|
|
|
cc=cc,
|
2019-12-31 11:51:16 +00:00
|
|
|
reply_to=reply_to,
|
|
|
|
)
|
2019-10-08 12:30:17 +00:00
|
|
|
self.attach_alternative(html_content, "text/html")
|