[Django]-Setting "cc" when sending e-mail from Django

4๐Ÿ‘

Add the Cc: header just like you did, and additionally pass the list of CC addresses in the โ€œbccโ€ keyword arg to the EmailMessage constructor. It seems a little counterintuitive but the real effect of this is simply to add the CC addresses to the recipients list, which is exactly what you want to do. (If you want to learn more about the difference between headers and the recipient list, the Wikipedia article on SMTP gives some nice background).

message = EmailMessage(subject=subject,
                       body=body,
                       from_email=sender,
                       to=to_addresses,
                       bcc=cc_addresses,
                       headers={'Cc': ','.join(cc_addresses)})
message.send()
๐Ÿ‘คSteve

1๐Ÿ‘

There is a BCC kwarg for EmailMultiAlternatives, I use it in a wrapper function to automatically BCC a records email account on all outbound communications.

from django.core.mail import EmailMultiAlternatives

def _send(to, subject='', text_content='', html_content='', reply_to=None):
    if not isinstance(to, (list, tuple)):
        to = (to,)
    kwargs = dict(
        to=to,
        from_email='%s <%s>' % ('Treatful', settings.EMAIL_HOST_USER),
        subject=subject,
        body=text_content,
        alternatives=((html_content, 'text/html'),)
    )
    if reply_to:
        kwargs['headers'] = {'Reply-To': reply_to}
    if not settings.DEBUG:
        kwargs['bcc'] = (settings.RECORDS_EMAIL,)
    message = EmailMultiAlternatives(**kwargs)
    message.send(fail_silently=True)
๐Ÿ‘คJeremy Lewis

0๐Ÿ‘

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

Copied from Link

๐Ÿ‘คuser5091906

Leave a comment