如何通过 Promise (Bluebird) 使用 NodeMailer(不是 SMTP)发送邮件?

How to Sendmail Using NodeMailer (Not SMTP) With Promise (Bluebird)?

如何使用 nodemailer + bluebird (Promise) 发送邮件。我成功发送到电子邮件,但没有回复(在页面中它仍在加载并且没有任何回复,但它成功发送到电子邮件)。

这是文件 js :

    return new Promise(resolve => {
    var transporter = nodemailer.createTransport({
        sendmail: true,
        newline: "windows",
        logger: false
      }),
      message = {
        from: "noreply@from.com",
        to: variabletoemail,
        subject: variablesubject,
        html: variablesendhtml
      };
    transporter.sendMail(message, (error, response) => {
      if (error) {
        resolve("0"); //can't call
      } else {
        resolve("1"); //can't call
      }
    });
  });

有谁知道我上面的问题。请帮助我

根据文档,发送邮件 return 承诺,您不能将其与回调传递一起使用。 您可以将它与 await 一起使用,并使用 try catch 来解决或拒绝您的承诺。

return new Promise(async (resolve, reject) => {
    var transporter = nodemailer.createTransport({
        sendmail: true,
        newline: "windows",
        logger: false
      }),
      message = {
        from: "noreply@from.com",
        to: variabletoemail,
        subject: variablesubject,
        html: variablesendhtml
      };
    try{
      await transporter.sendMail(message);
      resolve("1")
    }catch(e){
      reject(e)
    }
  });

或没有try catch:

return new Promise((resolve, reject) => {
        var transporter = nodemailer.createTransport({
            sendmail: true,
            newline: "windows",
            logger: false
          }),
          message = {
            from: "noreply@from.com",
            to: variabletoemail,
            subject: variablesubject,
            html: variablesendhtml
          };
          transporter.sendMail(message).then(()=>{
           resolve("1")
          }).catch((e) => {
           reject(e)
          });
      });