Lindsay
Lindsay

Reputation: 937

Postmark: Send email with template

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!

Upvotes: 6

Views: 4241

Answers (2)

Greg Wozniak
Greg Wozniak

Reputation: 7232

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

Upvotes: 2

Andrew Theken
Andrew Theken

Reputation: 3480

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.

Upvotes: 6

Related Questions