Sending Emails with Node

Austin Buhler
3 min readMar 17, 2021

In this tutorial we’ll learn how to setup and send emails in Node with the Nodemailer package and Gmail SMTP. Whether you want to communicate with your users or just notify yourself when something has gone wrong, one of the best options for doing so is through email.

Thankfully Nodemailer makes the process extremely easy and we can setup our service and succesfully send emails in just a few lines of code.

Setup

We’ll start by first initializing our project and installing our dependencies — nodemailer and dotenv (you’ll want to move your email/password into environment variables)

npm init -y
npm install nodemailer dotenv

After creating our index.js file we’ll bring in the packages:

const nodemailer = require('nodemailer')
require('dotenv').config()

Configuration

Next we configure our transporter object that holds our service and auth

const transporter = nodemailer.createTransport({
service: 'gmail',
auth:{
user: process.env.EMAIL,
pass : process.env.PASSWORD
}
})

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

--

--