How to Send Email in Node.js using Nodemailer & Gmail
Sending emails from a Node.js application might seem like a daunting task at first, but with the right tools, it can be quite straightforward. One popular tool for this purpose is Nodemailer. In this guide, we’ll walk you through the process of sending emails from your Node.js app using Nodemailer and Gmail.
Why Nodemailer?
Nodemailer is a well-known Node.js module designed specifically for sending emails. Its ease of setup and use makes it a popular choice among developers. While there are other email-sending services like SendGrid, Nodemailer coupled with Gmail is a simple and effective solution for many use cases.
Important Update: Gmail Security Changes
As of May 30, 2022, Google removed the "Less secure apps" option, which previously allowed straightforward access to Gmail accounts from third-party apps. Now, you need to use an App Password instead of your regular Gmail password for increased security.
Don’t worry; setting up Nodemailer with Gmail is still manageable. By the end of this tutorial, you’ll be able to send emails from your Node.js application seamlessly.
Step by Step Guide to Sending Emails
1. Set Up Your Project
First, you’ll need to create a new directory for your project and set it up:
Create a Project Folder: Name it whatever you like.
Open Terminal: Navigate to your project folder.
Initialize a New Node.js Project: Run the command:
npm init -y
This command creates a package.json file with default settings.
2. Install Nodemailer
Next, you’ll need to install the Nodemailer module. Run the following command in your terminal:
npm install nodemailer
3. Create Your Script
Create a file named app.js in your project folder and add the following code:
const nodemailer = require("nodemailer");
const sendEmail = async (mailDetails) => {
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
auth: {
user: "[email protected]",
pass: "your-app-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: "[email protected]",
to: "[email protected]",
subject: "Test Email via Node.js using Nodemailer",
text: "Hi there... This is a test email sent via Node.js App using Nodemailer."
});
4. Configure Gmail Access
To send emails using Gmail, you need to use an App Password instead of your Gmail account password. Here’s how to generate one:
Go to Your Google Account Settings: Navigate to Google Account.
Access Security Settings: Click on "Security" from the menu.
Generate an App Password: Under "Signing in to Google," select "App passwords." Choose the app and device for which you need the password. Google will generate a 16-character password for you.
Replace "your-app-password" in the app.js code with this 16-character code.
5. Send Your Email
With everything set up, run your Node.js script by executing:
node app.js
This command will trigger the sendEmail function and send the email as specified.
Conclusion
In this guide, we covered how to send emails in a Node.js application using Nodemailer and Gmail.
Despite recent changes in Gmail’s security protocols, using Nodemailer remains a reliable and straightforward method for sending emails.
Whether you’re using this setup for experimentation or in a production environment, Nodemailer provides an effective solution.
If you encounter any issues or have questions, feel free to mail or contact us. If you found this guide helpful, consider sharing it with others. Happy coding!