MongoDB 的 Nodemailer 多收件人邮件

Nodemailer multiple recipient mail with MongoDB

我正在尝试在创建新 post 后向我网站上的每个用户发送电子邮件。我遇到的问题是调用时只有一个用户会收到电子邮件。这是向每个用户发送电子邮件的代码:

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); 行 return 同一个用户,他们应该记录 10 个以上的用户。如果我删除函数底部的 res.redirect("/admin");,我会收到两个错误:

Error: No recipients defined

Error: Can't set headers after they are sent.

任何答复将不胜感激。提前致谢。

编辑:我自己想出了一个解决方案,我post在这里为任何人 未来。

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");

这是对您的代码的重构,我希望它能起作用:

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");

        });
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");