I have been reading mulitple forums and tried for many hours to get my Nodemailer app to work and it simply isn't working. I have taken the suggestions from people who were able to get theirs to work but the result is still the same for me. I have posted the error below that I am getting whenever I try to send a POST request on the form I have created.
UPDATE:!!
I have managed to get the code to link up to the email but now I cannot get the data inputted into the contact form to submit to the email. I am somewhat new so if I sound inexperienced, I am. I appreciate the patience. Here is the current code:
app.post('/contact',urlencodedParser,(req,res)=>{
const output= `
<p>You have a new contact request</p>
<h3>Contact Details</h3>
<ul>
<li> Name:${req.body.name}</li>
<li> Email:${req.body.email}</li>
<li> Message:${req.body.message}</li>
</ul>
`;
async function main(){
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user:'[email protected]',
pass: process.env.PASSWORD
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: req.body.email, // sender address
to: "[email protected]", // list of receivers
subject: "Hello ✔", // Subject line
html: `${req.body.message}` // html body
});
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
}
main().catch(console.error);
res.render('contact')
})
This is just a forgot password controller which mails the user with a password. emailConfig.js
var email = "[email protected]";
const password = "xxxxxx";
const service = "gmail";
module.exports = {
service: service,
email: email,
password: password
}
Controller.js
var transporter = nodemailer.createTransport({
service : nodeMailerConfig.service,
auth :{
user : nodeMailerConfig.email,
pass : nodeMailerConfig.password
}
});
const mailOptions = {
from: nodeMailerConfig.email,
to: req.body.email,
subject: "Forgot Password",
html: `<p>Hi ${req.body.email}</p> <br/>
<pre>Your password is ${randomPassword}</pre><br/>
<pre>Cheers, OpenBox Team</pre>`
}
transporter.sendMail(mailOptions,(mailerr,info)=> {
if(mailerr){
res.status(400).json("Unable to send email");
}else{
res.status(201).json(info);
}
})
Please make sure that you are turning on the insecure email settings in your gmail, else most of the time the mail will be rejected or will be in spam box.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With