【问题标题】:Nodemailer not sending email, displays "Message sent:Undefined"Nodemailer 不发送电子邮件,显示“已发送消息:未定义”
【发布时间】:2021-11-05 18:37:49
【问题描述】:

它正在工作,然后它没有。我发了几封邮件,过了一会儿它就停止工作了。我明白了 “已发送消息:未定义” (node:9048) UnhandledPromiseRejectionWarning: 错误: spawn sendmail ENOENT 在 Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)

我不知道为什么。

前端 - Axios 控制台记录响应数据,因此服务器和前端都在工作。这只是 Nodemailer 的问题。

感谢任何帮助。谢谢!

const express = require("express");
const app = express();
const cors = require("cors");
const nodemailer = require("nodemailer");
const path = require("path");
const fs = require("fs");
const readData = path.join(__dirname, "../email_take2/data.json");
const { v4: uuidv4 } = require("uuid");


app.use(express.json());
app.use(cors());

const port = process.env.PORT || 5000;
app.listen(5000, () => console.log(`Listening at port ${port}`));
if (process.env.NODE_ENV === "production") {
  // Set static folder
  app.use(express.static("../client/build"));

  app.get("*", (req, res) => {
    res.sendFile(path.join(__dirname, "../client", "build", "index.html"));
  });
}
function listUserData() {
  const data = fs.readFileSync(readData);
  return JSON.parse(data);
}

app.post("/sendEmail", function (req, res) {
  console.log(req.body);
  const emailInfo = {
    id: uuidv4(),
    email: req.body.email,
    cc: req.body.cc,
    message:req.body.message,
  };
  const dataArray = listUserData();
  dataArray.push(emailInfo);
  fs.writeFileSync(readData, JSON.stringify(dataArray));
  res.send(emailInfo);
  console.log("SentBody", emailInfo);

  let transporter = nodemailer.createTransport({
    sendmail:true,
    host: "smtp.outlook.com",
    port: 587,
    secure: false, // true for 465, false for other ports
    tls: {
        ciphers: "SSLv3",
           },
    auth: {
      user: "memail@outlook.com", // generated ethereal user
     pass: "mypw", // generated ethereal passwordAccount.pass, 
    },
  });


  // send mail with defined transport object
  let info = transporter.sendMail({
    from: '"Fred Foo ????" <foo@example.com>', // sender address
    to: "garbageacc7878@outlook.com", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world?", // plain text body
    html: "<b>Hello world?</b>", // html body
  });
  console.log("Message sent: %s", info.messageId);
  return emailInfo;
});

【问题讨论】:

    标签: javascript node.js express email nodemailer


    【解决方案1】:

    transporter.sendMail 返回一个承诺,这就是为什么你的控制台日志有undefined。所以要么附上.then.catch

    transporter.sendMail(...)
    .then((data)=>{console.log('Mail sent', data)})
    .catch(err => {console.error('Failure',err)})
    

    或者将您的请求处理程序设为异步函数并使用async awaittryCatch

    try{
    let data = await transporter.sendMail(...)
    console.log('Mail sent', data)
    } catch (err) {
    console.error('Failure',err)
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      相关资源
      最近更新 更多