Skip to content

Email sending

Igor Balos edited this page Mar 26, 2024 · 21 revisions

With Postmark you can send emails easy. Before diving into this chapter, make sure you have checked out our getting started page.

To send a simple email, all you need to do is to create Postmark ServerClient and send a message. You can validate the message by validating result.

For these API requests you will need to use a server API token.

const serverToken = "xxxx-xxxxx-xxxx-xxxxx-xxxxxx"
let postmark = require("postmark")
let client = new postmark.ServerClient(serverToken);

Send a simple message

client.sendEmail(
    {
        From: "[email protected]",
        To: "[email protected]",
        Subject: "test email",
        HtmlBody: "test",
        TextBody: "test"
    }
).then(response => {
    console.log(response.To);
    console.log(response.SubmittedAt);
    console.log(response.Message);
    console.log(response.MessageID);
    console.log(response.ErrorCode);
});

Sending a message with tag

client.sendEmail(
    {
        From: "[email protected]",
        To: "[email protected]",
        Subject: "test email",
        HtmlBody: "test",
        TextBody: "test",
        Tag: "test_tag"
    }
).then(response => {
    console.log(response.Message);
});

Sending a message with custom message stream

client.sendEmail(
    {
        From: "[email protected]",
        To: "[email protected]",
        Subject: "test email",
        HtmlBody: "test",
        TextBody: "test",
        MessageStream: "outbound"
    }
).then(response => {
    console.log(response.Message);
});

Sending a message with metadata

client.sendEmail(
    {
        From: "[email protected]",
        To: "[email protected]",
        Subject: "test email",
        HtmlBody: "test",
        Metadata: {"parameter1": "1", "parameter2": "2"}
    }
).then(response => {
    console.log(response.Message);
});

Sending a message with open tracking enabled

client.sendEmail(
    {
        From: "[email protected]",
        To: "[email protected]",
        Subject: "test email",
        HtmlBody: "test",
        TrackOpens: true
    }
);

Sending a message with link tracking enabled

Setting up link tracking is easy. All you need to to is set the message to track links with setting tracking type to:

  • HtmlOnly
  • TextOnly
  • HtmlAndText
  • None

This will allow Postmark to track links in text, html body of your message or both.

client.sendEmail(
    {
        From: "[email protected]",
        To: "[email protected]",
        Subject: "test email",
        HtmlBody: "test",
        TrackLinks: "TextOnly"
    }
);

Sending a message with attachments

Sending attachments is easy. All you need to do is create an array of attachments you plan to send. Keep in mind though that you need to Base64 encode content you plan to send.

let fs = require('fs');

let message = new postmark.Models.Message("[email protected]", "Test subject", "Html body", "Text body", "[email protected]");
const attachment1 = new postmark.Models.Attachment("report.txt", Buffer.from("test").toString("base64"), "text");
const attachment2 = new postmark.Models.Attachment("book.pdf", fs.readFileSync('/Folder/book.pdf').toString("base64"), "application/pdf");
message.Attachments = [attachment1, attachment2]

client.sendEmail(message).then(response => {
    console.log("Sending message");
    console.log(response.To);
    console.log(response.SubmittedAt);
    console.log(response.Message);
    console.log(response.MessageID);
    console.log(response.ErrorCode)
});

Sending a message with inline attachments

In order to send email with inline images, you will need to add images as attachments. These special attachments have to have ContentId. In your email message, in html body you can reference by ContentId to a specific image attachment to use it inline.

In order to do that, you will use similar process as for regular attachments.

let fs = require('fs');

// create a message that you plan to send
let message = new postmark.Models.Message("[email protected]", "Test subject", "<!DOCTYPE html><html><body><p>Hello world <img src=\"cid:image.jpg\"/></a></p></body></html>", "Text body", "[email protected]");

// load an attachment with provided name, base64 encoded string that represents it's content, type and content id 
const image: Attachment = new postmark.Models.Attachment("Picture", fs.readFileSync('/Users/path/image').toString("base64"), "image/jpg", "cid:image.jpg");

// attach inline image to message planned to be sent
message.Attachments = [image]

client.sendEmail(message).then(response => {
    console.log(response.To);
    console.log(response.SubmittedAt);
    console.log(response.Message);
});

Sending a message with custom headers

Library allows you to simply specify custom headers which you would like to be sent in your email message. Postmark library allows you to specify headers by passing an Array of all your headers.

Check out the code example.

let message = new postmark.Models.Message("[email protected]", "Test subject", "Html body", "Text body", "[email protected]");
message.Headers = [{Name: "header_name1", Value: "header_value1"}, {Name: "header_name2", Value: "header_value2"}];

client.sendEmail(message).then(response => {
    console.log("Sending message");
    console.log(response.To);
    console.log(response.SubmittedAt);
    console.log(response.Message);
    console.log(response.MessageID);
    console.log(response.ErrorCode)
});

Sending message to multiple recipients

client.sendEmail(
    {
        From: "[email protected]",
        To: "[email protected], [email protected]",
        Cc: "[email protected], [email protected]",
        Subject: "test email",
        HtmlBody: "test",
        TextBody: "test"
    }
).then(response => {
    console.log(response.Message);
});

Deliver email in batches

Sometimes you would like to send many emails at once. Postmark allows you to do that by sending array of emails in batches. In order to do that, check out the following code example:

client.sendEmailBatch(
    [{
        From: "[email protected]",
        To: "[email protected]",
        Subject: "first email",
        HtmlBody: "test body"
    },
    {
        From: "[email protected]",
        To: "[email protected]",
        Subject: "second email",
        TextBody: "test"
    }]
).then(response => {
    console.log(response[0].Message);
    console.log(response[1].Message);
});

Please note that the delivering email in batches will return a 200-level HTTP status, even when validation for individual messages may fail. Users of this endpoint should check the response for each message in the response from our API.

Embed images in emails

If you would like to embed images in your emails check out the following article with example.