How to send emails with NodeJS using Gmail account

Suhail Roushan 🚀
3 min readApr 20, 2023

--

Email Image
Photo by Solen Feyissa on Unsplash

Sending emails programmatically has become a common practice in many applications. In this blog, we will explore how to send emails using Node.js and Gmail. Gmail provides a secure SMTP server that can be used to send emails programmatically. We will use the nodemailer package to send emails using our Gmail account.

Prerequisites

Before we begin, make sure you have the following:

  • Node.js installed on your system.
  • A Gmail account.

Step 1: Install nodemailer

To send emails using Node.js, we need to install the nodemailer package. We can install it using npm, which is the package manager for Node.js. Run the following command to install nodemailer:

npm install nodemailer

Step 2: Create a Gmail account

If you don’t have a Gmail account, create one. We will use this account to send emails programmatically.

Step 3: Enable less secure apps

By default, Gmail blocks access to less secure apps, including applications that use SMTP authentication. We need to enable less secure apps for our Gmail account to send emails using Node.js. Follow the steps below to enable less secure apps:

  1. Go to your Gmail account settings.
  2. Click on “Security” in the left sidebar.
  3. Scroll down to the “Less secure app access” section and click on “Turn on access (not recommended)”.

Step 4: Write the code

Now that we have installed nodemailer and enabled less secure apps, we can write the code to send emails using Node.js and Gmail.

Create a new file called send-email.js and paste the following code:

import nodemailer from 'nodemailer';

const sendEmail = async () => {
try {
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your-email@gmail.com',
pass: 'your-password',
},
});

const mailOptions = {
from: '"Your Name" <your-email@gmail.com>',
to: 'recipient-email@example.com',
subject: 'Hello ✔',
text: 'Hello world?',
html: '<b>Hello world?</b>',
};

const info = await transporter.sendMail(mailOptions);
console.log(`Message sent: ${info.messageId}`);
} catch (error) {
console.log(error);
}
};

sendEmail();

In the above code, replace the following fields with your own information:

  • user: Your Gmail account email address.
  • pass: Your Gmail account password.
  • from: Your name and email address.
  • to: The email address of the recipient.
  • subject: The subject of the email.
  • text: The plain text content of the email.
  • html: The HTML content of the email.

Save the file and run it using the following command:

node send-email.js

If everything is set up correctly, you should see a message that says “Message sent” along with the message ID.

Congratulations! You have successfully sent an email using Node.js and Gmail.

Conclusion

In this blog, we explored how to send emails using Node.js and Gmail. We used the nodemailer package to send emails programmatically and followed a few simple steps to enable less secure apps in our Gmail account. With this knowledge, you can now integrate email sending functionality into your Node.js applications.

--

--

Suhail Roushan 🚀

⭐ Software Engineer at T-Works 🧑‍💻 Full Stack Web Developer 🎓 CS.CODE.IN Fellow '21 💡 Startup Tech Innovator 👨‍🎨 UI/UX Designer 🤖 Automation