如何在nodejs中使用nodemailer进行批量数据发送? [英] how to use nodemailer in nodejs for bulk data sending?
本文介绍了如何在nodejs中使用nodemailer进行批量数据发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面有nodemailer代码,它正常工作.我的问题是将单个数据发送到单个电子邮件.id_array有两个电子邮件,no_array有两个单独的数据,我如何将"1"发送到pradag@cybrosys.in,将"2"发送到blockchain@cybrosys.net?
i have nodemailer code below, its fine and working.My problem is to send individual data to individual email. id_array have two emails and no_array have two individual data, How do i send "1" to prayag@cybrosys.in and "2" to blockchain@cybrosys.net?
var id_array = ["prayag@cybrosys.in","blockchain@cybrosys.net"];
var no_array = ["1","2"];
var mailer = require("nodemailer");
// Use Smtp Protocol to send Email
var smtpTransport = mailer.createTransport({
service: "Gmail",
auth: {
user: "mymail@gmail.com",
pass: "mypassword"
}
});
var mail = {
from: "Sachin Murali <blockchain@cybrosys.net>",
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并行运行所有电子邮件
Prepare the parameters for each receiver in a loop and use a promise to run all emails in parallel
var id_array = ["prayag@cybrosys.in","blockchain@cybrosys.net"];
var no_array = ["1","2"];
var mailer = require("nodemailer");
// Use Smtp Protocol to send Email
var smtpTransport = mailer.createTransport({
service: "Gmail",
auth: {
user: "mymail@gmail.com",
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 <blockchain@cybrosys.net>",
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();
});
})
}
这篇关于如何在nodejs中使用nodemailer进行批量数据发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文