1

How can I call a template instead of plain HTML to send email with django core email?

My views:

to = articles.user.email
            html_content = '<p>This is your email content...</p>'
            msg = EmailMultiAlternatives('You have an email',html_content,'from@server.com',[to])
            msg.attach_alternative(html_content,'text/html')
            msg.send()

In the variable html_content you can see is plain HTML, how can I call here a template? This template would be the content of the email.

Steve Tjoa
  • 59,122
  • 18
  • 90
  • 101
dfrojas
  • 673
  • 1
  • 13
  • 32
  • possible duplicate of [Creating email templates with Django](http://stackoverflow.com/questions/2809547/creating-email-templates-with-django) – John Mee May 10 '15 at 02:13

1 Answers1

1

You can use the template loader get_template function to load your template:

from django.template import Context
from django.template.loader import get_template

my_context = {}
html_content = get_template('mytemplate.html').render(Context(my_context))
Reto Aebersold
  • 16,306
  • 5
  • 55
  • 74