简体   繁体   English

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

[英]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.

相关问题 html 文件中的 javascript 在我使用 VS Code 的实时服务器中工作正常,但如果我在本地打开 .html 文件,它就不起作用 - javascript in a html file works fine in my live server using VS Code but if i open the .html file locally it doesn't work Express.js应用程序在本地可以正常运行,但是在Heroku上部署时无法正常工作 - Express.js app works fine locally but doesn't work when deployed on Heroku Nodemailer - 在本地工作但不在生产中 - Nodemailer - Works locally but not on production getJSON在服务器上不起作用,但在本地起作用 - getJSON doesn't work on the server but does locally 从服务器加载的js文件不起作用,但在本地加载的工作 - Js file loaded from server doesn't work, but loaded locally works 我的Optin表单在Internet Explorer中不起作用,但在Firefox中可以正常工作 - My Optin form doesn't work in Internet Explorer, but works fine in Firefox Flip Clock 代码可在 Codepen 上运行,但不能在本地运行 - Flip Clock code works on Codepen and doesn't work locally javascript中的click()无法正常运行,但在控制台中可以正常运行 - click() in javascript doesn't work but in console it works fine Javascript在IE中不起作用,在其他所有方面都可以正常工作 - Javascript doesn't work in IE, works fine in everything else WebDriverIO 6 – $ 工作正常,$ 在 iframe 中不起作用 - WebDriverIO 6 – $ works fine, $ doesn't work in iframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM