繁体   English   中英

使用 django 发送安全的 html 电子邮件消息

[英]sending a safe html with django emailmessage

美好的一天,我正在尝试在 django email 中发送 html,请问我还能在代码中添加什么。 email 发送运行良好,但仍显示 html 标签。

from celery import task
from django.template.loader import render_to_string, get_template
from django.core.mail import EmailMessage
from orders.models import Order


@task
def payment_completed(order_id):
    order = Order.objects.get(id=order_id)

    subject = f'Testing html sending'
    message = render_to_string('orders/order/pdf.html', {'order':order})
    email = EmailMessage(
        subject,
        message,
        'youremai@gmail.com',
        [order.email, 'youremai@gmail.com']
    )
    email.content_subtype = 'html'
    email.send()

我试过render_to_stringget_template相同的结果

这是我在邮件中收到的内容。(显示 HTML 标记)

您可以使用EmailMultiAlternatives代替EmailMessage ,代码可能如下所示

email = EmailMultiAlternatives(
    subject,
    message_plain_text,
    'youremai@gmail.com',
    [order.email, 'youremai@gmail.com']
)
email.attach_alternative(message, 'text/html')
email.send()

暂无
暂无

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

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