For a project I’m working on, I wanted to be able to send emails to notify my users of events and updates.
To begin with, I just wanted to send a simple email to myself to get everything working for now and learn how to use the API. I’m posting this as their current documentation isn’t very up-to-date at the time of posting. I’m using SendinBlue as I already use it behind the scenes for my website’s emails.

What is a Transactional Email?

Let’s get this out of the way, there are two types of email with SendinBlue: Campain and Transactional. Campaigns are just as they seem, used to send mass emails to all of your subscribers. This is used for promotions among other things. Transactional emails are more direct, usually used for sending password reset, verification and one-off notification emails.

The Setup

Setup with Node.js is pretty simple, in my project I have created a file for handling emails and, soon, it’ll handle text messaging too. Feel free to have a look at their v3 API over on GitHub. There’s a lot of information there with plenty of functions for everything.

To install the package, just open your working directory in a terminal and type:

npm install sib-api-v3-sdk --save

Then open the file you want to handle emails, mine’s simply called systemMailer.js and require the SDK along with adding the needed configuration. I won’t go into detail as it’s pretty self-explanatory.

var SibApiV3Sdk = require("sib-api-v3-sdk");
var defaultClient = SibApiV3Sdk.ApiClient.instance;

// Configure API key authorization: api-key
var apiKey = defaultClient.authentications["api-key"];
apiKey.apiKey =
  "API_KEY_HERE";

The Implementation

For this next part, I’ll include a simple function I use. It’s pretty simple and mostly copied from the official docs, just with a few tweaks. These tweaks include adding the sender email address, subject and textContent and removing templateId and params. These are for sending from a template on the SendinBlue website. While I will make use of this later down the line, I don’t need it now. Simple plain-text emails will do.

function SendTestEmail(address) {
  var apiInstance = new SibApiV3Sdk.TransactionalEmailsApi();
  var sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail(); // SendSmtpEmail | Values to send a transactional email

  sendSmtpEmail = {
    sender: { email: "[email protected]" },
    to: [
      {
        email: "[email protected]",
        name: "Person Name",
      },
    ],
    subject: "Test Email",
    textContent: "Test Email Content",
  };

  apiInstance.sendTransacEmail(sendSmtpEmail).then(
    function (data) {
      console.log("API called successfully. Returned data: " + data);
    },
    function (error) {
      console.error(error);
    }
  );
}

This creates the API Instance and SMTP objects required to send the email. Below the creation of these objects, we’re using sendSmtpEmail to also set different variables that are required to send the email. Make sure the sender address matches one in your SendinBlue account or you may face errors. After all of these, we’re using the API Instance and calling sendTransacEmail, which sends the email. If there’s a problem, you’ll be notified in the console or check your SendinBlue dashboard for logs.

If you’re new here you may not know I do quite a few things with software from game development to app development. The reason for me using this is for an app I’m currently working on. This Node.js project is for its backend API.

Categories: Tutorials