WittCode💻

Node How To Send An Email From Gmail With Nodemailer

By

Learn how to send an email from a Gmail account using the Node library nodemailer. We will also go over what an app password is and why Google requires one.

Table of Contents 📖

Gmail Takes Security Seriously

Sending an email with a Gmail account using nodemailer isn't as easy as other email providers as Google doesn't allow you to use a regular password for 3rd party applications. Instead, we have to generate an app password for our Gmail account. An app password is a 16 digit passcode that gives less secure apps permission to access your Google account.

Generating an App Password

Before we start coding, lets generate an app password. Note that an app password can only be generated for accounts that have two factor authentication enabled. So if you are using an account without two factor authentication, you will not be able to generate an app password. If you do have two factor authentication enabled, follow these steps.

  • Navigate to your Google account.
  • Click on "Security".
  • Click on "2-Step Verification".
  • Scroll to the bottom and click on "App passwords".
  • Create a an app name and click on "Create".
  • After clicking "Create" an app password should be displayed on the screen. Copy the code and save it somewhere safe as if it is lost you will have to generate a new one.

Project Setup and Library Installation

Now lets create a small project to use nodemailer. First, lets initialize an empty directory as an ES6 npm project.

npm init es6 -y

Now lets install nodemailer to send an email.

npm i nodemailer

Configuring Nodemailer

Now lets import and configure nodemailer.

import nodemailer from 'nodemailer';

const transporter = nodemailer.createTransport({
  host: 'smtp.gmail.com',
  port: 465,
  secure: true,
  auth: {
    user: 'your_email@gmail.com',
    pass: 'generated_app_password'
  },
});
  • Create a transporter with the createTransport function. A transporter is what nodemailer uses to send emails.
  • The host is the hostname or IP address to connect to for sending the email. Because we are sending an email with Gmail, it will be the smtp.gmail.com.
  • Port is the port number to connect to. We are using port 465 as it is commonly used for outgoing mail.
  • The secure property is true because we are using TLS.
  • The auth object contains the username and password for the email account to send the email. The password is the app password that we generated previously.

Sending an Email with Nodemailer

Now lets send an email with Nodemailer. This can be done with the sendMail method of our transporter object.

transporter.sendMail({
  to: 'who_to_send_to@your_domain.com',
  subject: 'My Subject',
  html: '<h1>Hi how are you</h1>'
}).then(() => {
  console.log('Email sent');
}).catch(err => {
  console.error(err);
});
  • The to property is who to send the email to.
  • The subject property is the subject of the email.
  • The html property is the HTML body of the email.

Finally, just run the application and check if the email was sent.

node ./src/index.js
Email sent