Nodemailer is a popular Node.js module that allows you to send emails from your Node.js application. In this tutorial, we will learn how to send an email using Nodemailer in Node.js.
Step 1: Install Nodemailer
First, you need to install Nodemailer in your Node.js application. You can do this by running the following command in your terminal:
npm install nodemailer
Step 2: Set up your email configuration
Next, you need to set up your email configuration. You will need to provide your email service provider’s SMTP (Simple Mail Transfer Protocol) server details, username, and password. In this tutorial, we will use Gmail as our email service provider.
const nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your_email@gmail.com',
pass: 'your_email_password'
}
});
Step 3: Create and send the email
Now, you can create an email message and send it using the sendMail
method of the transporter object.
// setup email data with unicode symbols
let mailOptions = {
from: 'your_email@gmail.com', // sender address
to: 'recipient_email@example.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world?', // plain text body
html: '<b>Hello world?</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});
Step 4: Test your email sending functionality
You can now run your Node.js application and test if the email is being sent successfully using Nodemailer.
node app.js
If everything is set up correctly, you should see a message in the console indicating that the email has been sent successfully.
In this tutorial, we learned how to send an email using Nodemailer in Node.js. Nodemailer is a powerful module that allows you to send emails from your Node.js application easily. You can customize your email message further by setting various options such as subject, text, and html body.
I hope this tutorial was helpful in guiding you through the process of sending an email using Nodemailer in Node.js. If you have any questions or run into any issues, feel free to leave a comment below. Thank you for reading!
response.end() where do get this ?
Those who can't able to find "App Password" option after the search, they need to first turn on 2-step Verfication than they will be able to see that option.
brother apki video help ful toh hai lakin apka mail receive time and jab send kara ho vo toh different hai ??? asa kyu ??
Sir, Is there any limitations of this method? Can I use it free & unlimited times forever?
Bahut hi zyada achha se explain kiye hain bhaiya … Thank u so much
You always keep post this type of content for us
app password not coming on search
Thanks bro for creating this amazing content for us
this only works with localhost.
in production it doesnt works,
is there any way to send emails from gmail in production too?
but I'm receiving mail in spam, what should I do about that?
port dena jruri h kya mera bina port k bhi chalra hai lekin yr production me mail send nahi hora
Damn…..this is awesome!!!
Thanks man It worked
Thanks bro it was helpful alot, it saved my time
Okay , i get the problem , If u want app password , first u need to activate 2 factor authentication , not just check but activate and then ur id will be elegible for app generation
Bro mere pass 8 gmail id hai , kisis me bhi app password nahi a rah hai , bussiness or personal dono , what should i do aur ye 2 factor authenctication dekh ke karna kya hai uska ??,
how to get code??
i dont see app password when I type in search, can u help me
thanks for such helpful video
thanks for this video