0

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

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


  

此代码是发送简单的字符串,而不是发送 HTML。例如,如果我传入<h1>test</h1>我的消息正文,那么它将返回相同。我想<h1>在“测试”中应用标签。怎么做 ?

4

2 回答 2

1
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)
于 2020-10-10T07:23:03.863 回答
0

你可以做这样的事情这会奏效。但是您必须在“html_message”中传递该消息

from django.template import Template

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

       ) 
于 2020-10-10T07:17:30.473 回答