In this article, you will learn how to send emails from Node.js with SendInBlue.

What is SendInBlue?

SendInBlue is an email service that allows you to send emails from your Node.js application.

Get the Sendinblue email API key

  1. Go to SendinBlue and create an account.
  2. Go to Dashboard and click on the top right-hand side.
  3. Click on the SMTP & API tab.
  4. Click on the Create new API key button.
  5. Now we need to store the API key in an environment variable.

Setup

npm init -y
npm i dotenv sib-api-v3-sdk

API_KEY=<your_api_key>

const Sib = require('sib-api-v3-sdk')

require('dotenv').config()

const client = Sib.ApiClient.instance

const apiKey = client.authentications['api-key']
apiKey.apiKey = process.env.API_KEY

Explanation:

require('dotenv').config(): This is used to load the environment variables from the .env file.

const tranEmailApi = new Sib.TransactionalEmailsApi()

const sender = {
	email: '[email protected]',
	name: 'Anjan',
}

const receivers = [
	{
		email: '<email address>',
	},
]

Explanation

With tranEmailApi we can send emails.

tranEmailApi
	.sendTransacEmail({
		sender,
		to: receivers,
		subject: 'Subscribe to Cules Coding to become a developer',
		textContent: `
        Cules Coding will teach you how to become {{params.role}} a developer.
        `,
		htmlContent: `
        <h1>Cules Coding</h1>
        <a href="https://cules-coding.vercel.app/">Visit</a>
                `,
		params: {
			role: 'Frontend',
		},
	})
	.then(console.log)
	.catch(console.log)

Explanation:

node index.js

Sendinblue has templates that you can use. If you want me to teach you how to create a newsletter, please let me know.


By the way, I am looking for a new opportunity in a company where I can provide great value with my skills. If you are a recruiter, looking for someone skilled in full-stack web development and passionate about revolutionizing the world, please contact me. Also, I am open to talking about any freelance project. I am available on Upwork.

Thank you so much for reading this blog.


Also published here or watch the video tutorial here.