Skip to content

Commit 10f6f3f

Browse files
committed
Postmark: Support both TemplateAlias and TemplateId as template_id
Accept either Postmark's template alias or numeric id for `template_id`.
1 parent b59aadd commit 10f6f3f

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ Breaking changes
4040
code depended on "temporary failure" showing up as "bounced" you will need to update it.
4141
(Thanks `@costela`_.)
4242

43+
Features
44+
~~~~~~~~
45+
46+
* **Postmark:** Allow either template alias (string) or numeric template id for
47+
Anymail's `template_id` when sending with Postmark templates.
48+
4349
Fixes
4450
~~~~~
4551

anymail/backends/postmark.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def __init__(self, message, defaults, backend, *args, **kwargs):
139139

140140
def get_api_endpoint(self):
141141
batch_send = self.merge_data is not None and len(self.to_emails) > 1
142-
if 'TemplateId' in self.data or 'TemplateModel' in self.data:
142+
if 'TemplateAlias' in self.data or 'TemplateId' in self.data or 'TemplateModel' in self.data:
143143
if batch_send:
144144
return "email/batchWithTemplates"
145145
else:
@@ -257,7 +257,11 @@ def set_track_opens(self, track_opens):
257257
self.data["TrackOpens"] = track_opens
258258

259259
def set_template_id(self, template_id):
260-
self.data["TemplateId"] = template_id
260+
try:
261+
self.data["TemplateId"] = int(template_id)
262+
except ValueError:
263+
self.data["TemplateAlias"] = template_id
264+
261265
# Subject, TextBody, and HtmlBody aren't allowed with TemplateId;
262266
# delete Django default subject and body empty strings:
263267
for field in ("Subject", "TextBody", "HtmlBody"):

docs/esps/postmark.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,14 @@ and :ref:`batch sending <batch-send>` with per-recipient merge data.
137137
:attr:`~anymail.message.AnymailMessage.merge_global_data` with Postmark.)
138138

139139
To use a Postmark template, set the message's
140-
:attr:`~anymail.message.AnymailMessage.template_id` to the numeric Postmark
141-
"TemplateID" (*not* its name or "TemplateAlias"). You can find a template's
142-
numeric id near the top right in Postmark's template editor.
140+
:attr:`~anymail.message.AnymailMessage.template_id` to either the numeric Postmark
141+
"TemplateID" or its string "TemplateAlias" (which is *not* the template's name).
142+
You can find a template's numeric id near the top right in Postmark's template editor,
143+
and set the alias near the top right above the name.
144+
145+
.. versionchanged:: 5.0
146+
147+
Earlier Anymail releases only allowed numeric template IDs.
143148

144149
Supply the Postmark "TemplateModel" variables using Anymail's normalized
145150
:attr:`~anymail.message.AnymailMessage.merge_data` and
@@ -151,7 +156,7 @@ Supply the Postmark "TemplateModel" variables using Anymail's normalized
151156
# (subject and body come from the template, so don't include those)
152157
153158
)
154-
message.template_id = 80801 # Postmark template id
159+
message.template_id = 80801 # Postmark template id or alias
155160
message.merge_data = {
156161
'[email protected]': {'name': "Alice", 'order_no': "12345"},
157162
'[email protected]': {'name': "Bob", 'order_no': "54321"},

tests/test_postmark_backend.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,17 @@ def test_template(self):
385385
self.assertNotIn('HtmlBody', data)
386386
self.assertNotIn('TextBody', data)
387387

388+
def test_template_alias(self):
389+
# Anymail template_id can be either Postmark TemplateId or TemplateAlias
390+
message = AnymailMessage(
391+
from_email='[email protected]', to=['[email protected]'],
392+
template_id='welcome-message',
393+
)
394+
message.send()
395+
self.assert_esp_called('/email/withTemplate/')
396+
data = self.get_api_call_json()
397+
self.assertEqual(data['TemplateAlias'], 'welcome-message')
398+
388399
def test_merge_data(self):
389400
self.set_mock_response(raw=json.dumps([{
390401
"ErrorCode": 0,

0 commit comments

Comments
 (0)