title: Google Apps Script: Service invoked too many times for one day: email
body: <p>I'm trying to loop through an spreadsheet, and for each row send an email. Once the email is sent, i'd like to delete that row.</p>
  
  <p>That however is not working.</p>
  
  <p>For some reason, it starts sending emails like crazy, and at some point it reaches the limit and quits.</p>
  
  <p>It actually only deletes one row.</p>
  
  <p>See the code below:</p>
  
  <pre><code>function sendEmails() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheets()[0];
    var startRow = 2;  // First row of data to process
    var maxRows = sheet.getMaxRows();
  
    var range = sheet.getRange(startRow, 1, maxRows, 50)
    var values = range.getValues();
  
   for (var row in values) {
       Logger.log('ID=' + values[row][0]);
       var theID = values[row][0];
       var message = "";
       var sendto = "";
       var emailAddress = values[row][2];
       if (emailAddress=="Autre"){sendto="test@test.com"}
       if (emailAddress=="Autre1"){sendto="test1@test.com"}
       if (emailAddress=="Autre2"){sendto="test2@test.com"}
      message+="\n ID: " + values[row][1];
      message+="\n Project Number: " + values[row][2];
  
      var subject = "Project ID: " + values[row][1];
  
     if (sendto!=''){
       MailApp.sendEmail(sendto, subject, message);
       sheet.deleteRow(row+2)
      }
   }
  </code></pre>
  
  <p>Logic is, if there's a valid email, send the email, delete the row.</p>
  
  <p>But once it executes, it only deletes one row, sends emails like crazy, and get the email max error.</p>
  
  <p>Thoughts?</p>