Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

google apps script - MailApp.sendEmail function runs without errors but no emails are sent

The below script runs without any visible error, but no emails are sent. Any ideas as to why?

function sendEmailLoop() {

  var sheets =   SpreadsheetApp.getActiveSpreadsheet().getSheets();
  sheets.forEach(function(sheet) {
    var range = sheet.getDataRange();

    if (sheet.getName() == "Summary") //Disregard tab named 'Summary' 
    {      
    }
    else {    
      var range = sheet.getDataRange(); //to set the range as array
      var values = range.getDisplayValues(); //to get the value in the array
      var lastRow = range.getLastRow();
      var ss = SpreadsheetApp.getActiveSpreadsheet();  //declare the spreadsheet
      var sheet = ss.getSheetByName("Sheet1");
      var Title = values[0][0];         //[Title] cell A1
      var URL = values[0][1];           //[URL] cell B1
      var i;
      var logContent = '';

      for (i = 3; i < lastRow; i++) {   
        var Name = values[i][0];     //[Name] cell A++
        var Email = values[i][1];        // [Email] cell B++

        Logger.log('to: ' + Email);
        Logger.log('subject: ' + Name + Title + 'Test');
        Logger.log('message: ' + 'This is a test message for the training that can be found at ' + URL);
        /*
        MailApp.sendEmail({
          to: Email,
          subject: Name + Title + 'Test',
          message: 'This is a test message for the training that can be found at ' + URL});
        */
      }; //end for loop - email tab data
    };   // end 'else'
  }); // end function(sheet)       
} // end SendEmailLoop()  

Here is the Stackdriver log of a successful execution (success meaning no errors, but still no emails are sent):

Stackdriver log

The structure of the spreadsheet associated with the script:

spreadsheet associated with the script

Note - an earlier version of this script didn't include the sheets.forEach() method call (ie, to loop through each tab of the spreadsheet) and the emails were sent fine.

Could the lack of emails being sent or received be related to the fact that I have many sheets and this function is looping through them?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
function sendEmailLoop() {
  var exclA=['Summary'];
  var ss=SpreadsheetApp.getActive();
  var sheets=ss.getSheets();
  sheets.forEach(function(sheet) {
    if (exclA.indexOf(sheet.getName())==-1) {      
      var range=sheet.getDataRange();
      var values=range.getDisplayValues(); 
      var lastRow=range.getLastRow();
      var Title=values[0][0];
      var URL=values[0][1];  
      var logContent='';
      for (var i=3; i <values.length; i++) {   
        var Name=values[i][0];
        var Email=values[i][1];
        Logger.log('to: %s
subject: %s %s Test
message: %s This is a test message for the training that can be found at %s',Email,Name,Title,URL);
        /*
        MailApp.sendEmail({
        to: Email,
        subject: Name + Title + 'Test',
        message: 'This is a test message for the training that can be found at ' + URL});
        */
      }
    }
  });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...