Sending Email via Gmail SMTP Server in Node.JS

Sending emails through the SMTP server is a convenient option. It may happen your emails are landing in the spam or emails not working on your server. In both cases, you can use the SMTP server. When you send an email through the SMTP server, there is a high possibility of your email will go straight into the user’s inbox.

One more benefit of using the SMTP server is you can send emails from the local server. It helps you to test and fix email content on the local server itself.

In this article, I show you how to send an email via the Gmail SMTP server in Node.js. For sake of the tutorial, I choose the Gmail SMTP server but following the same technique shown in the article you can pick up any other SMTP service provider.

There are several Node.js libraries available that make development easy and reliable. One of them is Nodemailer which I am going to use for this tutorial. This package allows sending email easily.

Before proceeding, make sure you have Node.js installed on your system.

In order to use the Gmail SMTP server, you need to change Gmail settings. Login to your Google account and click on Account. Once you are on the Account page click on Security. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set it to ON.

less-secure-apps

Send Email via Gmail SMTP Server in Node.JS

Let’s install the Nodemailer library. Open the terminal in the root directory of your project and run the command:

npm install --save nodemailer

This command installs the nodemailer library which you can see under your node_modules directory. Next, open your JS file in the editor for writing the code. Let’s say your file is app.js, add the below code in it.

"use strict";
const nodemailer = require("nodemailer");
 
async function main() {
 
    let transporter = nodemailer.createTransport({
        host: 'smtp.googlemail.com', // Gmail Host
        port: 465, // Port
        secure: true, // this is true as port is 465
        auth: {
            user: 'GMAIL_USERNAME', // username
            pass: 'GMAIL_PASSWORD', // password
        },
    });
 
    // send mail with defined transport object
    let info = await transporter.sendMail({
        from: '"FROM_NAME" <FROM_EMAIL_ADDRESS>', // sender address
        to: "RECEPIENT_EMAIL_ADDRESS", // list of receivers
        subject: "Welcome Email", // Subject line
        //text: "Hello world?", // plain text body
        html: "This email is sent through <b>GMAIL SMTP SERVER</b>", // html body
    });
 
    console.log("Message sent: %s", info.messageId);
}
 
main().catch(console.error);

Make sure to replace placeholders with the actual values. Here I am using the HTML content. If the user wants to send plain text then pass the content to the text key. Run the above code in the terminal using the command:

node app.js

Upon sending the email successfully, you should see an output something like the below screenshot.

Output

That’s it! I hope you understand how to send an email through the Gmail SMTP server in Node.js. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

4 thoughts on “Sending Email via Gmail SMTP Server in Node.JS

  1. Really a piece of good information you have shared which is very helpful for beginners of the technology management people. Your articles are always very helpful and in a language that even non-technical people can also understand what is the information all about. Thank you for sharing a rich article.

    1. i tried smtp.gmail.com and it did not work for me.

      also this worked for me:

      port: 587, secure: false,

      this website suggests to only use port 587 and not 465:
      https://pepipost.com/blog/25-465-587-2525-choose-the-right-smtp-port/

      maybe “less secure access app” can be left off to disable less secure apps?

      and per documentation: “If false (the default) then TLS is used if server supports the STARTTLS extension.” – so i suppose its ok to use “secure:false” but it feels counterintuitive.

Leave a Reply

Your email address will not be published.