Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cc/bcc #73

Merged
merged 6 commits into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=['[email protected]'],
text="Hello world",
html='<p>Hello world</p>',
from_email='[email protected]',
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=['[email protected]'],
cc=['[email protected]'],
bcc=['[email protected]'],
text="Hello world",
html='<p>Hello world</p>',
from_email='[email protected]',
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=['[email protected]'],
bcc=['[email protected]'],
html='<p>Hello {{name}}</p>',
text='Hello {{name}}',
from_email='[email protected]',
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=['[email protected]'],
cc=['[email protected]'])
assert results['recipients'] == [
{'address': {'email': '[email protected]'}},
{'address': {'email': '[email protected]',
'header_to': '[email protected]'}},
]
assert results['content']['headers'] == {
'CC': '[email protected]'
}


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


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


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