使用 nodemailer 一次发送多封电子邮件 [英] Sending multiple emails at once with nodemailer

查看:144
本文介绍了使用 nodemailer 一次发送多封电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 nodemailer 根据数据库中的条目发送电子邮件.我几乎尝试了我能想到的任何事情(手动创建一个 Promises 数组并在 Promise.all() 的调用中使用它,使用 map 等),但我总是收到相同的错误:UnhandledPromiseRejectionWarning:错误:消息失败:450 SQLITE_ERROR:无法在事务中启动事务.nodemailer-documentation 清楚地指出——当不传递回调 fn 时,transporter.sendMail()-function 被包装为一个承诺.但是,当我像这样手动定义这些承诺的数组时......

I am trying to send an email based on the entries inside a database using nodemailer. I have tried practically anything I can think of (manually creating an array of Promises and using that in a call of Promise.all(), doing that using a map, etc.), but I always keep getting the same error: UnhandledPromiseRejectionWarning: Error: Message failed: 450 SQLITE_ERROR: cannot start a transaction within a transaction. The nodemailer-documentation clearly states that -- when not passing a callback-fn, the transporter.sendMail()-function is wrapped as a promise. However, when I am manually defining an array of those promises like so...

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

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

...即使我还没有使用 Promise.all() 来跑"过那个数组,同样的错误已经发生了;数组中的函数只有在我手动指定时才会运行是一种误解吗?

... the same error already fires even though I have not even 'ran' through that array using Promise.all(); is it a misconception that the functions in the array will only run when I manually specify?

无论如何,这是我完整的数据库代码;我正在使用 sequelize 从数据库中检索数据.我已验证此任务的从数据库端检索数据没有问题.

Anyhow, this is my complete code for the database; I am using sequelize to retrieve the data from the database. I have verified that there are no problems on the retrieving-data-from-the-db-side of this task.

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);
        });
  }
}

任何帮助将不胜感激!提前致谢:)

Any help would be greatly appreciated! Thank you in advance :).

推荐答案

我已经使用 promise.all 测试了 nodeMailer API,但没有发现任何问题 这是我在下面使用的代码,另一件事是您的错误我认为与均衡器或 SQL 相关,导致您得到的错误是由 SQLITE_ERROR 引发的 UnhandledPromiseRejectionWarning:错误:消息失败:450 SQLITE_ERROR:无法在事务中启动事务,还有另外一件事,你在 promise 中编写异步的方式,我认为是错误的,我将在这里与你分享你应该编写函数的方式,但是关于你在运行 promise 之前触发的第一个问题.当您创建一个类似这样的数组 [promise(arg)] 时,您正在调用该函数,因此即使您没有将其放入 promise 中,promise 也会启动并且 node 将处理它.all

I have tested nodeMailer API with promise.all and I did not found any problem with it this is the code I used below, and another thing the error you are getting is I think related to equalizer or SQL cause the error you are getting is being thrown by SQLITE_ERROR UnhandledPromiseRejectionWarning: Error: Message failed: 450 SQLITE_ERROR: cannot start a transaction within a transaction, and another thing the way you are writing async inside the promise all I think is wrong I will share with you here the way you should write the function, but about your fist question about the promises firing before you run promise.all that's all when you create an Array-like this [promise(arg)] you are calling the function at that moment so the promise will start and node will handle it even if you don't put it inside of promise.all

 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: "user@gmail.com",
  to: "test@gmail.com",
  subject: "testing due",
  text: "Dudes, we really need your money.",
};

const mailOptions2 = {
  from: "user@gmail.com",
  to: "test1@gmail.com",
  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));

这篇关于使用 nodemailer 一次发送多封电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆