【问题标题】:Django DRF - Sending email with html template - html template appears as plain textDjango DRF - 使用 html 模板发送电子邮件 - html 模板显示为纯文本
【发布时间】:2021-12-28 23:11:51
【问题描述】:

我正在尝试使用 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 %}

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    您可以使用EmailMessage 类的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()
    

    【讨论】:

      猜你喜欢
      • 2016-11-03
      • 2015-10-10
      • 2014-05-24
      • 2016-02-20
      • 2012-09-16
      • 2017-05-26
      • 2019-06-11
      • 2020-09-08
      • 1970-01-01
      相关资源
      最近更新 更多