如何使用nodemailer将邮件发送到存储在数据库(mongodb)中的多个收件人? 目前我发送给单个收件人。但我无法弄清楚如何将邮件发送给多个邮件ID存储在mongodb中的人。
如果有人知道答案,请回复。 提前谢谢你:)
答案 0 :(得分:0)
使用mongodb disting为您要发送电子邮件的所有email_address获取数组,并将该数组传递给nodemailer。
const nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'gmail.user@gmail.com',
pass: 'yourpass'
}
});
let email_arr = db.users.distinct( "email", { /* Any condition you want to put*/ } )
let mailOptions = {
from: "test@example.com", // sender address
subject: "Hello ✔", // Subject line
text: "Hello This is an auto generated Email for testing from node please ignore it", // plaintext body
to: email_arr
}
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});