title: SendGrid Multiple Emails to Multiple Recipients Node JS from Firebase Firestore
body: <p>I am using SendGrid and Firebase Functions to send multiple emails to multiple recipients. The code that I am using works correctly when sending to a test list of 4 email addresses, but does not work when trying to send to 4,000 email addresses. There is also no error message from SendGrid. </p>
  
  <p>This code also works and returns the list of email addresses printed in the console if the SendGrid block of code is commented out. 
  Do you know what could be going wrong?</p>
  
  <p>Thank you</p>
  
  <pre><code>exports.adminSendGroupMessage = functions.region('europe-west2').https.onCall((data, context) =&gt; {
    const emailHTMLData = emailHTMLData;
  
    var emailDataArray = [];
  
            //Fetch contacts list
            let testContactsRef = db.collection('contacts-list');
            return testContactsRef.get().then(snapshot =&gt; {
                snapshot.forEach(doc =&gt; {
                  // console.log(doc.id, '=&gt;', doc.data());
                  console.log("Fetched contact with ID: " + doc.id);
  
                  //Extract contact data
                  const firstName = doc.data().name || "";
                  const surname = doc.data().surname || "";
                  const emailAddress = doc.data().emailAddress;
  
                  var emailData =  {
                    to: emailAddress,
                    from: 'fromEmail@email.com',
                    subject: messageSubject,
                    text: 'Email for ' + firstName,
                    html: emailHTMLData,
                    customArgs: {
                      ref: 'msg-ref'
                    },
                  }
  
                      //Add new email data to the array
                      emailDataArray.push(emailData);
                });
  
                return Promise.all(emailDataArray).then(results =&gt; {
  
                    //Send emails with all data once contact fetch complete
                  console.log("Success fetching contacts - send emails.");
                  sendGridGroupMessages(emailDataArray);
                  return { success : true, message: "Success sending emails" };
  
              })
  
  });
  
  function sendGridGroupMessages(emailDataArray) {
    console.log('Send emails to group function with data: ' + emailDataArray.length);
  
    var i,j, splitArray,chunk = 998;
    for (i=0,j=emailDataArray.length; i&lt;j; i+=chunk) {
        splitArray = emailDataArray.slice(i,i+chunk);
        // do whatever
        //Send emails
      sgMail.send(splitArray, (error, result) =&gt; {
        if (error) {
          //Do something with the error
          console.log("Error sending group emails: " + error);
          // throw new functions.https.HttpsError('internal', 'There was an error sending the group emails - ' + error.message + ' (' + error.code + ')');
        } else {
          //Celebrate
          console.log("Success sending group emials: " + JSON.stringify(result));
          // return { success : true };
        }
      });
    }
  </code></pre>