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

2024-09-30 10:34:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用此视图从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})

但电子邮件内容以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>

我使用了mark_safe()方法,希望它能起作用,但它没有起作用。我如何解决它


Tags: orinstancefromformsendidifobjects
1条回答
网友
1楼 · 发布于 2024-09-30 10:34:55

我们刚刚了解了如何使用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()

相关问题 更多 >

    热门问题