以联系方式表达req body empty

时间:2018-05-15 13:31:22

标签: express nodemailer

我正在使用nodemailer来实现自己从联系表单发送电子邮件。 我的app.js看起来像这样

    app.post('/jobs/join-us', (req, res) => {
  console.log(req.body); //to return body
  const output = `


      <p>You have a new message from contact form.</p>
      <h3>Contact Details</h3>
      <ul> 
        <li>Name: ${req.body.name}</li>
        <li>Email: ${req.body.email}</li>
      </ul>
      <h3>Message</h3>
      <p>${req.body.message}</p>
    `;
  // create reusable transporter object using the default SMTP transport
  let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    secure: false, // true for 465, false for other ports
    auth: {
      user: 'xx', // generated ethereal user
      pass: 'xx',
    },
    tls: {
      rejectUnauthorized: false
    }
  });

  // setup email data with unicode symbols
  let mailOptions = {
    from: 'xx', // sender address
    to: 'xx', // list of receivers
    subject: 'Contact Request', // Subject line
    text: 'Hello world?', // plain text body
    html: output // html body
  };

  // send mail with defined transport object
  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      console.log(error);
      res.end("error");
    } else {
      console.log('Message sent: %s', info.messageId);
      //console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
      res.sendStatus(200);
    }
  });
});

有人能帮帮我吗?我也尝试在const输出之前使用console.log(req)之类的东西但是没有&#39;给我任何可行的回报。

这是我的联系表单本身,POST请求返回200。

       <div class='input-wrapper'>
<input class="flying-label-input" type="text" name="job_form[role]" id="job_form_role" />
<label class="flying-label required" for="job_form_role">Role</label>
</div>
<div class='input-wrapper'>
<input class="flying-label-input" type="text" name="job_form[email]" id="job_form_email" />
<label class="flying-label required" for="job_form_email">E-mail</label>
</div>
<div class='input-wrapper'>
<input class="flying-label-input" type="text" name="job_form[phone_number]" id="job_form_phone_number" />
<label class="flying-label" for="job_form_phone_number">Phone number</label>
</div>
<div class='input-wrapper'>
<label class="label required" for="job_form_cv">CV (PDF)</label>
<input type="file" name="job_form[cv]" id="job_form_cv" />
</div>

<div class='input-wrapper-space-top'>
<input type="hidden" name="job_form[referer]" id="job_form_referer" />
<input type="submit" name="commit" value="Submit Job Application" class="btn-round btn-primary" />
</div>

我的app.js中间件功能:&#39;

const express = require('express')
var app = express();
var path = require('path')
const nodemon = require('nodemon')
const nodemailer = require('nodemailer');
const bodyParser = require('body-parser');

// Static folder
app.use(express.static('public'))

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

1 个答案:

答案 0 :(得分:0)

req.body默认为undefined,因此您获得空对象的事实意味着您没有使用req.body和/或{{填充body-parser 1}}。事实上,错误捕获multer不会在控制台中返回错误,因此req.body存在并且它是一个空对象。在app.post之前尝试以下快速中间件,并按如下方式更新app.post:

if statement