How to Send Email Using Nodemailer and Gmail in Node.js
Hi to all, in this article I am going to tell you all about how to send email using nodemailer and Gmail by using Node.js easily by using simple code.
So, read this article carefully and completely to get clear understanding about sending email using Nodemailer and Gmail by using node.js without any error or issue.
Nodemailer is a module of nod.js application and it is widely used to send the emails.
There are so many other modules and ways to send emails in Node.js like SendGrid, but here I will use Nodemailer and Gmail among them because they are easy to set up to send email.
I hope that you all know that Google has removed the less secure apps feature on May 30, 2022 an now it is a little more difficult task to send email using Gmail in Node.js application than before but do not worry at the end of this blog you will be able to send email from your Node.js application using Nodemailer and Gmail.
Steps to Send Email Using Nodemailer and Gmail by Node.js
Open Code Editor
First of all you need to open any code editor and I am using VS code to edit the code.
Now you need to create a new folder with any name in the code editor.
Now you need to open the terminal by pressing CTRL+J and run this command init -y to create a package.json file for a new node.js application.
Now install the Nodemailer module using this command npm i nodemailer.
Now you need to create an app.js file and paste the following code inside it.
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 7, there you need to pass your Google Account email and 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 from now you have to pass the google App password instead of account password.
Create Google App Password
- Login to your google account.
- Go to manage your google account.
- Click on Security.
- Under Signing in to Google select App passwords.
- Select App & Device.
Now you will get a 16 character code and this is your google app password.
For more details read Google Article.
Once you have done all the above steps completely, now you need to go line 26 and pass your email as from and receiver email as to value.
At last you need to open the terminal and run this command node app.js to run your node.js app to send an email.
Conclusion
I hope that you all have understood about sending email using Nodemailer and Gmail by using Node.js easily without any error or issue.
If you got encountered with an error or you have any problem with the article then you can contact us.