Send Verification Mail on User Registration Node Js without SSL certificate Solution - self signed certificate in certificate chain nodejs
First do npm install nodemailer & then import it in backend code
========== Backend Code ===
create EmailAPI.js file in your router folder & then import it to your IndexRouter.js file
// EmailAPI.js
import nodemailer from 'nodemailer';
var sendMail=(email,password,token)=>{
process.env.NODE_TLS_REJECT_UNAUTHORIZED='0'
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'vasubirla@gmail.com',
pass: 'mtvrqzmxlarnrg9h' // Create your own temporary password from Google setting
}
});
var mailOptions = {
from: 'vasubirla@gmail.com',
to: email,
subject: 'Verification Mail MyApp',
html: "<h1>Welcome To MyApp</h1><p>you have successfully register to our app , your login credentials are attached below</p><h3>Email : "+email+"</h3><h3>Password : "+password+"<h3>OTP for account verification is </h3>" + "<h1 style='font-weight:bold;'>" + otp +"</h1>"+"</h3><br><h2>Click on the link below to verify your account</h2>http://localhost:3000/verifyUser/"+email+'/'+token
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
export default sendMail;
============================================================end EmailAPI,js
// IndexRouter.js Start
router.post("/register",(req,res)=>{
IndexController.registerUser(req.body).then((result)=>{
var token = result.token;
sendEmail(req.body.email,req.body.password,token);
res.render("register",{"output":"User register successfully..."});
}).catch((err)=>{
res.render("register",{"output":err});
//console.log(err);
});
});
=========================================end IndexRouter.js
IndexController.registerUser(req.body).then((result)=>{
res.render("register",{"output":"User register successfully..."});
}).catch((err)=>{
res.render("register",{"output":err});
});
//res.json(req.body);
});
Comments
Post a Comment