Skip to content

Commit

Permalink
Resolved issue #22: Sending HTML emails
Browse files Browse the repository at this point in the history
  • Loading branch information
apragacz committed May 28, 2018
1 parent 0c1f599 commit 8b072f2
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions rest_registration/notifications/email.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.core.mail.message import EmailMessage
from django.template.loader import get_template
from django.core.mail.message import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags

from rest_registration.settings import registration_settings
from rest_registration.utils import get_user_setting
Expand All @@ -9,21 +10,35 @@ def send_verification(user, params_signer, template_config, email=None):
if email is None:
email_field = get_user_setting('EMAIL_FIELD')
email = getattr(user, email_field)
body_template = get_template(template_config['body'])
subject_template = get_template(template_config['subject'])

from_email = registration_settings.VERIFICATION_FROM_EMAIL
reply_to_email = (registration_settings.VERIFICATION_REPLY_TO_EMAIL or
from_email)
ctx = {
context = {
'user': user,
'email': email,
'verification_url': params_signer.get_url(),
}
subject = subject_template.render(ctx).strip()
body = body_template.render(ctx)
subject_template_name = template_config['subject']
subject = render_to_string(subject_template_name, context=context).strip()
body_template_name = template_config.get('body')
text_body_template_name = template_config.get('text_body')
html_body_template_name = template_config.get('html_body')
is_html_body = template_config.get('is_html')

if not html_body_template_name or not text_body_template_name:
assert body_template_name
if is_html_body:
html_body_template_name = body_template_name
text_body_template_name = body_template_name

text_body = strip_tags(
render_to_string(text_body_template_name, context=context))

email_msg = EmailMultiAlternatives(
subject, text_body, from_email, [email], reply_to=[reply_to_email])
if is_html_body:
html_body = render_to_string(html_body_template_name, context=context)
email_msg.attach_alternative(html_body, 'text/html')

email_msg = EmailMessage(
subject, body,
from_email, [email], reply_to=[reply_to_email],
)
email_msg.send()

0 comments on commit 8b072f2

Please sign in to comment.