Skip to content

Commit

Permalink
Allow sending batch with templates
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasserra committed Oct 9, 2020
1 parent 3b47731 commit d9c0f54
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ build/
env/
.pypirc
MANIFEST
venv
.python-version
27 changes: 24 additions & 3 deletions postmark/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ class PMBatchMail(object):
def __init__(self, **kwargs):
self.__api_key = None
self.__messages = []
self.__template = False

acceptable_keys = (
'api_key',
Expand Down Expand Up @@ -606,6 +607,15 @@ def __init__(self, **kwargs):
'''
)

template = property(
lambda self: self.__template,
lambda self, value: setattr(self, '_PMBatchMail__template', value),
lambda self: setattr(self, '_PMBatchMail__template', None),
'''
Bool to check send with template
'''
)

def add_message(self, message):
'''
Add a message to the batch
Expand All @@ -625,6 +635,10 @@ def _check_values(self):
type and are not missing.
'''
for message in self.__messages:
# Check list of messages to see if sending using templates
if not self.__template and (message.template_id or message.template_model):
self.__template = True

message._check_values()

def send(self, test=None):
Expand All @@ -648,9 +662,16 @@ def send(self, test=None):
for message in messages:
json_message.append(message.to_json_message())

if not self.__template:
endpoint_url = __POSTMARK_URL__ + 'email/batch'
payload = json.dumps(json_message, cls=PMJSONEncoder).encode('utf8')
else:
endpoint_url = __POSTMARK_URL__ + 'email/batchWithTemplates'
payload = json.dumps({'Messages': json_message}, cls=PMJSONEncoder).encode('utf8')

req = Request(
__POSTMARK_URL__ + 'email/batch',
json.dumps(json_message, cls=PMJSONEncoder).encode('utf8'),
endpoint_url,
payload,
{
'Accept': 'application/json',
'Content-Type': 'application/json',
Expand Down Expand Up @@ -934,7 +955,7 @@ def activate(self, bounce_id):
# print req_url
h1 = HTTPConnection('api.postmarkapp.com')
dta = urlencode({"data": "blank"}).encode('utf8')
req = h1.request(
h1.request(
'PUT',
req_url,
dta,
Expand Down

0 comments on commit d9c0f54

Please sign in to comment.