Skip to content

Commit

Permalink
SparkPost: work around json recipients problem
Browse files Browse the repository at this point in the history
python-sparkpost generates a transmissions.send
payload which is now considered invalid by the API,
if you try to use both `cc` (or `bcc`) and the
`recipients` dict structure required for merge_data.

[Anymail had been generating that recipients dict
structure in all cases, for simplicity. Sometime
between 2016-06-07 and 2016-06-22, SparkPost
began rejecting that if it appeared in the `header_to`
constructed by python-sparkpost.]
  • Loading branch information
medmunds committed Jun 22, 2016
1 parent 5adb07a commit b664ee3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
23 changes: 14 additions & 9 deletions anymail/backends/sparkpost.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,20 @@ def init_payload(self):
def get_api_params(self):
# Compose recipients param from to_emails and merge_data (if any)
recipients = []
for email in self.to_emails:
rcpt = {'address': {'email': email.email}}
if email.name:
rcpt['address']['name'] = email.name
try:
rcpt['substitution_data'] = self.merge_data[email.email]
except KeyError:
pass # no merge_data or none for this recipient
recipients.append(rcpt)
if len(self.merge_data) > 0:
# Build JSON recipient structures
for email in self.to_emails:
rcpt = {'address': {'email': email.email}}
if email.name:
rcpt['address']['name'] = email.name
try:
rcpt['substitution_data'] = self.merge_data[email.email]
except KeyError:
pass # no merge_data or none for this recipient
recipients.append(rcpt)
else:
# Just use simple recipients list
recipients = [email.address for email in self.to_emails]
if recipients:
self.params['recipients'] = recipients

Expand Down
12 changes: 3 additions & 9 deletions tests/test_sparkpost_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_send_mail(self):
self.assertEqual(params['subject'], "Subject here")
self.assertEqual(params['text'], "Here is the message.")
self.assertEqual(params['from_email'], "from@example.com")
self.assertEqual(params['recipients'], [{'address': {'email': "to@example.com"}}])
self.assertEqual(params['recipients'], ["to@example.com"])

self.assertEqual(self.get_send_api_key(), 'test_api_key')

Expand All @@ -118,10 +118,7 @@ def test_name_addr(self):
params = self.get_send_params()
self.assertEqual(params['from_email'], "From Name <from@example.com>")
# We pre-parse the to-field emails (merge_data also gets attached there):
self.assertEqual(params['recipients'], [
{'address': {'email': 'to1@example.com', 'name': 'Recipient #1'}},
{'address': {'email': 'to2@example.com'}}
])
self.assertEqual(params['recipients'], ['Recipient #1 <to1@example.com>', 'to2@example.com'])
# We let python-sparkpost parse the other email fields:
self.assertEqual(params['cc'], ['Carbon Copy <cc1@example.com>', 'cc2@example.com'])
self.assertEqual(params['bcc'], ['Blind Copy <bcc1@example.com>', 'bcc2@example.com'])
Expand All @@ -141,10 +138,7 @@ def test_email_message(self):
self.assertEqual(params['subject'], "Subject")
self.assertEqual(params['text'], "Body goes here")
self.assertEqual(params['from_email'], "from@example.com")
self.assertEqual(params['recipients'], [
{'address': {'email': 'to1@example.com'}},
{'address': {'email': 'to2@example.com', 'name': 'Also To'}}
])
self.assertEqual(params['recipients'], ['to1@example.com', 'Also To <to2@example.com>'])
self.assertEqual(params['bcc'], ['bcc1@example.com', 'Also BCC <bcc2@example.com>'])
self.assertEqual(params['cc'], ['cc1@example.com', 'Also CC <cc2@example.com>'])
self.assertEqual(params['custom_headers'], {
Expand Down

2 comments on commit b664ee3

@medmunds
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richleland
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just in case you didn't see - we fixed this underlying bug in 1.3.1. Thanks for bringing it to our attention.

Please sign in to comment.