2

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?

updev
  • 623
  • 5
  • 14
  • 32

4 Answers4

2

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);
});
JP Toto
  • 1,032
  • 9
  • 10
1

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": "sender@example.com",
    "To": "receiver@example.com",
    "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": "sender@example.com",
    "To": "receiver@example.com",
    "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

Inzamam Malik
  • 3,238
  • 3
  • 29
  • 61
1

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.

JBB
  • 317
  • 1
  • 9
1

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

To send an email with a template use:

client.sendEmailWithTemplate({
    TemplateId:1234567,
    From: "from@example.com",
    To: "to@example.com",
    TemplateModel: {company: "wildbit"}
});
Bergrebell
  • 4,263
  • 4
  • 40
  • 53