我已经部署了一个网站,并且在我的机器上一切正常,但是在部署的站点上的react / nodemailer联系人表格不起作用。
这是我的handlesubmit的代码: 异步handleSubmit(e){ e.preventDefault()
const { name, email, phone, zip, select, message } = this.state
const form = await axios.post("http://localhost:8080/api/form", {
name,
email,
phone,
zip,
select,
message
})
.then((response) => {
if (response.data.msg === 'success') {
alert("Message Sent.");
this.resetForm()
} else if (response.data.msg === 'fail') {
alert("Message failed to send.")
}
})
}
这是路线: router.post('/ api / form',(req,res)=> { console.log(要求正文)
nodemailer.createTestAccount((err, account) => {
const htmlEmail = `
<h3> Contact Details</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Email: ${req.body.email}</li>
<li>Phone: ${req.body.phone}</li>
<li>Zip: ${req.body.zip}</li>
<li>Referral: ${req.body.select}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`
let transporter = nodemailer.createTransport({
service: "gmail",
// no port needed for gmail
auth: {
user: process.env.NODEMAILER_ADDRESS,
pass: process.env.NODEMAILER_PASSWORD
}
})
let mailOptions = {
from: 'form@form.com',
to: process.env.NODEMAILER_ADDRESS,
subject: 'You Have A New Contact',
text: req.body.message,
html: htmlEmail
}
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
return console.log(err)
}
console.log("Message Sent: %s", info.message)
console.log('Message URL: %s', nodemailer.getTestMessageUrl(info))
})
});
});
这是我得到“ OPTIONS http://localhost:8080/api/form net :: ERR_CONNECTION_REFUSED”的确切错误,以及 “未捕获(承诺)错误:网络错误 在e.exports(createError.js:17) 在XMLHttpRequest.f.onerror(xhr.js:80)”
感谢您的帮助。谢谢!