How to send email in node js using gmail and node mailer

Emails are one of the most common ways for applications to communicate with users. Whether it’s a welcome email, a password reset link, or an order confirmation, sending emails is an essential part of any modern web application.

In this post, we will learn how to send email in Node.js using Gmail and Nodemailer step by step.

In this tutorial, we will use a Gmail App Password for authentication instead of your regular Gmail password, as it is more secure and recommended by Google. If you don’t already have an App Password, you can check out our detailed guide on how to generate an App Password in Gmail before continuing with this tutorial.

What is Nodemailer?

Nodemailer is a popular Node.js module that makes it easy to send emails. It supports different email services, including Gmail, and works with SMTP (Simple Mail Transfer Protocol). Developers prefer Nodemailer because:

  • It’s simple to use.
  • Works directly with Gmail.
  • Can send plain text, HTML, and attachments.

Install Nodemailer in Node.js

First, you need a Node.js project. Inside your project, install Nodemailer using below command:

npm install nodemailer

It will take some time to install nodemailer with it’s dependencies. Once Nodemailer added to your project, you can to configure and send emails.

Create Transporter in Nodemailer

Now, let’s configure the transporter for Gmail:

const nodemailer = require("nodemailer");

const transporter = nodemailer.createTransport({
  service: "gmail",
  auth: {
    user: "your-email@gmail.com", 
    pass: "your-app-password",  
  },
});

Here, we are importing nodemailer into code. Then creating transporter instance specifically for gmail service using user name and password.

Use you app password instead of regular password. With regular password google mails can fail to send as per gmail’s new policies.

Send Email in Node.js Using Gmail and Nodemailer

Once our transporter is configured, we can send mail using it. Checkout below example for that:

const mailOptions = {
  from: "sender@codewolfy.com",
  to: "recipient@codewolfy.com",
  subject: "Welcome to Our App",
  text: "Hello! Thank you for signing up. This is a test email sent from Node.js using Gmail and Nodemailer.",
};

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    console.log("Error sending email:", error);
  } else {
    console.log("Email sent successfully:", info.response);
  }
});

As you can see, in above code we have created mail options object that contains from, to, subject and text.

The sendMail function is sending email using our credentials defined in transporter configuration with mail options. It will try to send Email using SMTP protocol and print response based on that.

Send Email with Attachments

Sometimes, you may want to send files such as invoices or reports. With Nodemailer we can easily attach files while sending emails. Let’s take another example using attachments:

const invoiceMail = {
  from: "your-email@gmail.com",
  to: "client@example.com",
  subject: "Invoice for Your Order",
  text: "Please find attached your invoice.",
  attachments: [
    {
      filename: "invoice.pdf",
      path: "./invoice.pdf",
    },
  ],
};

transporter.sendMail(invoiceMail, (err, info) => {
  if (err) {
    console.log("Error:", err);
  } else {
    console.log("Invoice email sent:", info.response);
  }
});

This way, you can send email in Node.js using Gmail and Nodemailer with text, HTML, or attachments. It accepts attachments array so we can attach or send multiple files.

For each file we have to provide file name and actual path for that file. While creating real world application, most of the time these file is created specifically for user and there can be multiple files.

For this tutorial, we keep things simple for better understanding. While sending email using gmail sometimes you can face errors like invalid login details, blocked by google, or port issues which is common but normally it will not occur.

Conclusion

In this blog, we learned how to send email in Node.js using Gmail and Nodemailer. We installed Nodemailer, configured Gmail with an app password, created a transporter, and sent different types of emails, including real-world examples. With this knowledge, you can now integrate email functionality into your Node.js applications.