Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Send Mail in Nodejs Using a Gmail Account for Free

Let’s learn to send Mail in Nodejs using a Gmail Account, this is made possible using nodemailer node package.

Introduction

Nodemailer is a module for Node.js applications that allows applications to send mail. The project started in 2010. Nodemailer is one of the default options to send mail in NodeJs. It provides a transport layer using the createTransport({/*OPTIONS*/}) , which allows a user to send mail through different service providers like AWS, google, and even your own mailing server. So using nodemailer let us send mail in NodeJs using a Gmail account.

Prerequisites

  1. Basic understanding of Nodejs

Mail in Nodejs using a Gmail Account

For sending mail in Nodejs using a Gmail account, you need to create an application-specific password in Gmail, so let us look at that.

Generating Application Specific password

Following the steps given below let’s create an application-specific password to use in our node application.

1. Get into your Gmail account and click on the Manage your Google Account option by clicking on your profile image as highlighted in the image below.

Getting into account settings

2. After getting to your manage your account page click on the security option as highlighted below, then search for the option App passwords option and click on it.

Getting into the App Passwords page

3. Now that you are on the App passwords page, choose Mail in the first option as highlighted below, and select Other (Custom name).

Selecting the type of application

4. Give a name for the password as it makes it easier to reference in the future. Then click on Generate to receive the password.

Generating the application-specific password

5. You will receive the password shown below, now copy the password and keep it safe as this will be used in our application, as this password is used to interact with the applications that don’t support 2-factor authentication.

Your application-specific password

Now, that the password is generated, let’s get on with creating our node application, to achieve our goal of sending mail in Nodejs using a Gmail account.

Integrating in Nodejs

Firstly create a node application, install the required packages, and make the required files.

## creating a node application
$ mkdir scanskill-email-gmail
$ cd scanskill-email-gmail
$ npm init --y

## install required packages
$ npm install nodemailer dotenv

## creating required files
$ touch index.js
$ touch .env

## open the application with a code editor
$ code .

In the .env file insert your email and the application-specific password that we generated here, as we should use these kinds of variables from the environment.

# .env file

GOOGLE_EMAIL=****YOUR_EMAIL****
GOOGLE_PASSWORD=****YOUR_APPLICATION_SPECIFIC_PASSWORD****

Let’s edit the index.js file, and implement the nodemailer to send the mail.

//* index.js

//* importing the packages
const nodemailer = require("nodemailer");
const { config } = require("dotenv");

//* Initializes the dotenv package that enable the use of environment variables
config();

//* sets up the transport with Gmail
let mailTransport = nodemailer.createTransport({
  service: "gmail",
  auth: {
    user: process.env.GOOGLE_EMAIL,
    pass: process.env.GOOGLE_PASSWORD,
  },
});

//* mailing details that show up in the mail like [body of email, subject of email]
let mailDetails = {
  from: process.env.GOOGLE_EMAIL,
  to: "[email protected]",
  subject: "Mailing using nodemailer",
  text: "This is a test mail sent using nodemailer",
};

//* Function that sends mail to the reciever
mailTransport.sendMail(mailDetails, function (error, data) {
  if (error) {
    console.log("Error Encountered: " + error);
  } else {
    console.log("Email sent successfully");
    console.log(data);
  }
});

In the above code, the nodemailer.createTransport() creates a transport with Gmail in our case as gmail is given in the service option of the function. The mailDetail object contains the detail to be sent in a mail, it contains the options like [from, to, subject, text].

The mailTransport which has a reference to the createTransport which has a method sendMail() that takes in the mailDetails object, and returns a callback. Now, that the code is ready let’s run and test it out.

## Running the node application
$ node index.js

Results

The result is printed in the console with a successful response.

Success Response was sent back

Let us go to the recipient email account and check if the mail is present, and from the looks of the account, the mail has arrived as highlighted below.

Email Received by the recipient

We have successfully received the mail in the recipient’s account, and we have achieved the goal of sending mail from a node application using nodemailer to send mail by using a Gmail account.

Conclusion

So, in this article we learned about sending mail in nodejs using a Gmail account, saw the functionalities of nodemailer, and saw the results of using it.



This post first appeared on ScanSkill - Level Up Your Programming Skill, please read the originial post: here

Share the post

Send Mail in Nodejs Using a Gmail Account for Free

×

Subscribe to Scanskill - Level Up Your Programming Skill

Get updates delivered right to your inbox!

Thank you for your subscription

×