[英]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_string
和get_template
相同的結果
您可以使用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.