1

I have an issue with req.body being undefined in nodemailer.

React: I have the whole function written and all the data is correctly passed. console.log(data) gives me the correct object.

 axios
  .post(`${this.state.API_URL}/email/send`, JSON.stringify(data))
  .then((response) => {
  });

Express: just the important parts for the nodemailer.

const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const emailRoute = require("./routes/api/emailRoute");
app.use("/email", emailRoute);

emailRoute:

 router.post("/send", async (req, res) => {
  const EmailAddress = process.env.EMAIL_ADDRESS;
  const transporter = nodemailer.createTransport({
    service: "hotmail",
    auth: {
      user: EmailAddress,
      pass: process.env.EMAIL_PASS,
    },
  });

  await transporter.sendMail(
    {
      from: req.body.email,
      to: EmailAddress,
      subject: "Email from app",
      text: `name: ${req.body.name} 
      current-role: ${req.body.role} 
      sports-car: ${req.body.enthusiast} 
      comment: ${req.body.comment}`,
    },
    (err, data) => {
      if (err) {
        res.sendStatus(503).json(err);
      } else {
        res.sendStatus(200).json("email sent");
      }
    }
  );
});

I'm getting the email to my email account, but this is what I get.

from [unknown]
      name: undefined
      current-role: undefined
      sports-car: undefined
      comment: undefined

What am I doing wrong ?

Thank you.

kyh8725
  • 21
  • 4

2 Answers2

1

I finally got it working. So no JSON.stringify(data) just data in axios.

 axios
.post(`${this.state.API_URL}/email/send`, data)
.then((response) => {
});

and in emailRoute

  const EmailAddress = process.env.EMAIL_ADDRESS;
 const transporter = nodemailer.createTransport({
service: "hotmail",
auth: {
  user: EmailAddress,
  pass: process.env.EMAIL_PASS,
},
});


 await transporter.sendMail(
{
  from: EmailAddress,
  to: EmailAddress,
  subject: "Email from app",
  text: 
 `name: ${req.body.name} 
  email: ${req.body.email}
  current-role: ${req.body.role} 
  sports-car: ${req.body.enthusiast} 
  comment: ${req.body.comment}`,
 }

the email address for 'user' and 'from' should be the same. I thought I could use the sender's email for 'from' which I get from the form in React, but I guess not. For my case I'm sending and receiving email from the same email account of mine, and I'm just passing the sender's email address in the 'text'.

kyh8725
  • 21
  • 4
0

You Have To Use Body parser In Every Single Router! So If You Didn't Add It To Email Route Then Add It! All The Best

  • Still the same result after importing everything. is this what you mean or I have to pass body-parser as argument or something ? to the router.post? const express = require("express"); const bodyParser = require("body-parser"); const app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); const router = express.Router(); const nodemailer = require("nodemailer"); require("dotenv").config(); – kyh8725 Nov 04 '20 at 05:05
  • OK First Of All can you just specify the files you have – Monzer Abdul Aziz Nov 04 '20 at 06:03
  • and you don't have to but as an argument at all that's will made a bug – Monzer Abdul Aziz Nov 04 '20 at 06:07
  • I'm Sorry English Is Not My Language – Monzer Abdul Aziz Nov 04 '20 at 06:07
  • as in the question, I have a react component where I make axios call, server.js which has all the back-end and emailRoute which has nodemailer. I imported body-parser both in server.js and emailRoute.js. – kyh8725 Nov 04 '20 at 10:01