6

I have an email template that I use to send emails of different kinds. I'd rather not keep multiple email HTML templates, so the best way to handle this is to customize the message contents. Like so:

def email_form(request):
    html_message = loader.render_to_string(
            'register/email-template.html',
            {
                'hero': 'email_hero.png',
                'message': 'We\'ll be contacting you shortly! If you have any questions, you can contact us at <a href="#">meow@something.com</a>',
                'from_email': 'lala@lala.com',
            }
        )
    email_subject = 'Thank you for your beeswax!'
    to_list = 'johndoe@whatever.com'
    send_mail(email_subject, 'message', 'from_email', [to_list], fail_silently=False, html_message=html_message)
    return

When the email is sent however, the html codes don't work. The message appears as it is exactly, angled brackets and all. Is there a way for me to force it to render as HTML tags?

Bob
  • 283
  • 1
  • 4
  • 14

3 Answers3

32

Use EmailMessage to do it with less trouble:

First import EmailMessage:

from django.core.mail import EmailMessage

Then use this code to send html email:

email_body = """\
    <html>
      <head></head>
      <body>
        <h2>%s</h2>
        <p>%s</p>
        <h5>%s</h5>
      </body>
    </html>
    """ % (user, message, email)
email = EmailMessage('A new mail!', email_body, to=['someEmail@gmail.com'])
email.content_subtype = "html" # this is the crucial part 
email.send()
Ghasem
  • 14,455
  • 21
  • 138
  • 171
4

Solved it. Not very elegant, but it does work. In case anyone's curious, the variable placed in the email template should be implemented as so:

{{ your_variable|safe|escape }}

Then it works! Thanks guys!

Bob
  • 283
  • 1
  • 4
  • 14
3

You can use EmailMultiAlternatives feature present in django instead of sending mail using send mail. Your code should look like the below snipet.

from django.core.mail import EmailMultiAlternatives

def email_form(request):
    html_message = loader.render_to_string(
            'register/email-template.html',
            {
                'hero': 'email_hero.png',
                'message': 'We\'ll be contacting you shortly! If you have any questions, you can contact us at <a href="#">meow@something.com</a>',
                'from_email': 'lala@lala.com',
            }
        )
    email_subject = 'Thank you for your beeswax!'
    to_list = 'johndoe@whatever.com'
    mail = EmailMultiAlternatives(
            email_subject, 'This is message', 'from_email',  [to_list])
    mail.attach_alternative(html_message, "text/html")
    try:
        mail.send()
    except:
        logger.error("Unable to send mail.")
binu.py
  • 1,137
  • 2
  • 9
  • 20
  • Thanks for responding! I tried implementing your solution, as well as playing around with the view based on Django's own docs. It didn't work unfortunately. The HTML tags still didn't parse. I also tried using "html" instead of "text/html", didn't work either. – Bob Apr 02 '16 at 02:22