尽管连接正常,但 Nodemailer 无法正常工作

时间:2021-06-17 12:29:01

标签: javascript node.js nodemailer

尝试连接有效,但随后永远不会发送电子邮件,也不会抛出任何异常或任何内容。如您所见,我尝试在响应中添加一条消息(error1、success1、error2、success2)以缩小可能失败的范围,但消息也未设置。

这是我的运输车:

const transporter = nodemailer.createTransport({
  host: "my.host.com",
  port: 996,
  secure: true,
  auth: {
    user: "myuser@email.com",
    pass: "mypassword",
  },
});

然后下面的代码在一个post请求函数中。该函数的其余部分工作正常,因此端点或类似问题没有问题。

let mailOptions = {
    from: "me@mail.com",
    to: "you@mail.com",
    subject: "Test mail",
    text: "It's working",
  };
  // verify connection configuration
  let error1 = "nope";
  let success1 = "nope";
  let error2 = "nope";
  let success2 = "nope";

  try {
    transporter.verify();    
    success1 = "Server is ready to take messages";
  } catch (err) {
    error1 = "connection error";
    const error = new HttpError(`connection error: ${err}`, 500);
    return next(error);
  }

  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      error2 = `Error on sending mail: ${error}`;
    } else {
      success2 = `Mail sent: ${info.response}`;
    }
  });

  res.status(201).json({
    error1: error1,
    success1: success1,
    error2: error2,
    success2: success2,
  });

这是我提交后得到的回复:

error1: "nope",error2: "nope",success1: "Server is ready to take messages",success2: "nope"

error2 或 success2 也应该有一条消息。我错过了什么?

0 个答案:

没有答案