Sending EMail through Postmark in Node JS.

Posted By : Satish Joshi | 31-Jul-2018

 

Send emails with the greatest of ease! Now your node.js can send emails via Postmark. 

This is pretty easy to use - simply sign up over at http://postmarkapp.com/, follow their instructions to get your API key and putting your own API key or Token, use it in your application and send the email.

 

Installation:-

 

First, we need to install its dependency by using this command :

 

npm install postmark --save

 

Sending Email:-

 

To send an Email we need to follow this structure :

 

// Require:
var postmark = require("postmark");

// Send an email:
var client = new postmark.Client("<server key>");

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

 

Replace the server key with Postmark Server API Token which you will get from the above link and you are good to go!

 

The properties or members used in sendEmail method like From, To, Subject, TextBody, HtmlBody, Attachments are case-sensitive. Also, the client method of postmark accepts a callback as their last parameter which is an optional callback function, allowing you to handle any errors, and to examine the API response with the following convention :

 

callback(err, result){...} 

 

To send attachments with the email, use the following format may be used:

 

Example:-

 

var postmark = require("postmark");
var client = new postmark.Client("<server key>");
var fs = require('fs');

client.sendEmail({
    "From": "[email protected]", 
    "To": "[email protected]", 
    "Subject": "Test", 
    "TextBody": "Test Message",
    "Attachments": [{
      "Content": fs.readFileSync("./unicorns.jpg").toString('base64'),
      "Name": "PrettyUnicorn.jpg",
      "ContentType": "image/jpeg"
    }]
}, function(error, result) {
    if(error) {
        console.error("Unable to send via postmark: " + error.message);
        return;
    }
    console.info("Sent to postmark for delivery")
});

 

We can also send a batch of messages through Postmark.  As this API provides functionality for sending batches of emails with a single command, rather than issuing separate API calls for each message.

 

Example:-

 

var postmark = require("postmark");
var client = new postmark.Client("<server key>");

var messages = [
    {
        "From": "[email protected]",
        "To": "[email protected]",
        "Subject": "Message #1",
        "TextBody": "This is email number 1."
    },
    {
        "From": "[email protected]",
        "To": "[email protected]",
        "Subject": "Message #2",
        "TextBody": "This is email number 2."
    }
];

client.sendEmailBatch(messages, function (error, batchResults) {
    if (error) {
        console.error("Unable to send via postmark: " + error.message);
        return;
    }
    console.info("Messages sent to postmark");
});

 

It will return the array of status for each message sent. For further details, please see the Postmark Batch API link: https://postmarkapp.com/developer/user-guide/sending-email/sending-with-api#batch-emails.

 

Sending an Email using a Template:-

 

  • 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.
  • We need to use sendEmailWithTemplate method name to send an email in postmark which is similar to sending a single email, but there is a little bit of setup.
  • First, you need to include the TemplateId, and TemplateModel properties which are basically the ID and Name of an Email Template that you are using. 
  • Next, remove the Subject, TextBody, and HtmlBody properties that you normally include when sending a non-templated email.

 

Example:-

 

var postmark = require("postmark");
var client = new postmark.Client("<server key>");

client.sendEmailWithTemplate({
    "From": "[email protected]", 
    "TemplateId": <templateId>,
    "To": "[email protected]", 
    "TemplateModel": {
        "Property1" : 1,
        "Property2" : "hello"
    }
});

 

Happy Sending!

 

About Author

Author Image
Satish Joshi

Satish has experience on web development believes in hard work and dedication. He is friendly and an honest person.

Request for Proposal

Name is required

Comment is required

Sending message..