如何在nodejs中使用nodemailer进行批量数据发送?
how to use nodemailer in nodejs for bulk data sending?
我有下面的 nodemailer 代码,它很好,working.My 问题是将个人数据发送到个人电子邮件。 id_array 有两封电子邮件,no_array 有两个个人数据,我如何将“1”发送到 prayag@cybrosys.in 并将“2”发送到 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();
});
在一个循环中为每个接收者准备参数,并使用承诺 运行 所有电子邮件并行
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();
});
})
}
我有下面的 nodemailer 代码,它很好,working.My 问题是将个人数据发送到个人电子邮件。 id_array 有两封电子邮件,no_array 有两个个人数据,我如何将“1”发送到 prayag@cybrosys.in 并将“2”发送到 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();
});
在一个循环中为每个接收者准备参数,并使用承诺 运行 所有电子邮件并行
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();
});
})
}