简体   繁体   中英

Nodemailer with Gmail

I'm trying to implement contact form for my website using Nodemailer and Gmail, but unfortunately something is wrong. I can't find the issue. Below is my post route:

 app.post('/form', cors(), (req, res, next) => { let transporter = nodemailer.createTransport({ host: 'smtp.gmail.com', port: 465, secure: true, auth: { user: 'some.mail@gmail.com', pass: 'password' }, tls: { rejectUnauthorized: false } }); const htmlEmail = ` <h3>Contact Details</h3> <ul> <li>Name: ${req.body.author}</li> </ul> <h3>Message:</h3> <p>${req.body.body}</p> ` let mailOptions = { from: req.body.author, to: 'some@mail.com', replyTo: req.body.author, subject: "Portfolio Website - Contact", text: req.body.body, html: htmlEmail }; transporter.sendMail(mailOptions, (error, info) => { if (error) { console.log(error); return res.json({err: error}); } console.log('Message sent: %s', info.messageId); res.redirect('form'); }); }); 

And here is my HTML form:

 <form method="POST" action="form" accept-charset=utf-8> <div class="form-group"> <textarea name="body" placeholder="Write an e-mail here..." class="form-control"></textarea> </div> <div class="form-group"> <input type="text" name="author" placeholder="Author" class="form-control author" /> </div> <div class="form-group"> <input class="btn btn-lg btn-secondary btn-block" type="submit" value="Send" /> </div> </form> 

I will be greteful with any help!

Cheers, Szymon

PS. Here is the link to the repo on GitHub: https://github.com/alojzz/alojzz.github.io/blob/master/server.js

I had the same problem, i changed 'Less secure app access' in security of my google account to be 'on'. And that solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM