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

Time:12-29

I'm trying to send emails from django using a html template. It does send the email but the html is sent as plain text. How can I make the email appear as html to the receiver?

I'm using 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,
    '[email protected]',  
    [user.email],  
)
email.send()

activate_email.html

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

enter image description here

CodePudding user response:

you can use the content_subtype attribute on the EmailMessage class to change the main content.

email = EmailMessage(
    'Website email verification',  
    html_message,
    '[email protected]',  
    [user.email],  
)
email.content_subtype = "html"  # Main content is now text/html
email.send()
  • Related