我想发送html电子邮件,使用Django模板,像这样:

<html>
<body>
hello <strong>{{username}}</strong>
your account activated.
<img src="mysite.com/logo.gif" />
</body>

我找不到任何关于send_mail的东西,django-mailer只发送HTML模板,没有动态数据。

如何使用Django的模板引擎生成电子邮件?


当前回答

示例....中有一个错误如果你像写的那样使用它,会出现以下错误:

< type '异常。异常' >:'dict'对象没有属性'render_context'

您将需要添加以下导入:

from django.template import Context

并将字典改为:

d = Context({ 'username': username })

看到http://docs.djangoproject.com/en/1.2/ref/templates/api/ rendering-a-context

其他回答

示例....中有一个错误如果你像写的那样使用它,会出现以下错误:

< type '异常。异常' >:'dict'对象没有属性'render_context'

您将需要添加以下导入:

from django.template import Context

并将字典改为:

d = Context({ 'username': username })

看到http://docs.djangoproject.com/en/1.2/ref/templates/api/ rendering-a-context

在文档中,要发送HTML电子邮件,您需要使用替代内容类型,如下所示:

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

你可能需要两个电子邮件模板——一个纯文本模板,看起来像这样,存储在你的模板目录下的email.txt:

Hello {{ username }} - your account is activated.

还有一个html格式的,存储在email.html下:

Hello <strong>{{ username }}</strong> - your account is activated.

然后,你可以使用get_template函数使用这两个模板发送电子邮件,如下所示:

from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from django.template import Context

plaintext = get_template('email.txt')
htmly     = get_template('email.html')

d = Context({ 'username': username })

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = plaintext.render(d)
html_content = htmly.render(d)
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

我做了django-template -email来解决这个问题,灵感来自于这个解决方案(在某些时候,需要从使用django模板切换到使用mailchimp等一组模板来处理事务,为我自己的项目创建模板邮件)。虽然这仍然是一个正在进行的工作,但对于上面的例子,你可以这样做:

from templated_email import send_templated_mail
send_templated_mail(
        'email',
        'from@example.com',
        ['to@example.com'],
        { 'username':username }
    )

在settings.py中添加以下内容(以完成示例):

TEMPLATED_EMAIL_DJANGO_SUBJECTS = {'email':'hello',}

它会自动在普通的django模板dirs/loaders中分别为plain部分和html部分寻找名为templated_email/email.txt和templated_email/email.html的模板(如果找不到其中至少一个会报错)。

使用EmailMultiAlternatives和render_to_string来使用两个可供选择的模板(一个是纯文本,一个是html):

from django.core.mail import EmailMultiAlternatives
from django.template import Context
from django.template.loader import render_to_string

c = Context({'username': username})    
text_content = render_to_string('mail/email.txt', c)
html_content = render_to_string('mail/email.html', c)

email = EmailMultiAlternatives('Subject', text_content)
email.attach_alternative(html_content, "text/html")
email.to = ['to@example.com']
email.send()

Django Mail Templated是一个功能丰富的Django应用程序,可以用Django模板系统发送电子邮件。

安装:

pip install django-mail-templated

配置:

INSTALLED_APPS = (
    ...
    'mail_templated'
)

模板:

{% block subject %}
Hello {{ user.name }}
{% endblock %}

{% block body %}
{{ user.name }}, this is the plain text part.
{% endblock %}

Python:

from mail_templated import send_mail
send_mail('email/hello.tpl', {'user': user}, from_email, [user.email])

更多信息:https://github.com/artemrizhov/django-mail-templated