django用textarea html发送html电子邮件

时间:2013-10-08 11:46:54

标签: django email templates django-templates django-email

我有这段代码:

def send(modeladmin, request, queryset):
    emails = []
    for email in queryset.get().listing.emails.all():
        if email.is_valid():
            emails.append(email.email)

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

    template_data = {
        'body': '<p style="color:red">asdasdas</p>'
    }

    html_body = render_to_string("template.html", template_data)
    from_email = "%s <%s>" % (queryset.get().from_identity.full_name, queryset.get().from_identity.email)
    msg = EmailMultiAlternatives(subject=queryset.get().subject, from_email=from_email, to=emails, headers={})
    msg.attach_alternative(html_body, "text/html")

    msg.send()

现在,我收到了带模板的电子邮件,但是这个“正文”数据被转义了,如何在不逃避的情况下进行渲染?

1 个答案:

答案 0 :(得分:1)

您可以使用mark_safe()

将html_body标记为安全
from django.utils.safestring import mark_safe