节点邮件程序,在本地工作正常,但在我的服务器上不起作用



所以我在我的投资组合网站上遇到了这个问题,我是nodejs的新手,我想制作一个电子邮件表单,以便访问我网站的人可以与我联系。当我在Mac上本地运行时,它工作得很好,但是一旦我将其放在服务器上,我就会收到此错误{"message":"Something went wrong","error":{"code":"EAUTH","response":"535 Authentication Failed","responseCode":535,"command":"AUTH PLAIN"}}

她是我的代码的样子

require('dotenv').config();
const express = require('express');
const app = express();
const nodemailer = require('nodemailer');
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('public'));
// POST route from contact form
app.post('/contact', function (req, res) {
let mailOpts, smtpTrans;
smtpTrans = nodemailer.createTransport({
host: 'smtp.zoho.com',
port: 465,
secure: true,
auth: {
user: process.env.MAIL,
pass: process.env.PASS
}
});
mailOpts = {
from: process.env.MAIL,
to: process.env.MAIL,
subject: 'Nytt mail från ' + req.body.name,
text: `${req.body.name} (${req.body.email}) :
${req.body.message}`
};
smtpTrans.sendMail(mailOpts, function (error, response) {
if (error) {
res.status(400).json({ message: 'Something went wrong', error });
}
else {
res.status(200).json({ message: 'Success' });
}
});
});
app.listen(3000, () => {console.log('server is on, 3000')});

检查此环境的环境变量,对于服务器,您需要设置.bash_profile或其他形式的设置 ENV 变量。

我发现了问题,显然当我控制台.log(process.env(时变量名称"MAIL"已经被采用,因此我将其重命名为EMAIL,现在它可以工作了。

相关内容

最新更新