使用 Django 创建电子邮件模板
我想使用 Django 模板发送 HTML 电子邮件,如下所示:
<html>
<body>
hello <strong>{{username}}</strong>
your account activated.
<img src="mysite.com/logo.gif" />
</body>
我找不到有关 send_mail
的任何内容,并且 django-mailer 只发送 HTML 模板,没有动态数据。
如何使用 Django 的模板引擎生成电子邮件?
I want to send HTML-emails, using Django templates like this:
<html>
<body>
hello <strong>{{username}}</strong>
your account activated.
<img src="mysite.com/logo.gif" />
</body>
I can't find anything about send_mail
, and django-mailer only sends HTML templates, without dynamic data.
How do I use Django's template engine to generate e-mails?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
从 文档,发送 HTML e -mail 您想要使用替代内容类型,如下所示:
您可能需要两个电子邮件模板 - 一个看起来像这样的纯文本模板,存储在
email.txt< 下的模板目录中/code>:
和一个 HTMLy 模板,存储在
email.html
下:然后您可以通过使用
get_template
,如下所示:From the docs, to send HTML e-mail you want to use alternative content-types, like this:
You'll probably want two templates for your e-mail - a plain text one that looks something like this, stored in your templates directory under
email.txt
:and an HTMLy one, stored under
email.html
:You can then send an e-mail using both those templates by making use of
get_template
, like this:从 Django 1.7 开始, send_email 方法中的
html_message<添加了 /code> 参数。
所以你可以:
Since Django's 1.7 in send_email method the
html_message
parameter was added.So you can just:
受此启发,我制作了 django-templated-email 来解决这个问题解决方案(并且需要在某些时候从使用 django 模板切换到使用 mailchimp 等一组用于我自己项目的事务性、模板化电子邮件的模板)。虽然它仍然是一个正在进行的工作,但对于上面的示例,您可以这样做:
在 settings.py 中添加以下内容(以完成示例):
这将自动查找名为“templated_email/email.txt”的模板。 txt' 和 'templated_email/email.html' 分别用于普通 django 模板目录/加载器中的纯文本和 html 部分(如果找不到至少其中之一,则会抱怨)。
I have made django-templated-email in an effort to solve this problem, inspired by this solution (and the need to, at some point, switch from using django templates to using a mailchimp etc. set of templates for transactional, templated emails for my own project). It is still a work-in-progress though, but for the example above, you would do:
With the addition of the following to settings.py (to complete the example):
This will automatically look for templates named 'templated_email/email.txt' and 'templated_email/email.html' for the plain and html parts respectively, in the normal django template dirs/loaders (complaining if it cannot find at least one of those).
我知道这是一个老问题,但我也知道有些人就像我一样,总是在寻找最新的答案,因为旧的答案如果不更新,有时可能会包含已弃用的信息。
现在是 2020 年 1 月,我正在使用 Django 2.2.6 和 Python 3.7
注意:我使用 DJANGO REST FRAMEWORK,下面用于发送电子邮件的代码位于 model viewset 在我的
views.py
中,所以在阅读了多个不错的答案后,这就是我所做的。
重要! 那么
render_to_string
如何获取receipt_email.txt
和receipt_email.html
呢?在我的
settings.py
中,我有TEMPLATES
,下面是它的样子注意
DIRS
,有这一行os. path.join(BASE_DIR, 'templates', 'email_templates')
.这行代码使我的模板易于访问。在我的project_dir中,我有一个名为
templates
的文件夹和一个名为email_templates
的子目录,如下所示我的模板
receipt_email.txt
和receipt_email。 html
位于email_templates
子目录下。让我补充一点,我的
recept_email.txt
看起来像这样;而且,我的
receipt_email.html
看起来像这样;I know this is an old question, but I also know that some people are just like me and are always looking for uptodate answers, since old answers can sometimes have deprecated information if not updated.
Its now January 2020, and I am using Django 2.2.6 and Python 3.7
Note: I use DJANGO REST FRAMEWORK, the code below for sending email was in a model viewset in my
views.py
So after reading multiple nice answers, this is what I did.
Important! So how does
render_to_string
getreceipt_email.txt
andreceipt_email.html
?In my
settings.py
, I haveTEMPLATES
and below is how it looksPay attention to
DIRS
, there is this lineos.path.join(BASE_DIR, 'templates', 'email_templates')
.This line is what makes my templates accessible. In my project_dir, I have a folder called
templates
, and a sub_directory calledemail_templates
like thisMy templates
receipt_email.txt
andreceipt_email.html
are under theemail_templates
sub_directory.Let me just add that, my
recept_email.txt
looks like this;And, my
receipt_email.html
looks like this;使用 EmailMultiAlternatives 和 render_to_string 来利用两种替代模板(一种为纯文本,一种为 html):
Use EmailMultiAlternatives and render_to_string to make use of two alternative templates (one in plain text and one in html):
我创建了 Django Simple Mail 为每封交易电子邮件提供一个简单、可定制且可重用的模板您想发送。
电子邮件内容和模板可以直接从 django 的管理员编辑。
以您的示例为例,您将注册您的电子邮件:
并以这种方式发送:
我很乐意获得任何反馈。
I have created Django Simple Mail to have a simple, customizable and reusable template for every transactional email you would like to send.
Emails contents and templates can be edited directly from django's admin.
With your example, you would register your email :
And send it this way :
I would love to get any feedback.
send_emai()
对我不起作用,所以我使用了EmailMessage
在 django 文档中。我已经包含了两个版本的 anser:
如果您想包含电子邮件的纯文本版本,请像这样修改上面的内容:
我的纯文本和 html 版本如下所示:
plain_version.html:
html_version.html
send_emai()
didn't work for me so I usedEmailMessage
here in django docs.I have included two versions of the anser:
If you want to include a plain text version of your email, modify the above like this:
My plain and html versions look like this:
plain_version.html:
html_version.html
例子中有错误....如果按照写的使用的话,会出现以下错误:
您将需要添加以下导入:
并将字典更改为:
请参阅 http://docs.djangoproject.com/en/1.2/ref/templates/api/#rendering-a-context
There is an error in the example.... if you use it as written, the following error occurs:
You will need to add the following import:
and change the dictionary to be:
See http://docs.djangoproject.com/en/1.2/ref/templates/api/#rendering-a-context
Django Mail Templated 是一个功能丰富的 Django 应用程序,用于发送使用 Django 模板系统发送电子邮件。
安装:
配置:
模板:
Python:
更多信息: https://github.com/artemrizhov/django-邮件模板
Django Mail Templated is a feature-rich Django application to send emails with Django template system.
Installation:
Configuration:
Template:
Python:
More info: https://github.com/artemrizhov/django-mail-templated
我喜欢使用这个工具来轻松发送电子邮件 HTML 和 TXT 以及简单的上下文处理: https://github .com/divio/django-emailit
I like using this tool to permit easily to send email HTML and TXT with easy context processing: https://github.com/divio/django-emailit
我写了一个 snippet允许您发送使用数据库中存储的模板呈现的电子邮件。一个例子:
I wrote a snippet that allows you to send emails rendered with templates stored in the database. An example:
如果您想要邮件的动态电子邮件模板,请将电子邮件内容保存在数据库表中。
这是我在数据库中保存为 HTML 代码的内容 =
在您看来:
这将发送您在数据库中保存的动态 HTML 模板。
If you want dynamic email templates for your mail then save the email content in your database tables.
This is what i saved as HTML code in database =
In your views:
This will send the dynamic HTML template what you have save in Db.