How to send emails using NodeMailer, gmail and OAuth2

Alex
10 min readApr 15, 2020

Hi there, I thought that since sending emails was one of the first features of my app I am building I thought it would be cool to do a tutorial on it!

The email will be sent from an API end point and will use OAuth2 credentials to send them, this is much better from a security point of view as don’t have to store actual email credentials but instead store and use access tokens and these can only be used for certain purposes for a short amount of time.

Since we will be sending the email from an API endpoint we will first need to create a NodeJS project with the command below in the terminal

npm init myamazingproject

You can call your project whatever you like, now after you have ran this statement you will need to open the project in your favourite code editor I always use VS Code. Now create a file in the root of the project named “server” and make sure it is a JavaScript file, we will now need to install multiple packages including express, body-parser, nodemailer and google apis. You can do this using the command below

npm install express body-parser nodemailer googleapis

This will install all 4 packages and create a package-lock.json file, this file is created whenever the node_modules folder is modified by npm.

--

--

Responses (13)