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 lets send mail in NodeJs using a Gmail account.
Prerequisites
- 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.
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.
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.
3. Now that you are on the App passwords page, choose Mail in the first option as highlighted below, and select Other (Custom name).
4. Give a name for the password because it makes it easier to reference in the future. Then click on Generate to receive the 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. Basically, this password is used to interact with applications that don’t support 2-factor authentication.
Now, that the password is generated. Finally, 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 instead of explicitly calling the value.
# .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: "reciever@email.com",
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
Let’s look at the console to view the result we obtained.
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.
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. If you want to validate if the incoming email is from a spammer you can learn how to do that from How to Check for Disposable Emails in Node.js.