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:
medmunds
2016-06-22 16:54:40 -07:00
parent 5adb07a1be
commit b664ee3dbc
2 changed files with 17 additions and 18 deletions

View File

@@ -87,15 +87,20 @@ class SparkPostPayload(BasePayload):
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