How to use postmark email on node.js?

2.7k views Asked by At

I am new to node.js and not able to figure out how to reference js library postmark.js in node.js.

  var POSTMARK_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
var postmark = require("postmark")(POSTMARK_KEY);

postmark.send({
    "From": from, 
    "To": to, 
    "Subject": subject, 
    "TextBody": emailBody
}, function (err, to) {
if (err) {
    console.log(err);
    return;
}
console.log("Email sent to: %s", to);
});

I tried above code but not sure how to use postmark.js

Is there any easy way to have html email functionality using html templates in js?

4

There are 4 answers

0
Bergrebell On

You can find most of the information in the official wiki.

To send an email with a template use:

client.sendEmailWithTemplate({
    TemplateId:1234567,
    From: "[email protected]",
    To: "[email protected]",
    TemplateModel: {company: "wildbit"}
});
0
JP Toto On

You can use the "HtmlBody field to send html messages through postmark:

    postmark.send({
    "From": from, 
    "To": to, 
    "Subject": subject, 
    "TextBody": emailBody,
    "HtmlBody": "<h1>hellow</h1>"
}, function (err, to) {
if (err) {
    console.log(err);
    return;
}
console.log("Email sent to: %s", to);
});
0
JBB On

The method to send email using a template vi API with NodeJs is

sendEmailWithTemplate()

I could not esaily find this in the doc for NodeJs.

0
Inzamam Malik On

In official document it is described here with example https://postmarkapp.com/developer/integration/official-libraries#node-js

// Install with npm
npm install postmark --save
// Require
var postmark = require("postmark");

// Example request
var serverToken = "xxxx-xxxxx-xxxx-xxxxx-xxxxxx";
var client = new postmark.ServerClient(serverToken);

client.sendEmail({
    "From": "[email protected]",
    "To": "[email protected]",
    "Subject": "Test",
    "TextBody": "Hello from Postmark!" 
});

In order to send html body you can send "HtmlBody": "<h1>some html in string form</h1>" along with "TextBody": "Hello from Postmark!"

like this:

client.sendEmail({
    "From": "[email protected]",
    "To": "[email protected]",
    "Subject": "Test",
    "TextBody": "Hello from Postmark!"
    "HtmlBody": "<h1>some html in string form</h1>" 
});

which they have described here: https://postmarkapp.com/developer/api/email-api#send-a-single-email