How to Send Email in Node.js using Nodemailer & Gmail

0

How to Send Email in Node.js using Nodemailer & Gmail

As a part of experiments, developers need to send emails from their Node.js application for different purposes, and it looks like a complex task until they know the easy way to do it. So, if you are looking for an easy and straightforward way to send emails from your NodeJS app, this blog is written for you.

In this blog, you’ll learn how to Send Emails in Node.js using Nodemailer and Gmail. If you don’t know, Nodemailer is a module for Node.js applications widely used to send emails.

There are many modules and ways to send email in NodeJS like SendGrid, but I’ll use Nodemailer and Gmail among them because they are easy to set up to send mail.


As you know, Google removed the “Less secure apps” feature on May 30, 2022. So, now it is a little more difficult to send mail using Gmail in Node.js app than before but don’t worry at the end of this blog you’ll be able to send mail from the Node.js app using Nodemailer and Gmail.

Steps to Send Email in Node.js using Nodemailer

  1. Create a folder with any name and open it in your VS Code.
  2. Open the terminal by pressing ctrl + j and run this command npm init -y to create a package.json file for a new node.js app.
  3. Install the Nodemailer module using this command npm i nodemailer
  4. Create an app.js file and paste the given codes.
const nodemailer = require("nodemailer");

const sendEmail = async (mailDetails) => {
    const transporter = nodemailer.createTransport({
        host: "smtp.gmail.com",
        port: 587,
        auth: {
            user: "[email protected]",
            pass: "your-password",
        },
        tls: {
            rejectUnauthorized: false
        }
    });

    try {
        console.log("Sending your email...");
        await transporter.sendMail(mailDetails);
        console.log(`Email sent successfully to ${mailDetails.to}`);
    } catch (error) {
        console.log("Sorry, failed to send your email!");
    }
}

sendEmail({
    from: "sender-email-address",
    to: "receiver-email-address",
    subject: "Test Email via NodeJS using Nodemailer",
    text: "Hi, there...This is a test email sent via NodeJS App using Nodemailer."
});

If you look at line.no 7, there you need to pass your Google Account email and the Google App password as a user and pass value. Before May 30, 2022, when the “Less secure app” feature is allowed by Google, there you can pass your Google Account password to send an email. But, now you’ve to pass the App password.

To create the App Password in your Google Account, go to manage your google account > Click Security > Under “Signing in to Google” select App passwords > Select App & Device. Now you’ll get a 16-character code and this is your app password. For more details about creating App Passwords, read the Official Google Article.

Once you have done the above steps, go to line.no 26 and pass your email as from and receiver email as to value. At last, open the terminal and run this command node app.js to run your node.js app to send an email.


Conclusion

In this blog, you’ve learned how to Send an Email in Node.js applications using Nodemailer and Gmail. Remember, not only for experiments, but you can use Nodemailer in production too. If you want to know how to Send Emails in PHP from Localhost using Gmail. You can view this blog.

If you couldn’t send an email or found any problem, don’t hesitate to comment down. If you found this blog helpful, don’t forget to share it with others. Happy Coding!

Previous articleCreate A Download Button with Timer in HTML CSS & JavaScript
Next articleCreate A Draggable Image Slider in HTML CSS & JavaScript

LEAVE A REPLY

Please enter your comment!
Please enter your name here