用 django emailmessage 发送一个保险箱 html
sending a safe html with django emailmessage
美好的一天,我正在尝试在 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_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()
美好的一天,我正在尝试在 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_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()