[英]Nodemailer, works fine locally but doesn't work on my server
So I got this problem with my portfolio website, I'm new to nodejs and I wanted to make an email form so people visiting my website can get in contact with me. 所以我在我的投资组合网站上遇到了这个问题,我是Nodejs的新手,我想制作一封电子邮件表格,以便访问我网站的人可以与我联系。 It works great when I run it locally on my mac but as soon as I put it on my server I get this error {"message":"Something went wrong","error":{"code":"EAUTH","response":"535 Authentication Failed","responseCode":535,"command":"AUTH PLAIN"}}
当我在Mac上本地运行它时,它的工作效果很好,但是一旦将其放到服务器上,我就会收到此错误{"message":"Something went wrong","error":{"code":"EAUTH","response":"535 Authentication Failed","responseCode":535,"command":"AUTH PLAIN"}}
Her is how my code looks like this 她就是我的代码看起来像这样
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变量的某种其他形式。
我发现了问题,当我console.log(process.env)时,显然已经使用了变量名“ MAIL”,因此我将其重命名为EMAIL,现在可以使用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.