简体   繁体   中英

send email to Undisclosed recipients nodemailer

so I have this nodemailer function set up, where I query my db for the list of users who signed up, and send them an email... but the problem is, I have to manually put which user I want to send the email to, and then bcc everyone else so they don't see the other users. what I want is for each user to be send the same email, all at once, but they shouldn't be able to see the other receipts (like when you receive an email from a store or something)... here's what I have:

  app.post('/sendBatchEmail', (req, res) => {
    var emails = [];
    var emailSubject = req.body.emailSubject;
    var emailMessage = req.body.emailMessage;

    //perform db2 send
    var sendEmail = "select * from testEmails"
    ibmdb.open(ibmdbconnMaster, function (err, conn) {
      if (err) return console.log(err);
      conn.query(sendEmail, function (err, rows) {
        if (err) {
          console.log(err);
        }
        for (var i = 0; i < rows.length; i++) {
          emails.push(rows[i].EMAIL)
        }
       
        //send email
        async function main() {
        

          let transporter = nodemailer.createTransport({
            host: "smtphm.sympatico.ca",
            port: 587,
            secure: false, // true for 465, false for other ports
            auth: {
              user: "xxx@bell.net",
              pass: "xxx",
            },
          });
      
          // send mail with defined transport object
          let sendBatch = await transporter.sendMail({
            from: "xxx@bell.net", // sender address
            to: "reciever@gmail.com",
            bcc: emails, // list of receivers
            subject: emailSubject, // Subject line
            text: emailMessage, // plain text body
          });
          
      
          console.log("Message sent: %s", sendBatch.messageId);
          // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
      
         
        }
      
        main().catch(console.error);
        res.redirect("/");

        conn.close(function () {
          console.log("closed the function app.get(/sendBatchEmail)");
        });
      });
    });

  })

any idea on how to do this?

answered my own question:

just put this:

// send mail with defined transport object
      let sendBatch = await transporter.sendMail({
        from: "xx@bell.net", // sender address
        to: "Undisclosed Recipients",
        bcc: emails, // list of receivers
        subject: emailSubject, // Subject line
        text: emailMessage, // plain text body
      });

works like a charm, doesn't even have to field in the email, only shows up like this:

from: xx@bell.net
Subject: test email

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM