How to send emails from Node.js with SendInBlue
In this article, we will learn how to send emails from Node.js with SendInBlue.
Video Tutorial
What is SendInBlue?
SendInBlue is a mail service that allows you to send emails from your Node.js application.
Get Sendinblue email api key
Go to SendinBlue and create an account.
Go to Dashboard and click on the top right-hand side dropdown.
Click on the SMTP & API tab.
Click on the
Create new API key
button.Now we need to store the api key in an environment variable.
Setup
- Install packages:
npm init -ynpm i dotenv sib-api-v3-sdk
- Create a file called
.env
and add the following lines:
API_KEY=<your_api_key>
- Create a file called
index.js
and add the following lines:
const Sib = require('sib-api-v3-sdk')require('dotenv').config()const client = Sib.ApiClient.instanceconst 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. Then we have to add the api key to the Sendinblue client.
const tranEmailApi = new Sib.TransactionalEmailsApi()const sender = {email: 'thatanjan@gmail.com',name: 'Anjan',}const receivers = [{email: '<email address>',},]
Explanation: With tranEmailApi
we can send emails. The sender email has to be the email account that you have used in the SendinBlue account.
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:
You can send emails using the
sendTransacEmail
method.Subject is required.
You have to pass either
textContent
orhtmlContent
to the method.htmlContent
will overridetextContent
.You can pass parameters to the email content using the
params
object.Run the file and you will see the email was sent.
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.
Shameless Plug
Want to create your own blog? Well, I am creating a video series where you will learn about how to create a JAMstack blog with Nextjs and Chakra-UI.
Lessons
- Intro & Setup
- Build Homepage UI
- How our app will work
- MDX, MongoDB, Static Homepage
- Generate Static Blog Page
- Style Blog page with Chakra-UI and MDX-embed
- Build a real-time view counter
- Autocomplete search form with MongoDB Atlas Search Index
- Deploy application to Vercel
Demo
You can demo the website from here
Features
- Static Blog pages will make the website load faster.
- Blogs will have code blocks with syntax highlighting and many embed components like youtube videos, GitHub gist, Tweets, and so many other things.
- Autocomplete search feature for the blog posts.
- Real-time view counter and so on.
Please like and subscribe to Cules Coding. It motivates me to create more content like this.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
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, feel free to contact me. Also, I am open to talking about any freelance project. I am available on Upwork
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram (personal): @thatanjan
- Twitter: @thatanjan
- Upwork: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: