客户端数据未到达服务器端......也许

Client side data not reaching server side...maybe

我有一个联系表,正在尝试使用这些值向自己发送电子邮件。服务器正在接收 req.body 并且它出现在控制台中,但是,我的 nodemailer 文件无法使用该数据。我得到一个状态码 "Need at least one of 'text' or 'html' parameters specified..." mail.js 和 server.js 都在根目录中,我将数据传递给 server.js 使用 jQuery 然后 module.exports mail.js 到 server.js 但是在交换的某个地方,客户端数据没有进入我在 mail.js 中创建的对象,即使它只在 server.js.

中被调用

这是我的 mail.js 减去敏感信息后的内容:

const nodemailer = require("nodemailer");
const mailGun = require("nodemailer-mailgun-transport");

const auth = {
  auth: {
    api_key: "...",
    domain: "..."
  }
};

const transporter = nodemailer.createTransport(mailGun(auth));

const sendMail = (name, subject, email, phone, message, cb) => {
  const mailOptions = {
    from: email, 
    to: "...", // TODO: the receiver email has to be authorized for the free 
         tier
    name,
    phone,
    subject,
    message
  };

  transporter.sendMail(mailOptions, function (err, data) {
    if (err) {
      cb(err, null);
    console.log("error occurs");
    console.log(err)
    } else {
      cb(null, data);
    console.log("Message sent");

    }
  });
};

newFunction();
function newFunction() {
    module.exports = sendMail;
}

这是我的 HTML 和我的 server.js:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>        // Chunk 1
        const express = require('express');
        const path = require('path');
        const sendMail = require('./mail');
        const log = console.log;
        const app = express();
    
        const PORT = 8080;
    
    
        // Data parsing
        app.use(express.urlencoded({
          extended: false
        }));
        app.use(express.json());
    
        //STATIC FOLDER
        app.use("/public", express.static(path.join(__dirname, "public")));
    
        // Render home page
        app.get('/', (req, res) => {
          res.sendFile(path.join(__dirname, 'views', 'index.html'));
        });
    
    
        // field data
        app.post('/email', (req, res) => {
    
          const {
            name,
            subject,
            email,
            phone,
            message
          } = req.body;
          log('Data: ', req.body);
    
          sendMail(name, subject, email, phone, message, function(err, data) {
            if (err) {
              res.status(500).json({
                message: 'Internal Error'
              })
            } else {
              res.json({
                message: "Email sent!!!!"
              })
            }
          });
        });
    
    
        // Error page
        app.get('/error', (req, res) => {
          res.sendFile(path.join(__dirname, 'views', 'error.html'));
        });
    
        // Email sent page
        app.get('/email/sent', (req, res) => {
          res.sendFile(path.join(__dirname, 'views', 'emailMessage.html'));
        });
    
    
        // Start server
        app.listen(PORT, () => log(`Server is starting on PORT, ${PORT}`));
    
    
    
        <!DOCTYPE html>
        <html lang="en">
    
        <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <meta http-equiv="X-UA-Compatible" content="ie=edge">
          <title>Contact Me</title>
        </head>
    
        <body>
          <div class="container">
            <h1 class="brand"><span>King</span> Major</h1>
            <div class="wrapper animated zoomIn">
              <div class="subject-info">
                <ul>
                  <li><i class="fa fa-road"></i>...</li>
                  <li><i class="fa fa-phone"></i>...</li>
                  <li><i class="fa fa-envelope"></i>...</li>
                  <li>
                    <a class="navbar-brand" href="#home"><img src="../public/images/mstile- 
   150x150.png" alt="King's Brand Logo"></a>
                  </li>
                </ul>
              </div>
              <div class="contact">
                <h3>Contact Me</h3>
                <form method="POST" action="send">
                  <p>
                    <label>Name</label>
                    <input type="text" name="name" id="name">
                  </p>
                  <p>
                    <label>Subject</label>
                    <input type="text" name="subject" id="subject">
                  </p>
                  <p>
                    <label>Email Address</label>
                    <input type="email" name="email" id="email">
                  </p>
                  <p>
                    <label>Phone Number</label>
                    <input type="text" name="phone" id="phone">
                  </p>
                  <p class="full">
                    <label>Message</label>
                    <textarea name="message" rows="5" id="message"></textarea>
                  </p>
                  <p class="full">
                    <button type="submit" value="Submit">SEND</button>
                  </p>
                </form>
              </div>
            </div>
          </div>
    
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
   </script>
          <script>
            $("form").on("submit", e => {
              e.preventDefault();
    
              const name = $("#name")
                .val()
                .trim();
              const subject = $("#subject")
                .val()
                .trim();
              const email = $("#email")
                .val()
                .trim();
              const phone = $("#phone")
                .val()
                .trim();
              const message = $("#message")
                .val()
                .trim();
    
              const data = {
                name,
                subject,
                email,
                phone,
                message
              };
    
              $.post('/email', data, function() {
    
                console.log('Server received our data.')
                  .then(() => {
                    window.location.href = "/email/sent";
                  })
                  .catch(() => {
                    window.location.href = "/error";
                  });
              });
            });
          </script>
        </body>
    
        </html>

根据错误消息(期望字段 texthtml 存在)和一般的 nodemailer 文档(不包括 message 字段),看起来你只需要在 mailOptions 对象中将 message 字段重命名为 text。所以你会:

const mailOptions = {
    from: email, 
    to: "...", // TODO: the receiver email has to be authorized for the free 
         tier
    name,
    phone,
    subject,
    text: message // <-- Simple change!
  };