Django DRF - 使用 html 模板发送电子邮件 - html 模板显示为纯文本

Django DRF - Sending email with html template - html template appears as plain text

我正在尝试使用 html 模板从 django 发送电子邮件。它确实发送电子邮件,但 html 以纯文本形式发送。我怎样才能使电子邮件在收件人面前显示为 html?

我正在使用 Django 4.0

views.py

# import file with html content
html_version = 'activate_email.html'
html_message = render_to_string(html_version, {'context': context })

email = EmailMessage(
    'Website email verification',  
    html_message,
    'info@example.co',  
    [user.email],  
)
email.send()

activate_email.html

{% autoescape off %}
<h1>Test</h1>
{% endautoescape %}

您可以使用 EmailMessage class 上的 content_subtype 属性来更改主要内容。

email = EmailMessage(
    'Website email verification',  
    html_message,
    'info@example.co',  
    [user.email],  
)
email.content_subtype = "html"  # Main content is now text/html
email.send()
</pre>