send email to bcc and cc in django

此生再无相见时 提交于 2019-12-13 11:53:17

问题


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

The above is the view to send email.By that way i can send the html content along with mail,it is sending the email to [to] address alone ,i want to made another bcc and cc also.I gone through the Emailmessage objects in docs.I don't know how to include the bcc and cc to alter my views.

Need help.

Thanks


回答1:


EmailMultiAlternatives is a subclass of EmailMessage. You can specify bcc and cc when you initialise the message.

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



回答2:


EmailMessage now supports cc and bcc:

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




回答3:


I needed bcc with HTML content as body and here is my implementation

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)

For more details refer Django docs



来源:https://stackoverflow.com/questions/17064497/send-email-to-bcc-and-cc-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!