Sending Gmail Via Nodemailer & Gmail || Nodemailer + Node.js + Express.js

Jamilur Rahman
React A-Z
Published in
3 min readSep 13, 2019

There are two ways of sending emails with Nodemailer and Gmail.

  1. Simple way || Just add your Gmail user name and password to your config file and use smtp.gmail.com as host.
  2. Proper way || Using Gmail auth2 access token and google developer console API key.

We will be following the first method in this article and move on to the second one, as the second one is quite similar. You will just need to add auth2 information to your transport options.

Lest’s Get Started

Required packages and dependencies:

  1. nodemailer.
  2. express.
  3. Node.js
  4. Nodemon.

Let’s get started by setting up Node.js and all other dependencies.

Fire up your favourite terminal and cd into your work directory. Now just run the following command.

then follow the prompts to setup your node app. After that type in the following commands to setup dependencies for node app.

Your all set to go. Setup your project structure as follows.

Setting up config.js

config.js file will store our username, password and any other configuration information that we will require. Let’s create a config folder and config.js file within the config folder.

inside config.js file write:

Setting Up server.js || Nodemailer and Route

First, create a server.js file in your root directory. Write the following code at the top of server.js to import project dependencies.

After that’s done let's configure Nodemailer so that it can send mail. In your server.js file create a variable called transport. The transport variable holds Nodemailer configuration options. We will be using smtp.gmal.com as our host and set port address as 465.

Create the transport variable as follows.

The transporter variable initiates transporter for Nodemailer which takes transport as transport options.

Now let's configure transporter and set up a route to send mail. In order to do that we first need to verify if Nodemailer is able to connect to Gmail server and it is working properly. Write the following code in server.js to verify if Nodemailer is working properly.

Now let’s create a route to send emails via the transporter. Write the following code to send mail when you go to http://localhost:3002/send

When you will go to http://localhost:3002/send you will receive an email from your address.

You should add sender’s email address to your body as you will receive email from yourself. The function will try to send emails. If there is no error it will show sent data otherwise it will show the error.

Run Your Node.js App

To run your node.js app cd into your root directory and run the following command from your terminal.

Things to know

You may face some errors with this approach when published to the production server as google does not allow bots to send emails. You may need to configure your Gmail account security to allow bots. If you use the second approach, these errors go away. We will discuss the second method in another article.

Allow Non-Secure App To send Mail:

Before sending your email using gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.

--

--

Jamilur Rahman
React A-Z

Hi, I am Jamilur Rahman, web / android developer. I have been working with web and android frameworks for more than a year.