15

视图.py

if 'send_email' in request.POST:
    subject, from_email, to = 'Parent Incident Notification',user.email, person.parent_email
    html_content = render_to_string('incident/print.html',{'person':person,
                                                                 'report':report,
                                                                  }) 
    text_content = strip_tags(html_content) 
    msg = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL, [to])
    msg.attach_alternative(html_content, "text/html")
    msg.send()

以上是发送电子邮件的视图。通过这种方式,我可以将 html 内容与邮件一起发送,它仅将电子邮件发送到 [to] 地址,我还想制作另一个密件抄送和抄送。我浏览了Emailmessage objects 文档.我不知道如何包含密件抄送和抄送来改变我的观点。

需要帮忙。

谢谢

4

4 回答 4

40

EmailMultiAlternatives是 的子类EmailMessage。您可以指定bcc以及cc何时初始化消息。

msg = EmailMultiAlternatives(subject, text_content, from_email, [to_email], bcc=[bcc_email], cc=[cc_email])
于 2013-06-12T11:43:24.083 回答
7

EmailMessage现在支持ccbcc

https://docs.djangoproject.com/en/1.10/topics/email/#django.core.mail.EmailMessage

于 2016-08-20T00:12:23.850 回答
1

我需要以 HTML 内容为正文的密件抄送,这是我的实现

from django.core.mail import EmailMessage

email = EmailMessage(
            'Subject',
            'htmlBody',
            'from@email.com',
            [to@email.com],
            [bcc@email.com],
            reply_to=['reply_to@email.com']
        )
 email.content_subtype = "html"
 email.send(fail_silently=True)

有关更多详细信息,请参阅Django 文档

于 2019-07-12T08:12:33.863 回答
0

您可以使用文本字段

class Client(models.Model):
        bcc = models.TextField(null=True, blank=True)

像这样输入多封电子邮件(密件抄送字段):test1@example.com,test2@example.com

from django.core.mail import EmailMessage

clients= Client.objects.all()
for client in clients:
    subject = client.subject
    content = client.body
    contact_email = client.msg_from
    to = client.msg_to
    bcc_mails = client.bcc
    bcc = bcc_mails.split(",")
    bcc_mails.replace('"', "")
    email = EmailMessage(
        subject,
        content,
        contact_email,
        [to],
        bcc,
        headers={'Reply-To': contact_email}
    )
于 2021-10-05T09:25:36.680 回答