0
votes

I use the following code to send reminder emails to all the emails that are in a certain cell in column 7. But, it gives an error that "Cannot find SendEmail function". Would anyone please help me?

function myfunction() {
var sheet  = SpreadsheetApp.getActiveSheet();
  
   // figure out what the last row is
  var lastRow1 = sheet.getLastRow();
 
  // the rows are indexed starting at 1, and the first row
  // is the headers, so start with row 5
  var startRow1 = 5;
 
  // grab column 6 (the 'days left' column) 
  var range = sheet.getRange(5,6,lastRow1-startRow1+1,1 );
  var numRows = range.getNumRows();
  var days_left_values = range.getValues();
   
  // Now, grab the reminder name column
  range = sheet.getRange(5, 3, lastRow1-startRow1+1, 1);
  var reminder_info_values = range.getValues();
  
  range = sheet.getRange(5, 7, lastRow1-startRow1+1, 1);
    var emails_info_values = range.getValues();
   
  var warning_count = 0;
  var msg = "";
   
  // Loop over the days left values
  for (var i = 0; i <= numRows - 1; i++) {
    var days_left = days_left_values[i][0];
    if(days_left == 1) {
      // if it's exactly 1, do something with the data.
      var reminder_name = reminder_info_values[i][0];
       
      msg = msg + "Reminder: "+reminder_name+" is due in "+days_left+" days.\n";
      warning_count++;
    }
    var emails= emails_info_values [i][0];
      if(warning_count) {
    MailApp.sendEmail("emails", "msg");
  }
    
  }
  
}
2
I don't think you have enough parameters on the sendEmail method - Cooper
This looks wrong if(days_left =1= 0) { - Cooper
Are you sure these are strings. MailApp.sendEmail("emails", "msg"); I think they should be variables. - Cooper
@Cooper that was a typo. I corrected it. Thanks. I am not sure how to write the SendEmail function. - user4172070
Here's a link - Cooper

2 Answers

0
votes

As Cooper mentioned in his comment The MailApp.sendEmail() function has inputs of either a mail object or if you wanted to send a simple string text as: MailApp.sendEmail(recipient, subject, body) where recipient is a string, subject is a string, and body is a string.

The error that your function returns is stating exactly:

Cannot find method sendEmail(string,string).

Which is meaning to say that it can't find the sendEmail function that handles two string inputs, when it's actually looking for sendEmail(string,string,string)

0
votes

Question

Why my sendEmail is not working?

Answer

Your sendEmail syntax is wrong, you are missing subject


  1. Check the correct syntax for sendEmail()
function sendEmail(){
  MailApp.sendEmail(recipient, subject, body)
}

Example:

function sendEmail(){
  MailApp.sendEmail("[email protected]", "testing sendEmail via Apps Script", "It's working")
}
  1. Adapting to your code
MailApp.sendEmail(emails, "Email Subject", msg);

Final version

function myfunction() {

  var sheet = SpreadsheetApp.getActiveSheet();

  // figure out what the last row is
  var lastRow1 = sheet.getLastRow();

  // the rows are indexed starting at 1, and the first row
  // is the headers, so start with row 5
  var startRow1 = 5;

  // grab column 6 (the 'days left' column) 
  var range = sheet.getRange(5, 6, lastRow1 - startRow1 + 1, 1 );
  var numRows = range.getNumRows();
  var days_left_values = range.getValues();

  // Now, grab the reminder name column
  range = sheet.getRange(5, 3, lastRow1 - startRow1 + 1, 1);
  var reminder_info_values = range.getValues();

  range = sheet.getRange(5, 7, lastRow1 - startRow1 + 1, 1);
  var emails_info_values = range.getValues();

  var warning_count = 0;
  var msg = "";

  // Loop over the days left values
  for (var i = 0; i <= numRows - 1; i++) {

    var days_left = days_left_values[i][0];

    if(days_left == 1) {

      // if it's exactly 1, do something with the data.
      var reminder_name = reminder_info_values[i][0];

      msg = msg + "Reminder: " + reminder_name + " is due in " + days_left + " days.\n";
      warning_count++;

    }

    var emails = emails_info_values [i][0];

    if(warning_count) {
      MailApp.sendEmail(emails, "Email Subject", msg);
    }

  }

}

Reference: