【问题标题】:sending a safe html with django emailmessage使用 django emailmessage 发送安全的 html
【发布时间】:2020-12-12 13:28:06
【问题描述】:

美好的一天,我正在尝试在 django 电子邮件中发送一个 html,请问我还能在代码中添加什么。 电子邮件发送运行良好,但仍显示 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 结果一样

【问题讨论】:

  • 能否展示您在电子邮件中看到的内容?因为看起来您的发送电子邮件功能看起来不错。
  • 好的,我要把它添加到帖子中
  • 能否也显示您尝试过的 get_template 代码?
  • 还是一样

标签: python django django-templates django-email


【解决方案1】:

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

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

【讨论】:

  • 我可以用 emailmultialternative 渲染成字符串吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 2013-10-15
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多