Setting "cc" when sending email from Django

Django 1.3 will add the argument "cc" to EmailMessage, which is great. How can I simulate this with Django 1.2?

First, I tried this:

headers = None
if form.cleaned_data['cc_sender']:
    headers = {'Cc': sender} # `cc` argument added in Django 1.3

msg = EmailMultiAlternatives(subject, message, sender, recipients, headers=headers)
msg.attach_alternative(replace(convert(message)), 'text/html')
msg.send(fail_silently=False)

Set the "Cc" header correctly, but didn’t actually send a copy. I reviewed SMTP.sendmail for hints and it seems that all recipients accept as one argument (it does not have a separate one to, ccand bcc).

Next I tried this:

headers = None
if form.cleaned_data['cc_sender']:
    headers = {'Cc': sender} # `cc` argument added in Django 1.3
    recipients.append(sender) # <-- added this line

msg = EmailMultiAlternatives(subject, message, sender, recipients, headers=headers)
msg.attach_alternative(replace(convert(message)), 'text/html')
msg.send(fail_silently=False)

This worked, but meant that when I click "reply" (anyway in Gmail), both addresses appear in the "To" field. I also tried setting the Reply-To header (before sender), but that didn't matter.

"cc" , . ?

+3
3

Cc: , , CC "bcc" EmailMessage. , - CC- , , . ( , SMTP ).

message = EmailMessage(subject=subject,
                       body=body,
                       from_email=sender,
                       to=to_addresses,
                       bcc=cc_addresses,
                       headers={'Cc': ','.join(cc_addresses)})
message.send()
+3

BCC kwarg MailMultiAlternatives, -, BCC .

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)
+1

EmailMultiAlternatives EmailMessage. bcc cc.

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

Copied from Link

0
source

Source: https://habr.com/ru/post/1787888/


All Articles