Sending email through firebase cloud functions

孤街浪徒 提交于 2021-01-29 10:34:41

问题


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 <noreply@firebase.com>`,
       to: someVar[i],
       bcc: `qa@myloapp.in`
       }
       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.??


回答1:


If you store your emails like this:

var someVar = ['someemail@mail.com', 'another@mail.com'];

You can do something like this:

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


来源:https://stackoverflow.com/questions/50701485/sending-email-through-firebase-cloud-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!