簡體   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