Email being sent with Nodemailer
Email being sent with Node mailer

Send Emails from your NodeJs App with Node mailer

Shelcia David

--

We will look into using “Nodemailer” to send emails

Prerequisites

  • NodeJS
  • REST API
  • Code Editor (I will be using VS code)
  • Postman installed or Postman web account with CORS extension for chrome enabled.

Step 1: Introduction

This part consists of basic theory related to “Nodemailer”, Express and libraries used. You can skip if you are just here for the code part.

We will be doing this two way, first using the Ethereal account for testing after which we can shift over to the GMAIL account. We will run through the chaotic setting up before approaching code.

What is NodeJs?

Node.js is an open-source, cross-platform, back-end, JavaScript runtime environment that executes JavaScript code outside a web browser.

Node.js runs single-threaded, non-blocking, asynchronously programming, which is very memory efficient.

What can it do:

  • Node.js can generate dynamic page content
  • Node.js can create, open, read, write, delete, and close files on the server
  • Node.js can collect form data
  • Node.js can add, delete, modify data in your database

Source and Reference:

Let’s See if you have Node.Js

For that open your terminal and run this

node -v

This is to check if you got NodeJs installed already and its version.

shelciaabi@Shelcias-MacBook-Air nodemailer-medium % node -v
v14.15.0

So I have NodeJs version 14.15.0

If you don’t have Node.js download it here ,

Dependencies and Libraries Used

  • express

We will be using Express as it provides a robust set of features for our application.

  • dotenv

Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env.

For example if we have SECRET variable stored in our environment file(.env).

We can use it in our node file as process.env.SECRET

  • cors

CORS is a middleware that can be used to enable CORS with various options.

  • nodemon

nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.

  • nodemailer

Nodemailer is a module for Node.js applications to allow easy as cake email sending.

Step 2: Setting Up

You can skip this section to know how to initialise a node app and set .gitignore file.

1. Installation

  • open VS Code
  • create a folder
  • create index.js within your folder
  • then run these commands
npm init

Now press enter till the end(license part) and type “yes” when you prompted with the message IS THIS OK? and..

npm i cors dotenv nodemailer express --save
The folder structure will look like this.

Create .gitignore file within the folder and add these two.

node_modules.env

2. Creating Ethereal Account

We will be using a dummy account to test in development, after which we can migrate to Gmail. Using Ethereal mail we can receive mail but we won’t be sending mils but it would be prompted that the request has been made.

Create Ethereal account here.

You will have a dummy account credentials just like this.

Save these credentials we will have to use it later.

3. Creating App password for your GMAIL Account

We will have to create App password for your own Gmail Account(For security Reasons) to be used alongside “Nodemailer”.

App Passwords can only be used with accounts that have 2-Step Verification turned on.

Otherwise, tune your account settings in such a way that you make your account less secure and use your mail and password.

Resources:

Sign in with your google account credentials

Go to “Settings” from Side Menu Bar

Under “Signing in to Google,” select App Passwords.

Note: You will see this only if you have 2 step verification enabled otherwise you will have to enable less security for your account(not recommended).

At the bottom, choose Select app and choose the app you use.
Select the device and choose the device you’re using and generate the App Password.
The App Password is the 16-character code in the yellow bar on your device, then click done!.

Step 3: Coding

Add the .env file where we will add credentials so they won’t be exposed.

Create .env and add email and password just like this.

EMAIL = {your email id}PASSWORD = {your password}
Folder structure

Let’s add Ethereal account credentials for checking, now let’s move onto the code.

Code for sending mail.

You can also grab the code from my Github here.

Let’s test this.

Now back on your terminal run the command below.

nodemon index.js

Open Postman and test the endpoint just like this. I have added the API endpoint and sent the request.

Also, verify this by opening the Ethereal mailbox. (as I have previously mentioned you won’t be receiving mail but you can see that the request has been made to send it to the mail which has been given).

I got the mail !!

Now Let’s use Gmail Credentials. We need to replace the credentials in the .env file. We will have to change the service to Gmail just like this.

Code for sending email through gmail

I have received my mail (check below).

If you get any errors make sure your credentials are right and check the settings.

I got my mail !!

You can do a lot more with this. You can make this automated for verifying email or for forget password uses. You can have a contact form and for each response recorded you can get send the mail content (get content additionally from request body).

You can also use third party HTML templates and mail them from here.

Below is an example of a free template I got it from

I have added the HTML content in a separate file and then imported in index.js. (check in my Github)

Got my mail with template

Conclusion

Bye !

--

--

Shelcia David

MS in Information @ UW Madison || Ex Senior Software Developer @NASDAQ. I write articles about Web apps, UX and whatever I find interesting. www.shelcia-dev.me