我正在嘗試使用 html 模板從 django 發送電子郵件。它確實發送電子郵件,但 html 以純文本形式發送。如何使電子郵件對接收者顯示為 html?
我正在使用 Django 4.0
視圖.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,
'[email protected]',
[user.email],
)
email.send()
activate_email.html
{% autoescape off %}
<h1>Test</h1>
{% endautoescape %}

uj5u.com熱心網友回復:
您可以使用EmailMessage類content_subtype 上的 屬性 來更改主要內容。
email = EmailMessage(
'Website email verification',
html_message,
'[email protected]',
[user.email],
)
email.content_subtype = "html" # Main content is now text/html
email.send()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/395933.html
