Skip to content

Commit 4a0eebc

Browse files
committed
Merge pull request #73 from SparkPost/ISSUE-55
Add support for cc/bcc
2 parents 3cbcfe6 + 037cec1 commit 4a0eebc

File tree

4 files changed

+89
-10
lines changed

4 files changed

+89
-10
lines changed

docs/resources/transmissions.rst

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,28 @@ Here at SparkPost, our messages are known as transmissions. Let's use the underl
2727
Send a transmission
2828
-------------------
2929

30-
There are several ways to send a transmission:
30+
Using inline templates and/or recipients
31+
****************************************
3132

32-
* Using inline templates and/or recipients
33-
* Using a stored template
34-
* Using a stored recipient list
33+
.. code-block:: python
3534
35+
from sparkpost import SparkPost
3636
37-
Using inline templates and/or recipients
38-
****************************************
37+
sp = SparkPost()
38+
39+
sp.transmissions.send(
40+
recipients=['[email protected]'],
41+
text="Hello world",
42+
html='<p>Hello world</p>',
43+
from_email='[email protected]',
44+
subject='Hello from python-sparkpost',
45+
track_opens=True,
46+
track_clicks=True
47+
)
48+
49+
50+
Including cc, bcc
51+
*****************
3952

4053
.. code-block:: python
4154
@@ -45,6 +58,8 @@ Using inline templates and/or recipients
4558
4659
sp.transmissions.send(
4760
recipients=['[email protected]'],
61+
62+
4863
text="Hello world",
4964
html='<p>Hello world</p>',
5065
from_email='[email protected]',

examples/transmissions/send_transmission.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
}
1919
}
2020
],
21+
22+
2123
html='<p>Hello {{name}}</p>',
2224
text='Hello {{name}}',
2325
from_email='[email protected]',

sparkpost/transmissions.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,40 @@ def _translate_keys(self, **kwargs):
4343
model['content']['html'] = kwargs.get('html')
4444
model['content']['text'] = kwargs.get('text')
4545
model['content']['template_id'] = kwargs.get('template')
46-
model['content']['headers'] = kwargs.get('custom_headers')
46+
model['content']['headers'] = kwargs.get('custom_headers', {})
4747

4848
recipient_list = kwargs.get('recipient_list')
4949
if recipient_list:
5050
model['recipients']['list_id'] = recipient_list
5151
else:
5252
recipients = kwargs.get('recipients', [])
53-
model['recipients'] = self._extractRecipients(recipients)
53+
cc = kwargs.get('cc')
54+
bcc = kwargs.get('bcc')
55+
56+
if cc:
57+
model['content']['headers']['CC'] = ','.join(cc)
58+
cc_copies = self._format_copies(recipients, cc)
59+
recipients = recipients + cc_copies
60+
if bcc:
61+
bcc_copies = self._format_copies(recipients, bcc)
62+
recipients = recipients + bcc_copies
63+
64+
model['recipients'] = self._extract_recipients(recipients)
5465

5566
attachments = kwargs.get('attachments', [])
5667
model['content']['attachments'] = self._extract_attachments(
5768
attachments)
5869

5970
return model
6071

72+
def _format_copies(self, recipients, copies):
73+
formatted_copies = []
74+
if len(recipients) > 0:
75+
formatted_copies = self._extract_recipients(copies)
76+
for recipient in formatted_copies:
77+
recipient['address'].update({'header_to': recipients[0]})
78+
return formatted_copies
79+
6180
def _extract_attachments(self, attachments):
6281
formatted_attachments = []
6382
for attachment in attachments:
@@ -77,7 +96,7 @@ def _get_base64_from_file(self, filename):
7796
encoded_string = base64.b64encode(a_file.read()).decode("ascii")
7897
return encoded_string
7998

80-
def _extractRecipients(self, recipients):
99+
def _extract_recipients(self, recipients):
81100
formatted_recipients = []
82101
for recip in recipients:
83102
try:
@@ -103,10 +122,12 @@ def send(self, **kwargs):
103122
"""
104123
Send a transmission based on the supplied parameters
105124
106-
:param list|dict recipients: If list it is an array of email addresses,
125+
:param list|dict recipients: If list it is an list of email addresses,
107126
if dict ``{'address': {'name': 'Name', 'email': 'me' }}``
108127
:param str recipient_list: ID of recipient list, if set recipients
109128
above will be ignored
129+
:param cc: List of email addresses to send carbon copy to
130+
:param bcc: List of email addresses to send blind carbon copy to
110131
:param str template: ID of template. If set HTML or text will not be
111132
used
112133
:param bool use_draft_template: Default to False. Set to true if you

test/test_transmissions.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,47 @@ def test_translate_keys_for_email_parsing():
4949
]
5050

5151

52+
def test_translate_keys_with_cc():
53+
t = Transmissions('uri', 'key')
54+
results = t._translate_keys(recipients=['[email protected]'],
55+
56+
assert results['recipients'] == [
57+
{'address': {'email': '[email protected]'}},
58+
{'address': {'email': '[email protected]',
59+
'header_to': '[email protected]'}},
60+
]
61+
assert results['content']['headers'] == {
62+
63+
}
64+
65+
66+
def test_translate_keys_with_multiple_cc():
67+
t = Transmissions('uri', 'key')
68+
results = t._translate_keys(recipients=['[email protected]'],
69+
70+
assert results['recipients'] == [
71+
{'address': {'email': '[email protected]'}},
72+
{'address': {'email': '[email protected]',
73+
'header_to': '[email protected]'}},
74+
{'address': {'email': '[email protected]',
75+
'header_to': '[email protected]'}},
76+
]
77+
assert results['content']['headers'] == {
78+
79+
}
80+
81+
82+
def test_translate_keys_with_bcc():
83+
t = Transmissions('uri', 'key')
84+
results = t._translate_keys(recipients=['[email protected]'],
85+
86+
assert results['recipients'] == [
87+
{'address': {'email': '[email protected]'}},
88+
{'address': {'email': '[email protected]',
89+
'header_to': '[email protected]'}},
90+
]
91+
92+
5293
@responses.activate
5394
def test_success_send():
5495
responses.add(

0 commit comments

Comments
 (0)