tt
H.M. Tajul Islam Tanim

H.M. Tajul Islam Tanim

posted on 14 Nov 2022

How to send email with Nodemailer and Gmail

There are so many scenarios you have to face where you need to send emails programmatically. Like user authentication or a newsletter. So let's go ahead and check out how to send an email with node js.

Create a node project:

Go to the project directory and run:

npm init -y
bash

It will leave you with a package.json file. Now create a .env and index.js file.

Install dependencies:

We will use 2 dependency in our project:

  • nodemailer: Obviously to send email
  • dotenv: To import these environment variables to your project. From the project root, run:
npm install nodemailer dotenv --save
bash

It will install all the required dependencies to your project.

Set up google account:

  • Enable 2-factor authentication
  • Create an app password

To use Nodemailer you need a Gmail app password. Before creating an app password, you have to enable 2-factor authentication in your account. Now go ahead and create an app password. To create an app password, follow this URL. Click on Select app and select Gmail and from the Select device option select Mac. Then generate an app password. Copy the app password and store it for later steps.

Set up environment variables:

Open the .env file and add these variables to use it in the project:

FROM_GMAIL=example@gmail.com GMAIL_APP_PASS=xxxxxxxxxxxxxxxx
env

Import packages:

Now open the index.js file and add these lines. It will import the Nodemailer package and import the environment variables to the project file:

//index.js require("dotenv").config(); const nodemailer = require("nodemailer");
js

Create a transport:

Create a transport constant with the createTransport method of the Nodemailer module. Call the method with an object with service property to be Gmail, host to be google's SMTP address, and secure to be true if you want to use SSL. And also add a property named auth and define the user property in it to the FROM_GMAIL we just set in the environment variables and the pass property to be the GMAIL_APP_PASS environment variable.

//index.js const transport = nodemailer.createTransport({ service: "gmail", host: "smtp.gmail.com", secure: true, auth: { user: process.env.FROM_GMAIL, pass: process.env.GMAIL_APP_PASS, }, });
js

Call the sendMail method:

Now call the sendMail method of the transport we just created with all the property needed.

//index.js transport .sendMail({ from: process.env.FROM_GMAIL, to: "target@gmail.com", subject: "Hello World!", body: "Hello World!", html: "<b>Hello world!</b>", }) .then((res) => { console.log("Mail sent successfully!"); }) .catch((err) => { console.log("something went wrong!"); });
js

Replace the target@gmail.com with the Gmail you want to send the email to. Change the subject to be the subject of the email, the body to be the body of the email, and the HTML to be the HTML content if you want to send any. It is an asynchronous method, so listen for the response and error.

Run the script from the terminal:

From the project root run:

node index
bash

It will run the script. Now check the testing email if the email went through.

If you have any trouble with anything or any feedback, head for the git repository of this project here.

Thanks for reading.
Happy coding. 💖