Skip to content

Commit

Permalink
Merge pull request #13 from dynamomobile/adding_cc_and_bcc_to_send_email
Browse files Browse the repository at this point in the history
Adding cc and bcc to send email
  • Loading branch information
Tyrdall committed Oct 7, 2016
2 parents edadab9 + 72c21e9 commit dff62f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
15 changes: 15 additions & 0 deletions django_libs/tests/utils/email_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,18 @@ def test_send_email(self):
'info@example.com', ['recipient@example.com'])
self.assertEqual(Message.objects.count(), 1, msg=(
'An email should\'ve been sent'))

def test_cc_and_bcc(self):
send_email(
None,
{},
'subject.html',
'html_email.html',
'info@example.com',
['recipient@example.com'],
cc=['cc@example.com'],
bcc=['bcc@example.com']
)
email = mail.outbox[0]
self.assertEqual(['cc@example.com'], email.cc)
self.assertEqual(['bcc@example.com'], email.bcc)
37 changes: 14 additions & 23 deletions django_libs/utils/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

def send_email(request, context, subject_template, body_template,
from_email, recipients, priority="medium", reply_to=None,
headers={}):
headers=None, cc=None, bcc=None):
"""
Sends an email based on templates for subject and body.
Expand All @@ -34,15 +34,12 @@ def send_email(request, context, subject_template, body_template,
to prioritise email sendings).
:param reply_to: Optional email address to reply to.
:param headers: Additional dictionary to add header attributes.
:param cc: A list of CC recipients
:param bcc: A list of BCC recipients
"""
headers = headers or {}
if not reply_to:
reply_to = from_email
if django.get_version() >= '1.8':
# The reply_to argument has been added in 1.8
reply_to = [reply_to]
else:
headers.update({'Reply-To': reply_to})
if request and request.get_host():
domain = request.get_host()
protocol = 'https://' if request.is_secure() else 'http://'
Expand All @@ -66,21 +63,15 @@ def send_email(request, context, subject_template, body_template,
else:
subject = force_text(subject)
message = force_text(message_plaintext)

if django.get_version() >= '1.8':
# The reply_to argument has been added in 1.8
email = EmailMessage(
subject=subject, body=message, from_email=from_email,
to=recipients, headers=headers, reply_to=reply_to)
email = EmailMultiAlternatives(
email.subject, email.body, email.from_email, email.to,
headers=email.extra_headers, reply_to=reply_to)
else:
email = EmailMessage(
subject=subject, body=message, from_email=from_email,
to=recipients, headers=headers)
email = EmailMultiAlternatives(
email.subject, email.body, email.from_email, email.to,
headers=email.extra_headers)
email = EmailMultiAlternatives(
subject=subject,
body=message,
from_email=from_email,
to=recipients,
cc=cc,
bcc=bcc,
headers=headers,
reply_to=[reply_to],
)
email.attach_alternative(message_html, "text/html")
email.send()

0 comments on commit dff62f9

Please sign in to comment.