How to send emails in Nodejs

A guide explains how to compose and send emails in NodeJs

Kunal Saini
Kunal Saini
July 28, 2021
2 min read

Send Transactional emails With Confidence

Our dedicated support team works around the clock because transactional emails never stop.

Free Sign UpSet up in 2 minutes . No Credit Card Required

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: "[email protected]",
    pass: "yourpassword",
  },
})

var mailOptions = {
  from: "[email protected]",
  to: "[email protected]",
  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: "[email protected]",
  to: "[email protected],[email protected]",
  subject: "Sending Email using Node.js",
  text: "Hello World!",
}

Send HTML

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

var mailOptions = {
  from: "[email protected]",
  to: "[email protected]",
  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 ReadAPIsBest PracticesEmail SecurityEmailEmail TemplatesGolangMarketing Email



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. Mailazy has built to serve their customers a high value transactional email service. Sending transactional emails shouldn’t be hard with the right transaction email service. With an email service provider or simple transactional email service like Mailazy, you can easily send and track your transactional emails. Our goal is to help you achieve high delivery rates and keep your transactional emails out of the spam folder. We are always there for whenever ypu need us.

Lets get started!

Kunal Saini

Kunal Saini

Kunal 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