我尝试使用nodemailer和axios进行联系表单。不幸的是,我收到错误消息“ POST“ localhost:3000 / api / form'400错误的请求”。请看下面的代码。
问题的屏幕截图。 click me to see >
下面是带有表单的文件。我想从这里发送请求。
constructor() {
super();
this.state = {
MARKA: "",
MODEL: "",
ROCZNIK: "",
POJEMNOŚĆ: "",
ADRES: "",
IMIĘ: "",
TELEFON: "",
EMAIL: ""
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange = e => {
this.setState({ [e.target.name]: e.target.value });
};
handleSubmit(e) {
e.preventDefault();
const { MARKA, MODEL, ROCZNIK, POJEMNOŚĆ, ADRES, IMIĘ, TELEFON, EMAIL} = this.state;
axios.post('/api/form', {
MARKA, MODEL, ROCZNIK, POJEMNOŚĆ, ADRES, IMIĘ, TELEFON, EMAIL
});
}
下面是我要使用请求中的表单值并发送电子邮件的文件。
const nodemailer = require("nodemailer");
const express = require("express");
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.post('/api/form', (req, res) => {
nodemailer.createTestAccount((err, account) => {
const htmlEmail = `
<h3>Pan/Pani ${req.body.IMIĘ} chce wycenić:</h3><br />
<ul>
<li>Marka: ${req.body.MARKA}</li>
<li>Model: ${req.body.MODEL}</li>
<li>Rocznik: ${req.body.ROCZNIK}</li>
<li>Pojemność: ${req.body.ROCZNIK}</li>
</ul><br />
<h5>Dane kontaktowe oraz adres:</h5>
<ul>
<li>Numer telefonu: ${req.body.TELEFON}</li>
<li>Adres e-mail: ${req.body.EMAIL}</li>
<li>Lokalizacja: ${req.body.ADRES}</li>
</ul>
`;
let transport = nodemailer.createTransport({
service: "gmail",
auth: {
user: "deivuss331@gmail.com",
pass: "fuckingpasswd"
}
});
const message = {
from: "deivuss331@gmail.com",
to: "deivuss331@gmail.com",
subject: "AUTOPLUS GORLICE - Wniosek o wycenę pojazdu",
html: htmlEmail
};
transport.sendMail(message, (err, info) => {
if (err) {
console.log(err);
}
console.log('Wysłano');
});
})
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log("worksss");
});
``
答案 0 :(得分:0)
您的快递服务器需要响应客户端(网络)。因此,如下更改transport.sendMail
以响应客户。
transport.sendMail(message, (err, info) => {
if (err) {
console.log(err);
return res.send(err.message);
}
res.send('OK');
console.log('Wysłano');
});