DEV Community 👩‍💻👨‍💻


Posted on

Complete code Send mail on node JS for 60s

Install Nodemailer

If you want to learn more about Nodemailer you can read here:

Turn on the Terminal and type the following command to install

npm i nodemailer

Handling Send Mail

The outermost of the project creates the file as follows the following folder

handle sendMail

Code handle send mail (sendMail.js)

const nodemailer = require("nodemailer");
const transport = nodemailer.createTransport({
  service: "Gmail",
  auth: {
    user: Email name you use to send,
    pass: Email password you use to send,
module.exports.sendConfirmationEmail = (name, email) => {
      from: user,
      to: email,
      subject: "Confirm your registered account",
      html: `<h1>Email Confirmation</h1>
        <h2>Hello ${name}</h2>
        <p>You have successfully registered an account. Please confirm your email by clicking on the following link</p>
    .catch((err) => console.log(err));

Handling Router

Code handle router (router.js)
Add the following code to the router .js

const sendMail = require("../utils/sendMail");"/sendmail", async (req, res, next) => {
  try {
    return res.status(200).send("Oke");
  } catch (err) {
    console.log("🚀 ~ file: Routers.js:11 ~ ~ err", err);

Run test results

Here I use insomnia to test API


Check mail

Check mail


If you do not understand something, you can message me or join this group Zalo: Link, to exchange knowledge about BackEnd Nodejs

Top comments (0)

Tired of sifting through your feed?

Find the content you want to see.

Change your feed algorithm by adjusting your experience level and give weights to the tags you follow.