How to Send Email in Nodejs

Sunday, July 19, 2020 • 4 minutes to read


This is a series of articles on how to send an email in nodejs.
In the first article, we will send an email using the node command. Later, we will create a APIs and a frontend.

Sending emails in nodejs have only 2 requirements:

  • Mail service provider (For ex. Gmail, Sendinblue, Mailchimp)
  • Middleware to connect with mail service (For ex. nodemailer)



  • Node.js (>v6.0.0 for nodemailer module)
  • Code Editor (For Ex. VS Code, Atom)
  • An account in Sendinblue

Check out this step by step tutorial to Create an account in sendinblue.

Getting Started

For this series, we are going to use nodemailer package. It is an open source project.

Nodemailer Features

  • A single module with Zero dependencies
  • Support HTML Content
  • Add attachments to messages
  • Supports SMTP as default transport method. Other supported transport methods are SES, Sendmail, Stream.
  • OAuth2 authentication

For the mailing service, we are going to use Sendinblue. This mailing service offers many different solutions other than emails, for ex. SMS.
It provide 300 mails/day for a free account and paid account is also very cheap and offer more features compared to many other available mailing service provider.

Create a new Project

Create a new directory node-email. Open the terminal or cmd inside the directory.

Initiate the nodejs project using the npm init -y command. -y flag is optional, by passing this flag it uses the default arguments.

Install Nodejs Dependencies

Install nodemailer package.

1npm install --save nodemailer

Create a new SMTP Key in Sendinblue

This key will be used for authentication and sending the email via your sendinblue account.

Sign in to your Sendinblue account and click on SMTP & API from the top-right menu.

Go to SMTP tab and click on CREATE A NEW SMTP KEY. Copy the key and paste it somewhere safe.

⚠️ Note: If you lose this key, then you can't retrieve it. Then you have to create a new one and replace it accordingly.

Send Email

Create a index.js file and paste the below code.

 1"use strict";
 2const nodemailer = require("nodemailer");
 5 * sendEmail
 6 * @param {Object} mailObj - Email meta data and body
 7 * @param {String} from - Email address of the sender
 8 * @param {Array} recipients - Array of recipients email address
 9 * @param {String} subject - Subject of the email
10 * @param {String} message - message
11 */
12const sendEmail = async (mailObj) => {
13  const { from, recipients, subject, message } = mailObj;
15  try {
16    // Create a transporter
17    let transporter = nodemailer.createTransport({
18      host: "",
19      port: 587,
20      auth: {
21        user: "",
22        pass: "SMTP-KEY",
23      },
24    });
26    // send mail with defined transport object
27    let mailStatus = await transporter.sendMail({
28      from: from, // sender address
29      to: recipients, // list of recipients
30      subject: subject, // Subject line
31      text: message, // plain text
32    });
34    console.log(`Message sent: ${mailStatus.messageId}`);
35    return `Message sent: ${mailStatus.messageId}`;
36  } catch (error) {
37    console.error(error);
38    throw new Error(
39      `Something went wrong in the sendmail method. Error: ${error.message}`
40    );
41  }
44const mailObj = {
45  from: "",
46  recipients: [""],
47  subject: "Sending email by nodejs",
48  message: "Hello World;",
51sendEmail(mailObj).then((res) => {
52  console.log(res);

In the auth object, user will be your email address of sendinblue account and pass will be your SMTP KEY.

In the mailObj, from will be your email address of sendinblue account, recipients is an array of email addresses, subject is subject of email and message is plain text message.

Open the terminal and run the file.

1node index.js

On successful run, output will be

Message sent: <>
Message sent: <>

Now, go and check your mailbox. You have received a mail. If you could not find the mail, check in the promotion.



In this tutorial, we created a basic sendEmail function to send the email using the nodemailer package. In the next section, we are going to create the server and APIs and in the last section we will send the email using the frontend.

Cover is designed in Canva


PreviousHow to Send Email in Nodejs with Expressjs

NextHow to Send Email in Golang