Skip to content

Commit 920d8dd

Browse files
authored
SendGrid: Fix multiple recipients with only merge_global_data
In SendGrid backend, support non-batch template send to multiple recipients when `merge_global_data` is set without `merge_data`. Regression introduced in v6.0. Fixes #179
1 parent 4245d46 commit 920d8dd

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

CHANGELOG.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ Release history
2525
^^^^^^^^^^^^^^^
2626
.. This extra heading level keeps the ToC from becoming unmanageably long
2727
28+
vNext
29+
-----
30+
31+
*Unreleased changes on master*
32+
33+
Fixes
34+
~~~~~
35+
36+
* **SendGrid:** Allow non-batch template send to multiple recipients when
37+
`merge_global_data` is set without `merge_data`. (Broken in v6.0. Thanks to
38+
`@vgrebenschikov`_ for the bug report.)
39+
40+
2841
v7.0
2942
----
3043

@@ -1021,4 +1034,5 @@ Features
10211034
.. _@sebbacon: https://github.com/sebbacon
10221035
.. _@Thorbenl: https://github.com/Thorbenl
10231036
.. _@varche1: https://github.com/varche1
1037+
.. _@vgrebenschikov: https://github.com/vgrebenschikov
10241038
.. _@yourcelf: https://github.com/yourcelf

anymail/backends/sendgrid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ def build_merge_data(self):
133133
if self.merge_data or self.merge_global_data:
134134
# Always build dynamic_template_data first,
135135
# then convert it to legacy template format if needed
136+
only_global_merge_data = self.merge_global_data and not self.merge_data
136137
for personalization in self.data["personalizations"]:
137-
assert len(personalization["to"]) == 1
138+
assert len(personalization["to"]) == 1 or only_global_merge_data
138139
recipient_email = personalization["to"][0]["email"]
139140
dynamic_template_data = self.merge_global_data.copy()
140141
dynamic_template_data.update(self.merge_data.get(recipient_email, {}))

tests/test_sendgrid_backend.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,20 @@ def test_explicit_dynamic_template(self):
493493
'custom_args': {'anymail_id': 'mocked-uuid-2'},
494494
'substitutions': {"<%test%>": "data"}}])
495495

496+
def test_merge_data_global_only(self):
497+
# a template with only global data can be used to send the same message
498+
# to multiple recipients (non-batch)
499+
self.message.template_id = "d-5a963add2ec84305813ff860db277d7a"
500+
self.message.merge_global_data = {"test": "data"}
501+
self.message.to = ["[email protected]", "[email protected]"]
502+
self.message.send()
503+
504+
data = self.get_api_call_json()
505+
self.assertEqual(data['personalizations'], [
506+
{'to': [{'email': '[email protected]'}, {'email': '[email protected]'}], # not batch
507+
'custom_args': {'anymail_id': 'mocked-uuid-1'},
508+
'dynamic_template_data': {"test": "data"}}])
509+
496510
def test_legacy_merge_data(self):
497511
# unless a new "dynamic template" is specified, Anymail assumes the legacy
498512
# "substitutions" format for merge data

0 commit comments

Comments
 (0)