簡體   English   中英

客戶端數據未到達服務器端......也許

[英]Client side data not reaching server side…maybe

我有一個聯系表格,我正在嘗試使用這些值向自己發送 email。 服務器正在接收 req.body 並出現在控制台中,但是,我的 nodemailer 文件無法使用數據。 我得到一個狀態代碼“需要至少指定一個'text'或'html'參數......” mail.js 和 server.js 都在根目錄中,我使用 jQuery 然后模塊將數據傳遞給 server.js .exports mail.js 到 server.js 但在交換的某個地方,客戶端數據沒有進入我在 mail.js 中創建的 object,即使它只在 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:


  
  
  
        // 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字段),看起來您只需要將您的message字段重命名為mailOptions object 中的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!
  };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM