How to send emails from Node.js with SendInBlue

On this article, we are going to discover ways to ship emails from Node.js with SendInBlue.



Video Tutorial




What’s SendInBlue?

SendInBlue is a mail service that permits you to ship emails out of your Node.js utility.



Get Sendinblue electronic mail api key

  1. Go to SendinBlue and create an account.
  2. Go to Dashboard and click on on the highest right-hand aspect dropdown.
  3. Click on on the SMTP & API tab.
  4. Click on on the Create new API key button.
  5. Now we have to retailer the api key in an setting variable.



Setup

npm init -y
npm i dotenv sib-api-v3-sdk
Enter fullscreen mode

Exit fullscreen mode

  • Create a file known as .env and add the next traces:
API_KEY=<your_api_key>
Enter fullscreen mode

Exit fullscreen mode

  • Create a file known as index.js and add the next traces:
const Sib = require('sib-api-v3-sdk')

require('dotenv').config()

const consumer = Sib.ApiClient.occasion

const apiKey = consumer.authentications['api-key']
apiKey.apiKey = course of.env.API_KEY
Enter fullscreen mode

Exit fullscreen mode

Rationalization: – require('dotenv').config(): That is used to load the setting variables from the .env file. Then we’ve got so as to add the api key to the Sendinblue consumer.

const tranEmailApi = new Sib.TransactionalEmailsApi()

const sender = {
    electronic mail: 'thatanjan@gmail.com',
    identify: 'Anjan',
}

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

Exit fullscreen mode

Rationalization: With tranEmailApi we will ship emails. The sender electronic mail must be the e-mail account that you’ve used within the SendinBlue account.

tranEmailApi
    .sendTransacEmail({
        sender,
        to: receivers,
        topic: 'Subscribe to Cules Coding to turn into a developer',
        textContent: `
        Cules Coding will train you how one can turn into {{params.function}} a developer.
        `,
        htmlContent: `
        <h1>Cules Coding</h1>
        <a href="https://cules-coding.vercel.app/">Go to</a>
                `,
        params: {
            function: 'Frontend',
        },
    })
    .then(console.log)
    .catch(console.log)
Enter fullscreen mode

Exit fullscreen mode

Rationalization:

node index.js
Enter fullscreen mode

Exit fullscreen mode

Sendinblue has templates that you should utilize. If you need me to show you how one can create a publication, please let me know. That is it for immediately. Have an important day.

Add a Comment

Your email address will not be published. Required fields are marked *