在django中发送HTML电子邮件

时间:2010-07-13 13:16:42

标签: html django django-templates html-email

在我的项目中,我添加了一个简报Feed。但在尝试使用此功能发送电子邮件时:

def send(request):
    template_html = 'static/newsletter.html'
    template_text = 'static/newsletter.txt'
    newsletters = Newsletter.objects.filter(sent=False)
    subject = _(u"Newsletter")
    adr = NewsletterEmails.objects.all()
    for a in adr:
        for n in newsletters:
            to = a.email
            from_email = settings.DEFAULT_FROM_EMAIL           
            subject = _(u"Newsletter Fandrive")
            text = get_template(template_text)
            html = get_template(template_html)
            d = { 'n': n,'email': to }
            text_content = text.render(d)
            html_content = html.render(d)

            msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
            msg.attach_alternative(html_content, "text/html")
            msg.send()

使用这些模板:

//text

===================  Newsletter - {{ n.date }}  ============
==========================================================
                      {{ n.title }}
==========================================================          
{{ n.text }}
==========================================================

//html

<html>
    <head>
    </head>
    <body>
    <div style="">
        <div style="">
            <h1 style="">{{ n.title }} - {{n.date}}</h1>
                <p style="">            
                    {{ n.text }}
                </p>
        </div>
    </div>
    </body>
</html>

和模特:

class Newsletter(models.Model):
    title = models.CharField("title", blank=False, max_length=50)
    text = models.TextField("text", blank=False)
    sent = models.BooleanField("sent", default=False)
    data = models.DateTimeField("creation date", auto_now_add=True, blank=False)

class NewsletterEmails(models.Model):
    email = models.EmailField(_(u"e-mail address"),)

我得到了:

TemplateSyntaxError at /utils/newsletter_send/
Caught an exception while rendering: 'dict' object has no attribute 'autoescape'

在text_email模板

中的 {{n.date}}

虽然我的调试显示我正在向模板发送正确的新闻稿对象,以及调试上下文:

context {'email': u'admin@domain.com', 'n': <Newsletter: Newsletter object>}

为什么会这样?从我发现的这个错误,它以某种方式连接到将空字典发送到模板渲染器,但我的不是空的......

3 个答案:

答案 0 :(得分:17)

仅供参考。我找到了另一种方法:

def send(request):
    template_html = 'static/newsletter.html'
    template_text = 'static/newsletter.txt'
    newsletters = Newsletter.objects.filter(sent=False)
    subject = _(u"Newsletter Fandrive")
    adr = NewsletterEmails.objects.all()
    for a in adr:
        for n in newsletters:
            to = a.email
            from_email = settings.DEFAULT_FROM_EMAIL           
            subject = _(u"Newsletter Fandrive")

            text_content = render_to_string(template_text, {"title": n.title,"text": n.text, 'date': n.date, 'email': to})
            html_content = render_to_string(template_html, {"title": n.title,"text": n.text, 'date': n.date, 'email': to})

            msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
            msg.attach_alternative(html_content, "text/html")
            msg.send()

    return HttpResponseRedirect('/')

答案 1 :(得分:11)

他们已更新send_mail以允许dev version

中的html消息
def send(request):
    template_html = 'static/newsletter.html'
    template_text = 'static/newsletter.txt'
    newsletters = Newsletter.objects.filter(sent=False)
    subject = _(u"Newsletter Fandrive")
    adr = NewsletterEmails.objects.all()
    for a in adr:
        for n in newsletters:
            to = a.email
            from_email = settings.DEFAULT_FROM_EMAIL           
            subject = _(u"Newsletter Fandrive")

            text_content = render_to_string(template_text, {"title": n.title,"text": n.text, 'date': n.date, 'email': to})
            html_content = render_to_string(template_html, {"title": n.title,"text": n.text, 'date': n.date, 'email': to})

            send_mail(subject, text_content, from_email,
             to, fail_silently=False, html_message=html_content)
    return HttpResponseRedirect('/')

答案 2 :(得分:10)

这是一个非常简单的修复,你遗漏了一件小事。

你这样做:

  d = { 'n': n,'email': to }

然后尝试将该字典用作render()方法的一部分。但是,render需要Context,因此您需要执行此操作:

 d = Context({ 'n': n,'email': to })

确保也从django.template导入它。这应该可以解决您收到的错误。