如何使用 Django 在电子邮件中发送 HTML?

How to send HTML in email using Django?

我是 Django 的新手。我想使用 django 通过电子邮件发送 html。我正在使用以下代码

  send_mail(
            'Email Title',
            'My Message',
            'webmaster@localhost', 
            [to mail],   
            fail_silently=False,
           ) 


  

这段代码是发送简单的字符串,而不是发送HTML。例如,如果我在消息正文中传递 <h1>test</h1>,那么它将 return 相同。我想在 'test' 中应用 <h1> 标签。怎么做?

你可以做这样的事情,这会奏效。 但你必须在 'html_message'

中传递该消息
from django.template import Template

send_mail(
        'Email Title',
        html_message = Template("<b>Hello</b>"),
        'webmaster@localhost', 
        [to mail],   
        fail_silently=False,

       ) 
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string

def send_emails(request):
    merge_data = {
        'greetings': "hello"
    }
    html_body = render_to_string("email-templates.html", merge_data)

    message = EmailMultiAlternatives(
       subject='Django HTML Email',
       body="mail testing",
       from_email='xyz@abc.com',
       to=['wxyz1@abc.com']
    )
    message.attach_alternative(html_body, "text/html")
    message.send(fail_silently=False)