在django中将电子邮件发送到bcc和 cc

views.py

views.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在文档中。我不知道如何包括密件抄送和抄送来改变我的观点。

需要帮助。

谢谢

42

EmailMultiAlternativesEmailMessage的子类。您可以在初始化消息时指定bcccc

msg = EmailMultiAlternatives(subject, text_content, from_email, [to_email], bcc=[bcc_email], cc=[cc_email])
2

我需要 bcc 与 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 docs

0

您可以使用 TextField

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

像这样输入多个电子邮件(bcc字段):test1 @ example.com,test2 @ example.com

from django.core.mail import EmailMessage
clients= .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}
    )

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(748)
在 C# /UWP中发生特定事件时强制窗口闪烁
上一篇
NETGEAR路由器:DNS服务器没有响应
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(61条)