Sending Emails with Node

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. **

After we’ve setup our transporter we can verify that our SMTP configuration was correct. If it return an error, then we will log the error to our console otherwise the server is ready to accept messages.

transporter.verify((error, success) => {
if (error) {
console.log(error)
} else {
console.log("Server is ready to take our messages")
}
})

Next we’ll build out our basic email details containing the sender and receiver’s email addresses, subject and content of the message.

const mailDetails = {
from: 'xyz@gmail.com',
to: 'abc@gmail.com',
subject: 'Testing Testing',
text: 'This message was sent from Node!'
}

Additionally, we can pass in more addresses to our to field in a comma separated list or an array. Add a cc/bcc field, send attachments, embed images and even send an html version of our message. For additional detail and even more advanced fields check out the following section of the nodemailer docs.

Sending Mail

With our message created we are ready to use the built-in sendMail() function to send our email from the sender to receiver. If the message fails or contains an error we’ll log the error otherwise we’ll log the data and a success message to our console.

transporter.sendMail(mailDetails, (err, data) => {
if (err) {
console.log(err)
} else {
console.log(data)
console.log('Email sent successfully.')
}
})

TL;DR

And with that we’re all set. In short, all you need to do to send messages, is the following:

  1. Create a Nodemailer transporter using either SMTP or some other transport mechanism
  2. Set up message options (who sends what to whom)
  3. Deliver the message object using the sendMail() method of your previously created transporter

Conclusion

Now, sending out emails to the users is a really common practice today and has become an integral part of pretty much any modern application. Be it, a welcome email that we may want to send to a new user who just signed up to our application or a notification email that we want to send to our existing users informing them about the alerts they might have subscribed for. There are countless possibilities and thankfully they’ve been simplified greatly with Nodemailer!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store