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

时间:2018-06-05 16:56:55

标签: javascript node.js nodemailer

所以我的投资组合网站出现了这个问题,我是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')});

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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