使用nodemailer一次发送多封电子邮件

ovda96

我正在尝试使用来基于数据库中的条目发送电子邮件nodemailer我曾尝试几乎任何我能想到的(手动创建的阵列Promises,并使用在一个电话Promise.all(),这样做,使用map等),但我总是不断收到同样的错误:UnhandledPromiseRejectionWarning: Error: Message failed: 450 SQLITE_ERROR: cannot start a transaction within a transactionnodemailer-documentation明确指出-当不传递callback -fn时-functiontransporter.sendMail()被包装为一个promise。但是,当我像这样手动定义这些promise的数组时...

const transporter = nodemailer.createTransport(serverData);
const mailData = ...;

const arr = [transporter.sendMail(mailData), transporter.sendMail(mailData)];

...即使我什至还没有“遍历”该数组,也已经触发了相同的错误Promise.all()是否只有在手动指定时才运行数组中的函数,这是一个误解?

无论如何,这是我完整的数据库代码;我正在使用sequelize从数据库中检索数据。我已经验证了从该数据库的数据库侧检索数据没有问题。

class Mail extends Model {
...

  static resendUndeliveredMails(){
    this.findAll()
        .then((mails) => {
          return Promise.all(mails.map(async mail => {
             transporter.sendMail(mail.dataValues);
          }));
        })
        .catch((e) => {
          console.log(e);
        });
  }
}

任何帮助将不胜感激!先感谢您 :)。

查姆斯丁·布赞

我已经使用promise.all测试了nodeMailer API,但我没有发现任何问题,这是我在下面使用的代码,而另一错误是我认为与均衡器或SQL有关,这是导致错误的原因由SQLITE_ERROR 抛出UnhandledPromiseRejectionWarning: Error: Message failed: 450 SQLITE_ERROR: cannot start a transaction within a transaction,另一件事是您在promise中编写异步操作的方式我认为是错误的,我将在此处与您分享应编写函数的方式,但是有关您在运行promise.all之前会发出promise的第一个问题这就是创建类似Array的所有操作,此时[promise(arg)]您正在调用该函数,因此即使未将其放入promise.all中,promise也将启动,node将对其进行处理。

 static async resendUndeliveredMails() {
try {
  const mails = await findAll();
  const mailerPromises = mails.map((mail) => transporter.sendMail(mail.dataValues));
  const responses = await Promise.all(mailerPromises);
  console.log(responses, "All Mails Have Been Sent Successfully");
} catch (e) {
  console.log(e);
}

}

const nodemailer = require("nodemailer");

const transporter = nodemailer.createTransport({
  service: "gmail",
  auth: {
    user: "user",
    pass: "pass", // naturally, replace both with your real credentials or an application-specific password
  },
});

const mailOptions = {
  from: "[email protected]",
  to: "[email protected]",
  subject: "testing due",
  text: "Dudes, we really need your money.",
};

const mailOptions2 = {
  from: "[email protected]",
  to: "[email protected]",
  subject: "LOL due",
  text: "Dudes, we really need your money.",
};

Promise.all([
  transporter.sendMail(mailOptions),
  transporter.sendMail(mailOptions2),
])
  .then((res) => console.log(res))
  .catch((err) => console.log(err));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用nodemailer发送电子邮件

如何使用Codeigniter使用cc发送多封电子邮件?

对多封电子邮件执行一次批处理脚本

如何使用nodemailer和电子邮件模板发送消息?

使用Namecheap电子邮件发送Nodemailer电子邮件

使用nodemailer发送电子邮件时出错

使用Angular和NodeMailer发送电子邮件

使用nodemailer发送电子邮件

使用nodemailer发送AMP电子邮件

SMTP发送多封电子邮件

使用Google脚本发送到多封电子邮件

使用python发送多封电子邮件

使用codeigniter发送多封电子邮件

阻止用户发送多封电子邮件

一次删除多封邮件

如何处理:使用相同的电子邮件地址获取所有行,对ID进行操作,然后通过电子邮件发送一次

电子邮件仅发送一次

PHPMailer发送多封电子邮件失败

如何使用keystone-nodemailer发送电子邮件

如何在R中发送读取csv的电子邮件并一次发送多封电子邮件?

Drupal邮件一次发送超过1000封电子邮件

PHP:如果$ date是今天,请发送电子邮件-但仅发送一次

无法使用 nodemailer 在 nodejs 中发送电子邮件

如何使用 nodemailer 和 gmail 发送电子邮件?

无法使用 nodemailer 发送带有附件的电子邮件

发送多封电子邮件时出错,应该只发送一封

无法使用 nodemailer 和 Outlook 发送电子邮件

使用 Nodemailer 发送电子邮件时出错

我想在控制台应用程序中使用 smtp 一次发送多个电子邮件请求。如何使用线程一次发送批量电子邮件请求