nodemailer不从节点服务器发送电子邮件吗?

时间:2019-05-20 04:44:25

标签: node.js reactjs express nodemailer

我创建了一个带有React前端以及一个node.js和nodemailer后端的表单。我正在尝试通过电子邮件发送该数据。我可以将其发送到后端。在那里,它被完美接收。但是,当从Transporter.sendMail函数执行“未发送邮件”错误时,发送电子邮件时出现问题,我不确定为什么。

希望获得帮助。请查看下面用于我的服务器的代码。出于安全考虑,我显然已删除了身份验证详细信息。

控制台日志错误:

[0]未发送

const nodemailer = require('nodemailer');
const path = require('path');
const express = require("express");
const app = express();
const port = 5000;
const cors = require("cors");
app.use(cors());
const bodyParser = require("body-parser");
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(
  bodyParser.urlencoded({
    // to support URL-encoded bodies
    extended: true
  })
);

const transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'email',
    pass: 'password',

  }
});




app.get("/home", (req, res) => {
    console.log("Hello from .get /home", req.body.generalDetails, req.body.firstName, req.body.mName );

})






app.post("/home", (req, res) => {

  var mailOptions = {
                from: 'email',
                to: 'email',
                subject: 'Hello',
                text: req.body.generalDetails,

            }

            transporter.sendMail(mailOptions, function(err, res) {
                if (err) {
                    console.log('Mail not sent');
                } else {
                    console.log('Mail sent');
                }
            });

  let data = [{
    //page one data
    generalDetails: req.body.generalDetails,
    fName: req.body.fName,
    mName: req.body.mName,
    lName: req.body.lName,
    email: req.body.email,

   }];

   res.json(data);
 }

);



app.listen(port, () => `Server running on port ${port}`);

1 个答案:

答案 0 :(得分:0)

在最近的节点邮件程序更改中,他们在auth中引入了auth密钥。当前您在auth对象中传递密码和用户。我进行了这些更改,节点邮件程序开始工作。通过

if ($user['email'] === $email) {

         $emailerr="Email Already Exists";
}

}`

我希望这样会起作用。