How to send emails in Nodejs

ai google send emails nodejs
R
Rahul Yadav

Senior Email Marketing Strategist

 
June 30, 2025 2 min read

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!

R
Rahul Yadav

Senior Email Marketing Strategist

 

Technical Manager and Passionate Fullstack and polyglot developer.

Related Articles

seo

6 Authentic Ways to Trace an IP Address from Email

Learn effective methods to trace IP addresses from emails for security verification, spam identification, and enhanced email management with practical step-by-step guidance.

By Abhijeet Chattarjee June 30, 2025 6 min read
Read full article
marketing

Demystifying Email Delivery: A Deep Dive into Email APIs, SMTP Servers, and Email Delivery Services

This blog post dives into the world of email APIs, SMTP servers, and email delivery services. Learn how they work together to get your emails into inboxes, not spam folders. Boost your email marketing results with this essential guide!

By Abhijeet Chattarjee June 30, 2025 7 min read
Read full article
content

21 SMTP Response Codes

The SMTP response code list can be used to help quickly determine the reason for email bounces or why you received an SMTP error when sending an email.

By Rahul Yadav June 30, 2025 6 min read
Read full article
digital

6 Tips for Defending Against Business Email Cybercriminal Attacks

Learn how to defend your firm from business email cybercriminal attacks, including how to spot and block them. Discover important tips with Mailazy now!

By Abhijeet Chattarjee June 30, 2025 6 min read
Read full article