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



我有一个联系人表单,正在尝试使用这些值向自己发送电子邮件。服务器正在接收req.body,它出现在控制台中,但是,我的nodemailer文件无法使用数据。我得到一个状态代码"需要至少指定一个'text'或'html'参数…"mail.js和server.js都在根目录中,我使用jQuery将数据传递给server.js,然后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:


// 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!
};

相关内容

  • 没有找到相关文章

最新更新