Nodemailer,在本地工作正常但在我的服务器上不工作

Nodemailer, works fine locally but doesn't work on my server

所以我的投资组合网站出现了这个问题,我是 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 变量的其他形式。

我发现了问题,显然变量名 "MAIL" 在我 console.log(process.env) 时已经被占用,因此我将其重命名为 EMAIL,现在可以使用了。