使用django和html模板发送邮件

qfe3c7zg  于 2023-07-01  发布在  Go
关注(0)|答案(1)|浏览(91)

我尝试使用django发送HTML模板邮件。邮件发送正确,但无法获取html模板
这是我的View.py文件

def Mail_Send(request):
    name = "Shyam"
    mydist = { 'name': name }
    html_template = 'mail_template.html'
    render_html = get_template(html_template).render(mydist)
    
    email = EmailMessage('Welcome',
                         render_html,
                         settings.EMAIL_HOST_USER,
                         ['itsupport@synergyhospital.co.in'],
                        
    )
    email.content_subtype='html'
    email.send(fail_silently=False)
    tesddting = {
        'mailsend':'mailsend'
    }

    return render(request,"index.html",tesddting)

这是我的Html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>Hello This is testing</h2>
    <p>Hi {{name}}</p>
    
</body>
</html>

**the html file is in template folder**
zmeyuzjn

zmeyuzjn1#

在Django Docs中:

from django.core.mail import send_mail

send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None)

你的html_message应该是

from django.template.loader import render_to_string

html_message = render_to_string('mail_template.html', params)

相关问题