在 nodemailer 中找不到收件人

No recipients found in nodemailer

我正在尝试使用 nodemailer 发送邮件。 我想我已经正确地给出了所有的 mailOptions。正如我通过将 mailOptions 打印到控制台所确认的那样。

而且我还检查了身份验证和用户。它们是正确的并且是进口的。

但我仍然收到没有收件人定义的错误。问题出在哪里?

下面是整个 sendEmail 函数

function sendEmail(payload) {
var fromEmail = config.cfg.smtp.fromEmail;

console.log(fromEmail);
var toEmail = payload.to;
var subject = payload.subject;
var content = `<p>Greetings! your order is on the way. Order details are</p>`;
let smtpTransport = nodemailer.createTransport({
  service: "gmail",

  auth: {
    user: config.cfg.smtp.auth.user,
    pass: config.cfg.smtp.auth.pass,
  },
});
// setup email data with unicode symbols
let mailOptions = {
  from: fromEmail, // sender address.  Must be the same as authenticated user if using Gmail.
  to: toEmail, // receiver
  subject: subject, // subject
  html: content, // html body
};

return new Promise(function (resolve, reject) {
  smtpTransport.sendMail({ mailOptions }, function (err, data) {
    console.log(mailOptions)

    if (err) {
      console.log("your mail could not be sent");
      return reject(err);
    }
    resolve(data);
    console.log("Your mail is sent");
  });
});

}

您正在将 mailOptions 发送到另一个对象中。请尝试以下方法。

return new Promise(function (resolve, reject) {
  smtpTransport.sendMail(mailOptions, function (err, data) { // mailOptions is already an object.
    console.log(mailOptions)

    if (err) {
      console.log("your mail could not be sent");
      return reject(err);
    }
    resolve(data);
    console.log("Your mail is sent");
  });