简体   繁体   English

以 HTML 格式发送的电子邮件正文

[英]Email body sent in HTML

I am sending email from tinymce to all subscribed users using this view:我正在使用此视图从 tinymce 向所有订阅用户发送电子邮件:

def send_newsletter(request):
    form = NewsLetterEmailForm(request.POST or None)
    if form.is_valid():
        instance = form.save()
        newsltr = NewsLetterEmail.objects.get(id=instance.id)
        print(newsltr.status)

        if newsltr.status == 'Published':
            subject = newsltr.subject
            body = mark_safe(newsltr.body)
            from_email = settings.EMAIL_HOST_USER

            for newsletter_obj in NewsLetter.objects.all():
                send_mail(subject=subject, from_email=from_email,
                          message=body, recipient_list=[newsletter_obj.email])

    return render(request, 'newsletter/send-email.html', {'form': form})

but content of email is sent in html:但电子邮件的内容以 html 格式发送:

<p><span style="font-family: 'arial black', sans-serif; font-size: 18pt;"><strong>Completely optimize efficient internal</strong></span></p>
<p>or "organic" sources with fully tested schemas. Enthusiastically aggregate mission-critical infrastructures via top-line content. Objectively matrix cutting-edge bandwidth before viral action items. Objectively matrix viral users after sticky processes. Dramatically harness adaptive meta-services rather than scalable e-commerce.</p>

I used mark_safe() method hoping it will work, but it didn't.我使用了mark_safe()方法希望它能起作用,但它没有。 How do I solve it?我该如何解决?

Just figured out how to do it using EmailMultiAlternative() , replacing this part of the code solves the problem.刚刚想出如何使用EmailMultiAlternative() ,替换这部分代码即可解决问题。

    if newsltr.status == 'Published':
        subject = newsltr.subject
        body = newsltr.body
        from_email = settings.EMAIL_HOST_USER

        for newsletter_obj in NewsLetter.objects.all():
            msg = EmailMultiAlternatives(
                subject, body, from_email, [newsletter_obj.email])
            msg.attach_alternative(body, "text/html")
            msg.send()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM