Skip to content

Commit

Permalink
Add email body_html template (django-otp#112)
Browse files Browse the repository at this point in the history
- Add the requested tests for the linked pull request
  • Loading branch information
Ephraim Schnitzler committed Jun 12, 2023
1 parent ac0cbab commit 8e6ee73
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
30 changes: 30 additions & 0 deletions docs/source/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,21 @@ If this and :setting:`OTP_EMAIL_BODY_TEMPLATE_PATH` are not set, we'll render
the template 'otp/email/token.txt', which you'll most likely want to override.


.. setting:: OTP_EMAIL_BODY_HTML_TEMPLATE

**OTP_EMAIL_BODY_HTML_TEMPLATE**

Default: ``None``

A raw template string to use for the email html alternative body. The render context will
include the generated token in the ``token`` key. Additional template context
may be passed to
:meth:`~django_otp.plugins.otp_email.models.EmailDevice.generate_challenge`.

If this and :setting:`OTP_EMAIL_BODY_HTML_TEMPLATE_PATH` are not set, we won't attach any
html alternative to the email.


.. setting:: OTP_EMAIL_BODY_TEMPLATE_PATH

**OTP_EMAIL_BODY_TEMPLATE_PATH**
Expand All @@ -448,6 +463,21 @@ If this and :setting:`OTP_EMAIL_BODY_TEMPLATE` are not set, we'll render the
template 'otp/email/token.txt', which you'll most likely want to override.


.. setting:: OTP_EMAIL_BODY_HTML_TEMPLATE_PATH

**OTP_EMAIL_BODY_HTML_TEMPLATE_PATH**

Default: ``None``

A path string to a template file to use for the email html alternative body. The render context
will include the generated token in the ``token`` key. Additional template
context may be passed to
:meth:`~django_otp.plugins.otp_email.models.EmailDevice.generate_challenge`.

If this and :setting:`OTP_EMAIL_BODY_HTML_TEMPLATE` are not set, we won't attach any html
alternative to the email.


.. setting:: OTP_EMAIL_TOKEN_VALIDITY

**OTP_EMAIL_TOKEN_VALIDITY**
Expand Down
2 changes: 2 additions & 0 deletions src/django_otp/plugins/otp_email/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class OTPEmailSettings:
'OTP_EMAIL_SUBJECT': 'OTP token',
'OTP_EMAIL_BODY_TEMPLATE': None,
'OTP_EMAIL_BODY_TEMPLATE_PATH': 'otp/email/token.txt',
'OTP_EMAIL_BODY_HTML_TEMPLATE': None,
'OTP_EMAIL_BODY_HTML_TEMPLATE_PATH': None,
'OTP_EMAIL_TOKEN_VALIDITY': 300,
'OTP_EMAIL_THROTTLE_FACTOR': 1,
}
Expand Down
18 changes: 12 additions & 6 deletions src/django_otp/plugins/otp_email/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,18 @@ def generate_challenge(self, extra_context=None):
else:
body = get_template(settings.OTP_EMAIL_BODY_TEMPLATE_PATH).render(context)

send_mail(
str(settings.OTP_EMAIL_SUBJECT),
body,
settings.OTP_EMAIL_SENDER,
[self.email or self.user.email],
)
if settings.OTP_EMAIL_BODY_HTML_TEMPLATE:
body_html = Template(settings.OTP_EMAIL_BODY_HTML_TEMPLATE).render(Context(context))
elif settings.OTP_EMAIL_BODY_HTML_TEMPLATE_PATH:
body_html = get_template(settings.OTP_EMAIL_BODY_HTML_TEMPLATE_PATH).render(context)
else:
body_html = None

send_mail(settings.OTP_EMAIL_SUBJECT,
body,
settings.OTP_EMAIL_SENDER,
[self.email or self.user.email],
html_message=body_html)

message = "sent by email"

Expand Down

0 comments on commit 8e6ee73

Please sign in to comment.