如何在nodejs中使用nodemailer进行批量数据发送?

萨钦穆拉里

我在下面有 nodemailer 代码,它很好并且可以工作。我的问题是将个人数据发送到个人电子邮件。id_array 有两封电子邮件,而 no_array 有两个单独的数据,我如何将“1”发送到[email protected] 并将“2”发送到[email protected]

var id_array = ["[email protected]","[email protected]"];
var no_array = ["1","2"];


var mailer = require("nodemailer");

// Use Smtp Protocol to send Email
var smtpTransport = mailer.createTransport({
    service: "Gmail",
    auth: {
        user: "[email protected]",
        pass: "mypassword"
    }
});

var mail = {
    from: "Sachin Murali <[email protected]>",
    to: [id_array],
    subject: "Sachin's Test on new Node.js project",
    text: [no_array]
   // html: "<b>"\"[no_array]\""</b>"
}

smtpTransport.sendMail(mail, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + JSON.stringify(response));
    }

    smtpTransport.close();
  });

我是蝙蝠侠

在循环中为每个接收者准备参数,并使用 promise 并行运行所有电子邮件

  var id_array = ["[email protected]","[email protected]"];
  var no_array = ["1","2"];

  var mailer = require("nodemailer");

    // Use Smtp Protocol to send Email
  var smtpTransport = mailer.createTransport({
    service: "Gmail",
    auth: {
        user: "[email protected]",
        pass: "mypassword"
    }
 });

 let emailPromiseArray = [];

    //prepare the email for each receiver
    for(let i=0;i<id_array.length;i++){
         emailPromiseArray.push(
             sendMail({
                  from: "Sachin Murali <[email protected]>",
                  to: id_array[i],
                  subject: "Sachin's Test on new Node.js project",
                  text:no_array[i]
             })
         )
    }

    //run the promise
    Promise.all(emailPromiseArray).then((result)=>{
        console.log('all mail completed');
    }).catch((error)=>{
        console.log(error);
    })

    function sendMail(mail){

        return new Promise((resolve,reject)=>{
            smtpTransport.sendMail(mail, function(error, response){
        if(error){
            console.log(error);
            reject(error);
        }else{
            console.log("Message sent: " + JSON.stringify(response));
            resolve(response);
        }

        smtpTransport.close();
            });
        })
    }

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Hibernate中使用临时表批量删除记录?

我如何在Django批量创建中使用信号

如何使用Spring Kafka生产者发送批量数据

在没有GMail的nodejs中使用nodemailer进行SMTP

如何在nodejs中使用typescript / flow而不进行编译

如何在php中使用数组对数据进行排序?

如何在Laravel中使用不同的连接进行批量插入?

如何在Python中使用kafkaProducer发送数据?

如何在Az广告应用中使用过滤器进行批量删除

如何在批量pytest执行中使用对象的__subclasses __()方法?

如何在模块中使用BinaryConnection发送和接收数据

如何在Akka中使用元组发送消息?使用F#进行远程处理?

使用libpqxx来批量存储数据,或者如何在libpqxx中使用COPY语句

如何在Mongoid中使用时间字符串批量插入数据

如何在db2游标中使用批量收集

如何在Laravel批量更新中使用变异子?

如何在Mongoskin中使用无序批量插入?

如何在Android中使用Java发送数据报包

如何在Android中使用HttpPost发送多个Json数据

解析数据数组并使用nodemailer发送?

如何在批量Upsert中使用Mongo Pojos

如何在Ajax中使用FormData发送String数据

如何在循环中使用批量更新插入?

如何使用 express.js 框架将数据发送到 HTML 页面以及如何在 NodeJS 服务器中使用 AJAX 进行单页应用程序?

使用 Angular5 前端在 NodeJS 中使用 nodemailer 发送邮件

如何在 Retrofit 中使用 GET 方法发送 json 数据?

如何在 NodeJS 中使用 fetch 获取 API 数据

如何在 VueJS 中使用数据进行条件视图?

如何使用 nodejs 发送批量获取请求?