Postmark: Send email with template
Asked Answered
P

2

6

I am trying to send a template email with Postmark in Node.js

I created a template on the Postmark App website. I've looked through their documentation, but cannot find any way to go about sending a templated email.

Documentation Sources:

http://blog.postmarkapp.com/post/125849089273/special-delivery-postmark-templates http://developer.postmarkapp.com/developer-api-templates.html

I've tried a variety of methods, including:

client.emailWithTemplate("[email protected]",
   "[email protected]",<template-id>, {
   "link" : "https://example.com/reset?key=secret",
   "recipient_name" : "Jenny"
});

TypeError: Object # has no method 'emailWithTemplate'

client.sendEmail({
    "TemplateModel" : {
        "customer_name" : "Jenny",
    },
    "TemplateId" : 6882,
    "From": "[email protected]",
    "To": "[email protected]",
}, function(error, success) {
    if(error) {
        console.log(error);
    } else {
        console.log(success);
    }
});

Console Log Error: { status: 422, message: 'A \'TemplateId\' must not be used when sending a non-templated email.', code: 1123 }

Thanks!

Pattison answered 18/8, 2015 at 20:2 Comment(0)
W
6

I'm the current maintainer of the node.js library (as well as one of the engineers that worked on Postmark Templates).

One of the possible reasons the original snippet doesn't work is that you could be using an older version of Postmark.js. We added the template endpoint capabilities in version 1.2.1 of the node.js package.

In the package.json file for your project you should make sure to update it to use version 1.2.1 or greater of the postmark.js library. If you've been using an older version of the library, you'll also need to run npm update

Also note that if you click "Edit Template" in the Postmark UI, and then "API Snippets," the UI provides a completed snippet for a number of languages (including node.js).

If all else fails, please contact support and we'll be happy to help you solve this issue.

Wagtail answered 19/8, 2015 at 1:49 Comment(3)
Wow! Didn't realize it was that simple. I just got this a few days ago, but it was in fact version 1.0 -- now that I've updated, it's working like a charm.Pattison
Thank you so much for clarifying this! :)Pattison
@Pattison My pleasure, thanks for trying out Templates. We think they're great.Wagtail
M
2

In 2024, you should use the function:

   sendEmailWithTemplate(template: TemplatedMessage, callback?: Callback<MessageSendingResponse>): Promise<MessageSendingResponse>

Couldn't find the reference in the official docs but it's well documented in Message.d.ts file once you install the package.

Suprisingly, the TemplateId is a number value you can retrieve from the Postmark panel on the right top called "ID" but you can also use TemplateAlias

Mcknight answered 22/2 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.