Vishu
Vishu

Reputation: 35

Sending email through firebase cloud functions

I have a big query table containing email of the users and i want to send them a newsletter email ( around 500 rows in table) When the function runs it only sends email to 30-35 users after that time out, if i reduce the list to 100 it sends email for the multiple time on one email id Below is my code -: bigQuery.query({

         exports.date = functions.https.onRequest((req, res) => {
     const bigQuery = bigquery({ projectId: 'littleone-150007' });
       var someVar = [];
        var someVar1 =[];

       bigQuery.query({
   query:`Select email from table`
   useLegacySql: false
 }).then(function (rows) {
  setValue(rows);
  });
  function setValue(value) {
  someVar = value;

   //console.log(someVar); // data is printing here

   var someVar = value[0].map(function(o) { return o.email; });

//console.log(someVar); 

var i,datalength;
datalength = someVar.length;

 var emailsubj=`subject`;
 var emailbody=`newsleter body`
 for(i=0;i<datalength;i++){
     //console.log(someVar[i])
      const mailOptions = {
       from: `Madhu from Mylo <[email protected]>`,
       to: someVar[i],
       bcc: `[email protected]`
       }
       mailOptions.subject = emailsubj ;
       mailOptions.html = emailbody;
       mailTransport.sendMail(mailOptions).then(() => {
        console.log('Uninstall mail sent :', someVar[i]);

       });
       }

What i need to change so that only one mail will go to user.??

Upvotes: 0

Views: 510

Answers (1)

joseluismurillorios
joseluismurillorios

Reputation: 1014

If you store your emails like this:

var someVar = ['[email protected]', '[email protected]'];

You can do something like this:

var someVar = ['[email protected]', '[email protected]'];
var sendTo = someVar.join(','); 
var mailOptions = {
  from: '[email protected]', // sender address
  to: sendTo, // list of receivers
  subject: 'My newsletter', // Subject line
  html: '<b>content</b>', // html body
  // plain: 'Some text' // plain text
};

Upvotes: 1

Related Questions