djang电子邮件模板

2024-04-20 11:22:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个电子邮件模板在html即时消息发送电子邮件使用下面的看法,这是工作良好。但当我收到电子邮件时,它是以原始html格式显示的。我可以知道是什么错误吗。你知道吗

def confirmed_email_notification(sender, **kwargs):
    """
    Sends an email notification to the shop owner when a new order is
    completed.
    """
    print "EMAIL NOTIFICATION "
    subject_template_name = 'shop_simplenotifications/confirmed_subject.txt'
    body_template_name = 'shop_simplenotifications/confirmed_body.html'
    request = kwargs.get('request')
    order = kwargs.get('order')
    subject = loader.render_to_string(
        subject_template_name,
        RequestContext(request, {'order': order})
    )
    subject = subject.join(subject.splitlines())
    body = loader.render_to_string(
        body_template_name,
        RequestContext(request, {'order': order})
    )
    from_email = getattr(settings, 'SN_FROM_EMAIL',
                         settings.DEFAULT_FROM_EMAIL)
    owners = getattr(settings, 'SN_OWNERS', settings.ADMINS)
    send_mail(subject, body, from_email,
              [owner[1] for owner in owners], fail_silently=False)
    print body
    print [owner[1] for owner in owners]
confirmed.connect(confirmed_email_notification)

Tags: tonamesettingsemailrequesthtmlorderbody
1条回答
网友
1楼 · 发布于 2024-04-20 11:22:59

如果您使用的是django1.7,那么可以将消息的html版本作为html_message参数cfhttps://docs.djangoproject.com/en/1.7/topics/email/#send-mail传递给send_mail()

否则您可以使用EmailMultiAlternatives类,如下所述: https://docs.djangoproject.com/en/1.6/topics/email/#sending-alternative-content-types

或者使用EmailMessage类并将content_subtype属性设置为“html”。你知道吗

顺便说一句:我真的很怀疑subject.join(subject.splitlines())做你想做的事:

>>> subject = "hello Huston\nWe have a\nproblem"
>>> print subject.join(subject.splitlines())
hello Hustonhello Huston
We have a
problemWe have ahello Huston
We have a
problemproblem
>>> 

相关问题 更多 >