Home > Back-end >  Google Apps Script not sending email
Google Apps Script not sending email

Time:07-08

We are trying to get e-mail notifications in cases there would be some dramatic changes in our revenue data. Could anyone please indicate possible errors why it wouldn't send e-mail?

function sendEmail() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet1"); 
  const data = sh.getRange("B2:L80"   sh.getLastRow()).getValues();
  data.forEach(r=>{
     let overdueValue = r[9];  
     if (overdueValue === "TRUE"){
         let name = r[10];
         let message = "Reach out to "   name;
         let subject = "Reach out to this person.";
         //MailApp.sendEmail("[email protected]", subject, message);
         GmailApp.sendEmail("[email protected]", subject, message);  
     }
  });
}

CodePudding user response:

Try changing if (overdueValue === "TRUE") to if (overdueValue === true)

Updated Code:

function sendEmail() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet1"); 
  const data = sh.getRange("B2:L80"   sh.getLastRow()).getValues();
  data.forEach(r=>{
     let overdueValue = r[9];  
     if (overdueValue === true){
         let name = r[10];
         let message = "Reach out to "   name;
         let subject = "Reach out to this person.";
         //MailApp.sendEmail("[email protected]", subject, message);
         GmailApp.sendEmail("[email protected]", subject, message);  
     }
  });
}

Notes:

  • overdueValue is a boolean so trying to check if the value is a string will always end up being false.
  • You could also convert overdueValue to a string which could then be checked as you previously did. You'd just need to change the capitalization of TRUE: if (overdueValue.toString() === "true")
  • Related