Sending mail with nodemailer - email from the field is incorrect

Trying to set up a contact form using nodemailer. Here is what in my app.js:

// EMail configuration var smtpTransport = nodemailer.createTransport("SMTP",{ service: "Gmail", auth: { user: "myemailaddress", pass: "xxx" } }); // CONTACT FORM app.get('/contact', function (req, res) { res.render("contact"); }); app.post('/contact', function (req, res) { var mailOptions = { from: req.body.email, // sender address to: "myemailaddress", // list of receivers subject: req.body.subject, // Subject line text: req.body.message, // plaintext body } smtpTransport.sendMail(mailOptions, function(error, response){ if(error){ console.log(error); }else{ console.log("Message sent: " + response.message); } smtpTransport.close(); // shut down the connection pool, no more messages }); res.render("contact", { success: "building web app" }); }); 

And my contact.jade template is as follows:

 form#contact-form(action="/contact", method="POST") div.span5 p Full name: input#name(type="text", name="name") p Email address: input#email(type="email", name="email") p Subject: input#subject(type="text", name="subject") p Message: textarea#message(type="text", name="message", rows="5") p: button(type="submit") Send message 

Now the email works, but it comes from myemailaddress, and not from the one that I enter in the email field in the template. Any ideas

+4
source share
2 answers

Gmail and many other email services do not allow sending messages with a different FROM field.

+4
source

you can use the postmark, they provide an excellent api for sending emails, and there is a node module for it ( https://npmjs.org/package/postmark )

0
source

Source: https://habr.com/ru/post/1484673/


All Articles