diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e59e2a5..f771ee6 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -33,8 +33,8 @@ vNext Fixes ~~~~~ -* **Postmark:** Fix Postmark API error when sending with a template that doesn't - require any merge data. (Thanks to `@Tobeyforce`_ for reporting it.) +* **Postmark:** Fix two different errors when sending with a template but no merge + data. (Thanks to `@kareemcoding`_ and `@Tobeyforce`_ for reporting them.) v8.2 @@ -1226,6 +1226,7 @@ Features .. _@janneThoft: https://github.com/janneThoft .. _@jc-ee: https://github.com/jc-ee .. _@joshkersey: https://github.com/joshkersey +.. _@kareemcoding: https://github.com/kareemcoding .. _@kika115: https://github.com/kika115 .. _@Lekensteyn: https://github.com/Lekensteyn .. _@lewistaylor: https://github.com/lewistaylor diff --git a/anymail/backends/postmark.py b/anymail/backends/postmark.py index 9ca82a4..05535e2 100644 --- a/anymail/backends/postmark.py +++ b/anymail/backends/postmark.py @@ -160,7 +160,7 @@ class PostmarkPayload(RequestsPayload): super().__init__(message, defaults, backend, headers=headers, *args, **kwargs) def get_api_endpoint(self): - batch_send = self.is_batch() and len(self.to_emails) > 1 + batch_send = self.is_batch() if 'TemplateAlias' in self.data or 'TemplateId' in self.data or 'TemplateModel' in self.data: if batch_send: return "email/batchWithTemplates" @@ -187,8 +187,8 @@ class PostmarkPayload(RequestsPayload): elif api_endpoint == "email/batch": data = [self.data_for_recipient(to) for to in self.to_emails] elif api_endpoint == "email/withTemplate/": - assert len(self.to_emails) == 1 - data = self.data_for_recipient(self.to_emails[0]) + assert self.merge_data is None and self.merge_metadata is None # else it's a batch send + data = self.data else: raise AssertionError("PostmarkPayload.serialize_data missing" " case for api_endpoint %r" % api_endpoint) diff --git a/tests/test_postmark_backend.py b/tests/test_postmark_backend.py index 5f10921..9c0fc42 100644 --- a/tests/test_postmark_backend.py +++ b/tests/test_postmark_backend.py @@ -398,6 +398,19 @@ class PostmarkBackendAnymailFeatureTests(PostmarkBackendMockAPITestCase): # Postmark requires TemplateModel (can be empty) with TemplateId/TemplateAlias self.assertEqual(data['TemplateModel'], {}) + def test_template_multiple_recipients(self): + # This is a non-batch (no merge_data) template send + message = AnymailMessage( + from_email='from@example.com', + to=['to@example.com', "Also to "], + template_id=1234567, + ) + message.send() + self.assert_esp_called('/email/withTemplate/') + data = self.get_api_call_json() + self.assertEqual(data['To'], 'to@example.com, Also to ') + self.assertEqual(data['TemplateId'], 1234567) + _mock_batch_response = json.dumps([{ "ErrorCode": 0, "Message": "OK", @@ -464,14 +477,17 @@ class PostmarkBackendAnymailFeatureTests(PostmarkBackendMockAPITestCase): ) message.send() - self.assert_esp_called('/email/withTemplate/') + # because merge_data is set, it's treated as a batch send + self.assert_esp_called('/email/batchWithTemplates') data = self.get_api_call_json() self.assertEqual(data, { - "From": "from@example.com", - "To": "alice@example.com", - "TemplateId": 1234567, - "TemplateModel": {"name": "Alice", "group": "Developers", "site": "ExampleCo"}, + 'Messages': [{ + "From": "from@example.com", + "To": "alice@example.com", + "TemplateId": 1234567, + "TemplateModel": {"name": "Alice", "group": "Developers", "site": "ExampleCo"}, + }] }) recipients = message.anymail_status.recipients