Reputation: 24806
Django 1.3 will add a "cc" argument to EmailMessage
, which is excellent. How would one mimic this using 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)
This correctly set the "Cc" header but did not actually send the carbon copy. I looked at SMTP.sendmail for clues, and it appears to take all the recipients as a single argument (it doesn't have separate to
, cc
, and bcc
arguments).
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 hit "reply" (in Gmail, at any rate) both addresses appeared in the "To" field. I tried also setting the "Reply-To" header (to sender
), but this made no difference.
It must be possible to "cc" an address without also including the address among the direct recipients. How would I do so?
Upvotes: 3
Views: 5843
Reputation: 166
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
Upvotes: 0
Reputation: 41
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()
Upvotes: 4
Reputation: 1719
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)
Upvotes: 1