在创建新帖子后,我尝试通过电子邮件向我网站上的每个用户发送电子邮件。我遇到的问题是,只有一个用户会在收到电子邮件时被发送电子邮件。这是向每个用户发送电子邮件的代码:
User.find({}, function(err, allUsers){
if(err){
req.flash("error", "We seem to be experiencing issues. Please try again later.");
console.log(err);
return res.redirect("/");
}
allUsers.forEach(function(users){
console.log(users);
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'email@email.com',
pass: 'password'
}
});
var mailOptions = {
to: users.email,
from: 'email@email.com',
subject: 'A new post has been created!',
text: 'You are receiving this because you have requested to recieve notifications when a new post is created.\n\n' +
'If you would like to view this post please login to website.com\n'
};
smtpTransport.sendMail(mailOptions, function(err) {
if(err){
req.flash("error", "We seem to be experiencing issues. Please try again later.");
console.log(err);
return res.redirect("/");
}
});
console.log(users.email);
res.redirect("/admin");
});
});
当console.log(x);
行都应记录10个以上的用户时,它们返回相同的单个用户。如果删除函数底部的res.redirect("/admin");
,则会收到两个错误:
Error: No recipients defined
和
Error: Can't set headers after they are sent.
任何答复将不胜感激。预先感谢。
编辑:我已经找到了一个解决方案,我将其发布给这里的任何人 未来。
User.find({}, function(err, allUsers){
if(err){
console.log(err);
}
var mailList = [];
allUsers.forEach(function(users){
mailList.push(users.email);
return mailList;
});
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'email@email.com',
pass: "password"
}
});
var mailOptions = {
to: [],
bcc: mailList,
from: 'email@email.com',
subject: 'Subject',
text: 'blah blah blah\n\n' +
'blah blah blah\n\n' +
'blah blah blah\n'
};
smtpTransport.sendMail(mailOptions, function(err) {
if(err){
console.log(err);
req.flash("error", "We seem to be experiencing issues. Please try again later.");
res.redirect("/");
}
console.log('mail sent to ' + mailList);
});
});
res.redirect("/admin");
答案 0 :(得分:1)
这是代码的重构,我希望它能起作用:
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'email@email.com',
pass: 'password'
}
});
function sedEmail(toEmail){
var mailOptions = {
to: toEmail,
from: 'email@email.com',
subject: 'A new post has been created!',
text: 'You are receiving this because you have requested to recieve notifications when a new post is created.\n\n' +
'If you would like to view this post please login to website.com\n'
};
smtpTransport.sendMail(mailOptions, function(err) {
if(err){
req.flash("error", "We seem to be experiencing issues. Please try again later.");
console.log(err);
}
});
}
User.find({}, function(err, allUsers){
if(err){
req.flash("error", "We seem to be experiencing issues. Please try again later.");
console.log(err);
return res.redirect("/");
}
for(var i = 0; i<allUsers.length; i++){
sedEmail(allUsers[i].email);
}
res.redirect("/admin");
});
答案 1 :(得分:0)
User.find({}, function(err, allUsers){
if(err){
console.log(err);
}
var mailList = [];
allUsers.forEach(function(users){
mailList.push(users.email);
return mailList;
});
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'email@email.com',
pass: "password"
}
});
var mailOptions = {
to: [],
bcc: mailList,
from: 'email@email.com',
subject: 'Subject',
text: 'blah blah blah\n\n' +
'blah blah blah\n\n' +
'blah blah blah\n'
};
smtpTransport.sendMail(mailOptions, function(err) {
if(err){
console.log(err);
req.flash("error", "We seem to be experiencing issues. Please try again later.");
res.redirect("/");
}
console.log('mail sent to ' + mailList);
});
});
res.redirect("/admin");