Nobody
Nobody

Reputation: 13

Send mail to multiple recipients using nodemailer

how to send mail to multiple recipients which are stored in database(mongodb) using nodemailer? Currently Im sending to single recipient. But im not able to figure out how to send mail to multiple people whose mail ids are stored in mongodb.

If someone knows the answer, please respond. Thank you in advance :)

Upvotes: 1

Views: 6612

Answers (1)

Puneet Singh
Puneet Singh

Reputation: 3543

Use mongodb distinct to get array for all the email_address you want to send the email, and pass that array to nodemailer.

const nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: '[email protected]',
        pass: 'yourpass'
    }
});

let email_arr = db.users.distinct( "email", { /* Any condition you want to put*/ } )

let mailOptions = {
    from: "[email protected]", // 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);
});

Upvotes: 1

Related Questions