How to send emails in Nodejs
A guide explains how to compose and send emails in NodeJs
Wednesday, Jul 28, 2021
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!