Nodemailer 在 Localhost 上工作,而不是在实时 Netlify 站点上工作
Nodemailer working on Localhost, not on live Netlify site
我已经使用 nodemailer/axios 从要发送到电子邮件地址的表单中获取信息。
当站点 运行 且 localhost:3000 时,邮件发送没有问题,尽管它没有通过。
我假设它必须与代理有关?
这就是我的代码的工作原理:
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
app.user(proxy("/api", { target: "http://localhost:3000"}))
}
实时站点位于:https://deathless-studio.netlify.com/#/
我尝试将目标更改为该目标,以及:https://deathless-studio.netlify.com/ and also http://deathless-studio.netlify.com/#/,但没有发送任何邮件。
我总是遇到这些错误:
https://deathless-studio.netlify.com/api/v1/sendMail 404
POST https://deathless-studio.netlify.com/api/v1/sendMail 404
我的发送邮件功能如下所示:
const sendEmail = (name, email, message, newsletter) => {
const smtpTransport = mailer.createTransport({
service: "Gmail",
auth: {
user: "(correct email)",
pass: "(correct password)"
}
})
const mail = getEmailData(name, email, message, newsletter)
smtpTransport.sendMail(mail, function(error, response) {
if(error) {
console.log(error)
} else {
alert( "Thank you! We will be in touch shortly!")
}
smtpTransport.close();
})
我的表单组件上的 handleSubmit 函数也如下所示:
handleSubmit(event) {
event.preventDefault();
const data = {
name: this.state.name,
email: this.state.email,
message: this.state.message,
newsletter: this.state.newsletter
};
Axios.post("api/v1/sendMail", data)
alert( "Thank you! We will be in touch shortly!")
}
我的 index.js 看起来像这样:
const server = require("./server");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
server.use(bodyParser.urlencoded({ extended: true }));
server.use(bodyParser.json());
server.use(cookieParser());
const { sendEmail } = require("../server/routes/mail");
server.post("/api/v1/sendMail", (req, res) => {
sendEmail(req.body.name, req.body.email, req.body.message, req.body.newsletter);
});
const port = process.env.PORT || 3000;
server.listen(port, () => {
// eslint-disable-next-line no-console
console.log("Server listening on port", port);
});
关于如何解决这个问题有什么想法吗?
非常感谢。
事实证明 Netlify 不使用 Node.js 服务器,您可以使用他们的插件。
我已经使用 nodemailer/axios 从要发送到电子邮件地址的表单中获取信息。
当站点 运行 且 localhost:3000 时,邮件发送没有问题,尽管它没有通过。
我假设它必须与代理有关?
这就是我的代码的工作原理:
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
app.user(proxy("/api", { target: "http://localhost:3000"}))
}
实时站点位于:https://deathless-studio.netlify.com/#/
我尝试将目标更改为该目标,以及:https://deathless-studio.netlify.com/ and also http://deathless-studio.netlify.com/#/,但没有发送任何邮件。
我总是遇到这些错误:
https://deathless-studio.netlify.com/api/v1/sendMail 404
POST https://deathless-studio.netlify.com/api/v1/sendMail 404
我的发送邮件功能如下所示:
const sendEmail = (name, email, message, newsletter) => {
const smtpTransport = mailer.createTransport({
service: "Gmail",
auth: {
user: "(correct email)",
pass: "(correct password)"
}
})
const mail = getEmailData(name, email, message, newsletter)
smtpTransport.sendMail(mail, function(error, response) {
if(error) {
console.log(error)
} else {
alert( "Thank you! We will be in touch shortly!")
}
smtpTransport.close();
})
我的表单组件上的 handleSubmit 函数也如下所示:
handleSubmit(event) {
event.preventDefault();
const data = {
name: this.state.name,
email: this.state.email,
message: this.state.message,
newsletter: this.state.newsletter
};
Axios.post("api/v1/sendMail", data)
alert( "Thank you! We will be in touch shortly!")
}
我的 index.js 看起来像这样:
const server = require("./server");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
server.use(bodyParser.urlencoded({ extended: true }));
server.use(bodyParser.json());
server.use(cookieParser());
const { sendEmail } = require("../server/routes/mail");
server.post("/api/v1/sendMail", (req, res) => {
sendEmail(req.body.name, req.body.email, req.body.message, req.body.newsletter);
});
const port = process.env.PORT || 3000;
server.listen(port, () => {
// eslint-disable-next-line no-console
console.log("Server listening on port", port);
});
关于如何解决这个问题有什么想法吗?
非常感谢。
事实证明 Netlify 不使用 Node.js 服务器,您可以使用他们的插件。