mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 03:41:05 -05:00
SparkPost: work around json recipients problem
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.]
This commit is contained in:
@@ -87,6 +87,8 @@ class SparkPostPayload(BasePayload):
|
||||
def get_api_params(self):
|
||||
# Compose recipients param from to_emails and merge_data (if any)
|
||||
recipients = []
|
||||
if len(self.merge_data) > 0:
|
||||
# Build JSON recipient structures
|
||||
for email in self.to_emails:
|
||||
rcpt = {'address': {'email': email.email}}
|
||||
if email.name:
|
||||
@@ -96,6 +98,9 @@ class SparkPostPayload(BasePayload):
|
||||
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
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
|
||||
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')
|
||||
|
||||
@@ -118,10 +118,7 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
|
||||
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'])
|
||||
@@ -141,10 +138,7 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
|
||||
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'], {
|
||||
|
||||
Reference in New Issue
Block a user