How to send emails in Nodejs

A guide explains how to compose and send emails in NodeJs

Pranav Sharma
Pranav Sharma
July 28, 2021
2 min read

The Email API by developers, for developers

Integrate in minutes with our Email API or SMTP and deliver emails to customer's inbox instantly. Mailazy Email API built for developers that fits into any tech stack.

Introduction

Node.js is an open source server environment and allows you to run JavaScript on the server. The Nodemailer module makes it easy to send emails from your computer or from your server.

Prerequisites

  • Nodejs installed on your computer

Installation

Run the following npm command in your project directory to install nodemailer in your project.

npm i --save nodemailer

Import the module in your application or project

var nodemailer = require("nodemailer")

Send an Email

Use the username and password from your selected email service provider to send an email. Following will show you how to use your Gmail account to send an email:

var nodemailer = require("nodemailer")

var transporter = nodemailer.createTransport({
  service: "gmail",
  auth: {
    user: "youremail@gmail.com",
    pass: "yourpassword",
  },
})

var mailOptions = {
  from: "youremail@gmail.com",
  to: "recipient@example.com",
  subject: "Sending Email using Node.js",
  text: "Hello World!",
}

transporter.sendMail(mailOptions, function (error, info) {
  if (error) {
    console.log(error)
  } else {
    console.log("Sent: " + info.response)
  }
})

Sent to Multiple Recipients

In order to send an email to more than one receiver, add them to the "to" property of the mailOptions object in the above example, separated by commas.

var mailOptions = {
  from: "youremail@gmail.com",
  to: "recipient1@example.com,recipient2@example.com",
  subject: "Sending Email using Node.js",
  text: "Hello World!",
}

Send HTML

use the "html" property instead of the "text" property.

var mailOptions = {
  from: "youremail@gmail.com",
  to: "myfriend@yahoo.com",
  subject: "Sending Email using Node.js",
  html: "<h1>Welcome</h1><p>That was easy!</p>",
}

Common Issues and Resolutions

  • Error: Invalid login: 535-5.7.8 Username and Password not accepted.
    A potential solution to this error could be to enable the Gmail service to use it in third-party apps. To resolve this error just login in Gmail account and enable less secure apps using this link https://myaccount.google.com/lesssecureapps
  • ETIMEDOUT errors
    Check your firewall settings. Timeout usually occurs when you try to open a connection to a port that is firewalled either on the server or on your machine.
  • TLS errors
    Check your antivirus settings. Antiviruses often mess around with email port usage. Node.js might not recognize the MITM cert your antivirus is using.
    Latest Node versions allow only TLS versions 1.2 and higher, some servers might still use TLS 1.1 or lower. Check Node.js docs how to get correct TLS support for your app.

Conclusion

Gmail either works well or it does not work at all. It is probably easier to switch to an alternative email service provider such as Mailazy instead of fixing issues with Gmail. Using gmail for your production server isn’t recommended either, you should opt for an email provider to manage your emails.
Configuring and setting up Mailazy is easy and seamless and emails can be sent in a few minutes!



Mailazy Docs

Integrate with Transactional email service in minutes

click here

Most Popular Tags

EngineeringSMTPShort ReadBest PracticesEmailAPIsEmail SecurityCommunicationEmail APIEmail Delivery



What is Mailazy?

Mailazy is a Transactional Email Platform specially built for developers which satisfies the requirement for use cases like Reset Password Emails, OTP Emails, Welcome Emails, and so on. The Mailazy platform helps you to send transactional emails seamlessly and track email deliverability. Mailazy enables your applications to send messages via a simple HTTP REST interface or via easy SMTP integration and abstracts away the complexities of sending transactional emails.

Visit website

Pranav Sharma

Pranav Sharma

Pranav Sharma is a Lead Software Development Engineer at Mailazy. He graduated in Computer Science and considers himself a learner of life. He loves to play cricket, football and computer games.

View Profile