4

I am using nodemailer to do contact forms and mailing things.

But when I try to set the req.body.email as the from: email it just uses the authentication email instead. So all emails I receive are from: me@me.com to: me@me.com

instead of from: customer@them.com to: me@me.com

I'm pretty sure I'm doing it right

var mailOpts, smtpTrans;

  //Setup Nodemailer transport, I chose gmail. Create an routerlication-specific password to avoid problems.
  smtpTrans = nodemailer.createTransport({
      service: 'Gmail',
      auth: {
        user: "me@me.com",
        pass: "hey"
      }
  });
  //Mail options
    console.log(req.body.email);
  mailOpts = {
      from: req.body.email, //grab form data from the request body object
      to: 'me@me.com',
      subject: 'Stockist interest form',
      text: "Welcome to the My Leisure Stockists application process, we'd love to have you on board"+"\n Email: "+req.body.email
  };

  smtpTrans.sendMail(mailOpts, function (error, response) {

      if (error) {
        res.sendStatus(500);
      } else {
        res.sendStatus(200);
      };

  });
lopu
  • 67
  • 7
  • I am having the same issue. Did you ever figure it out? Or is that the expected behavior? – Michael Lynch Apr 27 '17 at 19:49
  • @MichaelLynch No actually I'm still using the solution of including "Email: "+req.body.email inside the text: property so that we can get the emails through. I don't think this is expected behaviour as I swear that when I first installed nodemailer it was setting from from: email address to the req.body.email Not any more though, we should submit a bug report – lopu Apr 28 '17 at 08:05
  • Have you figured this out now? I'm having the same exact issue. – Tommy Aug 04 '17 at 01:06
  • even I am also having this issue – user1187 Aug 09 '17 at 06:11
  • I think they fixed it... some how, it works now? – lopu Aug 18 '17 at 06:25
  • Same issue here. – Matt Gween Jan 29 '18 at 18:26

1 Answers1

0

First you need something like bodyparser. As 2019 yo can use:

app.use(express.json());
app.use(express.urlencoded({extended: true}));

Obviusly first use express:

const express = require('express');
const app = express();

The other thing is that is a text field so you put it in a string. As in es6 notation you can use:

`${req.body.email}`

So you get something like this:

const express = require('express');
const app = express();

const nodemailer = require('nodemailer')

app.use(express.json());
app.use(express.urlencoded({extended: true}));

const smtpTrans = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
      user: "me@me.com",
      pass: "hey"
    }
});

app.post('/send-email', function (req, res) {
  const mailOptions = {
    from: `${req.body.email}`, // Sender address
    to: 'me@me.com',         // List of recipients
    subject: 'Stockist interest form',
    text: "Welcome to the My Leisure Stockists application process, we'd love to have you on board"+"\n Email: "+req.body.email
  };

  smtpTrans.sendMail(mailOptions, function (error, info) {
    if (error) {
      return console.log(error);
    }
    console.log('Message sent: ' + info.response);
  });

  res.redirect("/index.html");
})