django-ses不发送HTML电子邮件

时间:2014-08-30 02:04:28

标签: django amazon-ses

我正在使用django-ses后端发送django-newsletter。但收到的电子邮件不包含任何HTML标记。

如果我使用django.core.mail.backends.console.EmailBackend发送,则会收到我想要的电子邮件,并且标题为正常:

MIME-Version: 1.0
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

1 个答案:

答案 0 :(得分:0)

将电子邮件对象的content_subtype设置为text/html

msg = EmailMessage(subject, html_content, from_email, [to])
msg.content_subtype = "html"  # Main content is now text/html
msg.send()

或者您可以使用EmailMultiAlternatives

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()

这样,默认情况下会呈现电子邮件html,但您仍然可以使用文本版本。