[Fixed]-Send email to bcc and cc in django

43👍

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

9👍

2👍

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

0👍

You can use TextField

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

Input multiple emails like this (bcc field): 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}
    )

Leave a comment