Skip to content

Commit 7474f58

Browse files
authored
[feature] Made HTML template configurable in send_email
1 parent a6606f4 commit 7474f58

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

docs/developer/admin-theme.rst

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,21 @@ In case the HTML version if not needed it may be disabled by setting
232232
233233
send_email(subject, body_text, body_html, recipients, **kwargs)
234234
235-
================= ==========================================================================================
236-
**Parameter** **Description**
237-
``subject`` (``str``) The subject of the email template.
238-
``body_text`` (``str``) The body of the text message to be emailed.
239-
``body_html`` (``str``) The body of the html template to be emailed.
240-
``recipients`` (``list``) The list of recipients to send the mail to.
241-
``extra_context`` **optional** (``dict``) Extra context which is passed to the template. The dictionary keys
242-
``call_to_action_text`` and ``call_to_action_url`` can be passed to show a call to action
243-
button. Similarly, ``footer`` can be passed to add a footer.
244-
``**kwargs`` Any additional keyword arguments (e.g. ``attachments``, ``headers``, etc.) are passed
245-
directly to the `django.core.mail.EmailMultiAlternatives
246-
<https://docs.djangoproject.com/en/4.1/topics/email/#sending-alternative-content-types>`_.
247-
================= ==========================================================================================
235+
====================== ==========================================================================================
236+
**Parameter** **Description**
237+
``subject`` (``str``) The subject of the email template.
238+
``body_text`` (``str``) The body of the text message to be emailed.
239+
``body_html`` (``str``) The body of the html template to be emailed.
240+
``recipients`` (``list``) The list of recipients to send the mail to.
241+
``extra_context`` **optional** (``dict``) Extra context which is passed to the template. The dictionary keys
242+
``call_to_action_text`` and ``call_to_action_url`` can be passed to show a call to action
243+
button. Similarly, ``footer`` can be passed to add a footer.
244+
``html_body_template`` **(optional, str)** The path to the template used for generating the HTML version. By
245+
default, it uses the template specified in :ref:`openwisp_email_template`.
246+
``**kwargs`` Any additional keyword arguments (e.g. ``attachments``, ``headers``, etc.) are passed
247+
directly to the `django.core.mail.EmailMultiAlternatives
248+
<https://docs.djangoproject.com/en/4.1/topics/email/#sending-alternative-content-types>`_.
249+
====================== ==========================================================================================
248250

249251
.. important::
250252

openwisp_utils/admin_theme/email.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@
1111
logger = logging.getLogger(__name__)
1212

1313

14-
def send_email(subject, body_text, body_html, recipients, extra_context={}, **kwargs):
14+
def send_email(
15+
subject,
16+
body_text,
17+
body_html,
18+
recipients,
19+
extra_context=None,
20+
html_email_template=app_settings.OPENWISP_EMAIL_TEMPLATE,
21+
**kwargs,
22+
):
23+
extra_context = extra_context or {}
1524
mail = EmailMultiAlternatives(
1625
subject=subject,
1726
body=strip_tags(body_text),
1827
from_email=settings.DEFAULT_FROM_EMAIL,
1928
to=recipients,
2029
**kwargs,
2130
)
22-
2331
if app_settings.OPENWISP_HTML_EMAIL and body_html:
2432
context = dict(
2533
subject=subject,
@@ -29,7 +37,7 @@ def send_email(subject, body_text, body_html, recipients, extra_context={}, **kw
2937
context.update(extra_context)
3038

3139
html_message = render_to_string(
32-
app_settings.OPENWISP_EMAIL_TEMPLATE,
40+
html_email_template,
3341
context=context,
3442
)
3543
mail.attach_alternative(html_message, 'text/html')

openwisp_utils/admin_theme/templates/openwisp_utils/email_template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
{% endblock call_to_action %}
9797
{% block mail_footer %}
9898
{% if footer %}
99-
{{ footer }}
99+
{{ footer | safe }}
100100
{% endif %}
101101
{% endblock mail_footer %}
102102
</div>

0 commit comments

Comments
 (0)