Skip to content

Send In Blue Transactional

David edited this page Jun 13, 2022 · 3 revisions

Transaction Emails with Sendinblue

This module is used to send transactional emails, and to manage those templates.

  • /app/lib/send-in-blue-transactional

Usage

import { SibGetTemplateId, SibDeleteSmtpTemplate, SibSendTransacEmail } from '../send-in-blue-transactional'

async SibGetTemplateId( fileNameRoot )

  • fileNameRoot is like 'jest-test' which refers to the file /assets/email-templates/jest-test.html
  • returns a number which is the template id to be passed in subsequent api calls. If a template by the name repo-name/fileNameRoot is found, that template id is returned.

A comparison of the HTML file is made with what is already there and error messages are logged about differences, but the call is a success. It may be that the template is later updated on the Sendinblue side by someone who is not a developer. This is fine. But the error messages will serve as a reminder that those updates should be brought back to the repo and checked in.

If a template is not found, then the html file by that name from /assets/email-templates is uploaded and the id is returned.

async SibSendTransacEmail(props)

props is an object of the properties to be passed to SendInBlue in the api call. See Send a Transactional Email for more details. The minimum is

{ to: [{email: you@yours.com, name: 'You Yours'}],
  templateId: 6,
  params: {...}
}

async SibDeleteSmtpTemplate(templateId)

  • templateId - the id of an existing template

Templates

Templates are placed in /assets/email-templates Templates are html files, with {{ param }} that are replaced by data when the message is sent. Using Visual Studio Code html files can be edited in split window mode, where the html is edited as text in one window, and viewed in another. Install the HTML Preview extension. After you open the HTML file, ctrl+k v or cmd+k v will open the preview window.

Templates can also be created on Sendinblue using there editor, and then exported into html which can be saved into a file in /assets/email-templates.

It is this system of using the repo name and file root name that makes it possible to manage template ids over time and repos and projects. Avoid creating a template in Sendinblue and then hardcoding the template Id.

Environment

Two Environment Variables need to be set in the .bashrc file:

export SENDINBLUE_API_KEY='xkeysib-axxxxxxxxxx'
export SENDINBLUE_DEFAULT_FROM_EMAIL='[email protected]'

Testing

The environment variables must be set for these tests to run. Otherwise they will be skipped without error. This testing has side effects:

  1. The jest-test template will be created in the SENDINBLUE account, with this repo name prepended.
  2. If there was already an undebate-ssp/jest-test template there, it will be deleted and a new one will be added.
  3. The Sendinblue templateId will always be incremented. (when a template is deleted, it's template id can never be reused)
  4. An email will be sent to the SENDINBLUE_DEFAULT_FROM_EMAIL that should be manually checked.
npm run test app/lib/__tests__/send-in-blue-transactional.js 

Setup

Go to Sendinblue.com to setup an account. This slide deck walks though signing up on Sendinblue and getting the SENDINBLUE_API_KEY. Developers can do this for testing using a personal email, without charge. Send In Blue Account Sign Up