简体   繁体   中英

sending a safe html with django emailmessage

Good day, I'm trying to send a html in django email, pls what more can I added to the code. the email sending is functioning well but it still shows the html tags.

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()

I've tried render_to_string and get_template same result

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

You can use EmailMultiAlternatives instead of EmailMessage , and code may look like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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