From Django 1.7, it is possible to send_email()
with a new parameter: html_message
. Unfortunately, there is not a comprehensive guide (newby friendly) on how to use it (or at least I could not find it).
I need to make sent emails nice and, thus, I'm trying to figure out how to include my message into a html/css template. I've read many posts about this subject, but most of them refer to previous version of Django. This post is for Django 1.7 but the answer shows the usage of a very basic html file.
my_html_message.html
<html lang="en">
<head>
<link href="path/to/application.css" rel="stylesheet">
</head>
<body>
<div class="email_header">
[..]
<a href="http://mysite.it"><img src="images/logo.png"/></a>
</div>
<div class="email_body">
[..]
{{my_message}}
</div>
</body>
my_message.txt
Dear {{username}},
you won {{money}}.
my_function.py
def my_send_email(request):
subject = 'Test html email'
from = 'my@email.com'
to = 'yours@email.com'
my_context = Context({'username': user.username,
'money': user.money})
html_message = render_to_string('mail/my_html_message.html', c)
message = render_to_string('mail/my_message.txt', c)
send_mail(subject,
message,
from,
[to],
html_message=html_message)
How can I pass to my_html_message.html
the file my_message.txt
with my_context
and send an email with a custom style?