Send free email using Nodemailer in javascript/nodejs

Jagannath Swarnkar
3 min readFeb 29, 2024

--

Sending Free Emails in JavaScript/Node.js” — Learn the ins and outs of Nodemailer to effortlessly send emails for free using gmail within your JavaScript and Node.js applications

Send free email using Nodemailer in javascript/nodejs Sending Free Emails in JavaScript/Node.js” — Learn the ins and outs of Nodemailer to effortlessly send emails for free using gmail within your JavaScript and Node.js applications

In this blog, we will be creating a script using Nodemailer for sending email in javascript, for this we need one persionalised email account (gmail/outlook/other). Even we will be going with gmail here because gmail is common for everyone.

Configuring Gmail:

Step 1: Create a new gmail account or you can use your existing one.

Step 2: You need user: your gmail id & pass: you need to create an App Password .

  • Go to your gmail and click on your gmail profile icon on top-right-corner, a popup will open, now click on Manage your google account .
  • Go to Security settings / Security , scroll down, you might have see App Passwords if not then click on 2-Step Verifications , enable & complete 2-Step verification, then you will be seeing the App Passwords section at the bottom.
  • Go to App Passwords and create one, you will get the Generated app password popup copy this password.
Image for generating google App Password

Note: Do not add other securities and restrictions.

Configuring Nodemailer:

Here we will create a nodejs application and implement this script on a get api to send the mail.

Create Nodejs Application

Initialize a nodejs application npm init , install express npm install express . Now create a javascript file index.js and add this initial express code to run node server.

import express from "express";
const app = express();
app.use(express.json({ limit: "50mb" }));

// default api route /
app.get("/", (_req, _res) => {
_res.send("Welcome to the Express API");
})

app.listen(8000, () => {
console.log(`Application is running on port: 8000`);
});

// Now you can hit your api url
// http://localhost:8000/

Create Nodemailer configuration

Install Nodemailer — npm install nodemailer and create a new file named nodemailer.js

import * as nodemailer from "nodemailer";

const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "your verified sender email id here",
pass: "app password which you generated in first step"
},
tls: {
// Do not fail on invalid certs
rejectUnauthorized: false
}
});
export const sendEmail = async ({ to, subject, html, text }) => {
try {
const info = await transporter.sendMail({
from: "your verified sender email id here", // Replace with your desired sender email
to,
subject,
text,
html
});
console.log("Email sent successfully!", info.response);
} catch (error) {
console.error("Error sending email:", error);
}
};

Now create a new api route /email for sending email.

import express from "express";
const app = express();
app.use(express.json({ limit: "50mb" }));
import { sendEmail } from './nodemailer.js';

// default api route /
app.get("/", (_req, _res) => {
_res.send("Welcome to the Express API");
})

// Get api to send default email
app.get("/email", (_req, _res) => {

const emailPayload = {
to: "recevier_email", // replace it with email you want to send to
subject: "Nodemailer Email Testing",
// text: "Send free email using Nodemailer in javascript/nodejs | by Jagannath Swarnkar | Feb, 2024 | Medium"
html: `
<div>
<h1>Send free email using Nodemailer in javascript/nodejs | by Jagannath Swarnkar | Feb, 2024 | Medium </h1>
<a href="https://jagannath18.medium.com/send-free-email-using-nodemailer-in-javascript-nodejs-39e29e8922f6">View Documentation</a>
</div>
`
}

try {
const res = await sendEmail(emailPayload)
return _res.status(200).send({
message: "Email sent successfully!",
status: 200
});

} catch (error) {
return _res.status(500).json({
message: "Internal Server Error",
error: error,
status: 500
});
}
})

app.listen(8000, () => {
console.log(`Application is running on port: 8000`);
});

// to send email, http://localhost:8000/email
// hit the api url in browser it will send the email ( as we created a get api )

All done! Check your receiver email you’ll get the email by sender gmail id.

Thank you

Happy Codeing :)

Demo view for “Sending Free Emails in JavaScript/Node.js” — Learn the ins and outs of Nodemailer to effortlessly send emails for free using gmail within your JavaScript and Node.js applications In this blog, we…”

--

--

Jagannath Swarnkar
Jagannath Swarnkar

Written by Jagannath Swarnkar

Sr. Software Engineer | React js | Next js | Node Js | Flutter

No responses yet