Django 1.7:如何使用html / css文件作为模板发送电子邮件 [英] Django 1.7: how to send emails by using an html/css file as template
问题描述
从Django 1.7开始,可以使用新参数 html_message
send_email()
。不幸的是,关于如何使用它(或者至少我找不到它),没有一个全面的指南(newby friendly)。
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).
我需要使发送的电子邮件更好,因此,我试图弄清楚如何将邮件包含到html / css模板中。
我已经阅读了许多有关此主题的文章,但其中大多数是关于Django的早期版本的。 这篇帖子适用于Django 1.7,但答案显示了用法一个非常基本的html文件。
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)
如何传递给 my_html_message。 html
文件 my_message.txt
和 my_context
并发送具有自定义样式的电子邮件?
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?
推荐答案
使用 包括 :
With include:
<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">
[..]
{% include "my_message.txt" %}
</div>
</body>
这篇关于Django 1.7:如何使用html / css文件作为模板发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!