Skip to content

Commit

Permalink
Merge pull request #73 from SparkPost/ISSUE-55
Browse files Browse the repository at this point in the history
Add support for cc/bcc
  • Loading branch information
richleland committed Mar 3, 2016
2 parents 3cbcfe6 + 037cec1 commit 4a0eebc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
27 changes: 21 additions & 6 deletions docs/resources/transmissions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,28 @@ Here at SparkPost, our messages are known as transmissions. Let's use the underl
Send a transmission
-------------------

There are several ways to send a transmission:
Using inline templates and/or recipients
****************************************

* Using inline templates and/or recipients
* Using a stored template
* Using a stored recipient list
.. code-block:: python
from sparkpost import SparkPost
Using inline templates and/or recipients
****************************************
sp = SparkPost()
sp.transmissions.send(
recipients=['someone@somedomain.com'],
text="Hello world",
html='<p>Hello world</p>',
from_email='test@sparkpostbox.com',
subject='Hello from python-sparkpost',
track_opens=True,
track_clicks=True
)
Including cc, bcc
*****************

.. code-block:: python
Expand All @@ -45,6 +58,8 @@ Using inline templates and/or recipients
sp.transmissions.send(
recipients=['someone@somedomain.com'],
cc=['carboncopy@somedomain.com'],
bcc=['blindcarboncopy@somedomain.com'],
text="Hello world",
html='<p>Hello world</p>',
from_email='test@sparkpostbox.com',
Expand Down
2 changes: 2 additions & 0 deletions examples/transmissions/send_transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
}
}
],
cc=['carboncopy@example.com'],
bcc=['blindcarboncopy@example.com'],
html='<p>Hello {{name}}</p>',
text='Hello {{name}}',
from_email='test@sparkpostbox.com',
Expand Down
29 changes: 25 additions & 4 deletions sparkpost/transmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,40 @@ def _translate_keys(self, **kwargs):
model['content']['html'] = kwargs.get('html')
model['content']['text'] = kwargs.get('text')
model['content']['template_id'] = kwargs.get('template')
model['content']['headers'] = kwargs.get('custom_headers')
model['content']['headers'] = kwargs.get('custom_headers', {})

recipient_list = kwargs.get('recipient_list')
if recipient_list:
model['recipients']['list_id'] = recipient_list
else:
recipients = kwargs.get('recipients', [])
model['recipients'] = self._extractRecipients(recipients)
cc = kwargs.get('cc')
bcc = kwargs.get('bcc')

if cc:
model['content']['headers']['CC'] = ','.join(cc)
cc_copies = self._format_copies(recipients, cc)
recipients = recipients + cc_copies
if bcc:
bcc_copies = self._format_copies(recipients, bcc)
recipients = recipients + bcc_copies

model['recipients'] = self._extract_recipients(recipients)

attachments = kwargs.get('attachments', [])
model['content']['attachments'] = self._extract_attachments(
attachments)

return model

def _format_copies(self, recipients, copies):
formatted_copies = []
if len(recipients) > 0:
formatted_copies = self._extract_recipients(copies)
for recipient in formatted_copies:
recipient['address'].update({'header_to': recipients[0]})
return formatted_copies

def _extract_attachments(self, attachments):
formatted_attachments = []
for attachment in attachments:
Expand All @@ -77,7 +96,7 @@ def _get_base64_from_file(self, filename):
encoded_string = base64.b64encode(a_file.read()).decode("ascii")
return encoded_string

def _extractRecipients(self, recipients):
def _extract_recipients(self, recipients):
formatted_recipients = []
for recip in recipients:
try:
Expand All @@ -103,10 +122,12 @@ def send(self, **kwargs):
"""
Send a transmission based on the supplied parameters
:param list|dict recipients: If list it is an array of email addresses,
:param list|dict recipients: If list it is an list of email addresses,
if dict ``{'address': {'name': 'Name', 'email': 'me' }}``
:param str recipient_list: ID of recipient list, if set recipients
above will be ignored
:param cc: List of email addresses to send carbon copy to
:param bcc: List of email addresses to send blind carbon copy to
:param str template: ID of template. If set HTML or text will not be
used
:param bool use_draft_template: Default to False. Set to true if you
Expand Down
41 changes: 41 additions & 0 deletions test/test_transmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,47 @@ def test_translate_keys_for_email_parsing():
]


def test_translate_keys_with_cc():
t = Transmissions('uri', 'key')
results = t._translate_keys(recipients=['primary@example.com'],
cc=['ccone@example.com'])
assert results['recipients'] == [
{'address': {'email': 'primary@example.com'}},
{'address': {'email': 'ccone@example.com',
'header_to': 'primary@example.com'}},
]
assert results['content']['headers'] == {
'CC': 'ccone@example.com'
}


def test_translate_keys_with_multiple_cc():
t = Transmissions('uri', 'key')
results = t._translate_keys(recipients=['primary@example.com'],
cc=['ccone@example.com', 'cctwo@example.com'])
assert results['recipients'] == [
{'address': {'email': 'primary@example.com'}},
{'address': {'email': 'ccone@example.com',
'header_to': 'primary@example.com'}},
{'address': {'email': 'cctwo@example.com',
'header_to': 'primary@example.com'}},
]
assert results['content']['headers'] == {
'CC': 'ccone@example.com,cctwo@example.com'
}


def test_translate_keys_with_bcc():
t = Transmissions('uri', 'key')
results = t._translate_keys(recipients=['primary@example.com'],
bcc=['bccone@example.com'])
assert results['recipients'] == [
{'address': {'email': 'primary@example.com'}},
{'address': {'email': 'bccone@example.com',
'header_to': 'primary@example.com'}},
]


@responses.activate
def test_success_send():
responses.add(
Expand Down

0 comments on commit 4a0eebc

Please sign in to comment.