in this article, i will show you send email from node js using nodemailer
step: 1 create folder & run command npm init -y
step: 2 than install nodemailer :
npm install nodemailer
step: 3 create app.js file
step: 4 write below line of code
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your mail id',
pass: 'your password'
}
});
var mailOptions = {
from: 'from mail id',
to: 'to mail id',
subject: 'testing email from node js',
html: '<h1>welcome</h1><p>that was easy!</p>'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('email sent: ' + info.response);
}
});
note: if you have enabled 2-factor authentication on your google account you can't use your regular password to access gmail programmatically. you need to generate an app-specific password and use that in place of your actual password.
step: 1 go to this url : https://myaccount.google.com/security
step: 2 go to my account > security > app passwords
step: 3 generated password and paste it into your app.js
step: 1 create folder & run command npm init -y
step: 2 than install nodemailer :
npm install nodemailer
step: 3 create app.js file
step: 4 write below line of code
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your mail id',
pass: 'your password'
}
});
var mailOptions = {
from: 'from mail id',
to: 'to mail id',
subject: 'testing email from node js',
html: '<h1>welcome</h1><p>that was easy!</p>'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('email sent: ' + info.response);
}
});
note: if you have enabled 2-factor authentication on your google account you can't use your regular password to access gmail programmatically. you need to generate an app-specific password and use that in place of your actual password.
step: 1 go to this url : https://myaccount.google.com/security
step: 2 go to my account > security > app passwords
step: 3 generated password and paste it into your app.js
0 Comments
if you have any doubts , please let me know