Jordan Pisani
Jordan Pisani

Reputation: 107

Nodemailer multiple recipient mail with MongoDB

I am attempting to email every user on my website after a new post is created. The issue I am having is that only one user will be emailed when this is called. Here is the code to email each user:

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

Both console.log(x); lines return the same single user when they should log 10+ users. If I remove res.redirect("/admin"); at the bottom of the function I receive two errors:

Error: No recipients defined

and

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

Any reply would be greatly appreciated. Thanks in advance.

Edit: I have figured out a solution on my own, I'm posting it here for anyone in the future.

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

Upvotes: 0

Views: 1588

Answers (2)

Jordan Pisani
Jordan Pisani

Reputation: 107

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

Upvotes: 1

khairy Mohamed
khairy Mohamed

Reputation: 176

this is a refactor of your code , i hope it will work:

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

        });

Upvotes: 1

Related Questions