dr_nick
dr_nick

Reputation: 11

MailApp sendEmail in google sheets

been using MailApp script to automatically send emails from within a google sheet for several years. developed my script from an example i found online and reverse engineered it until i could make it work (i am not a coder!). worked brilliantly for months/years...

but recently it is no longer functioning. i get a daily email summary of errors which states that EMAIL_SENT is not defined, but when running the script in debug mode it times out after the logger.log(user) - so does appear to hang on the EMAIL_SENT variable. As i say, this has been running brilliantly for many months, but appears to have stopped around july 22nd. i have included the script below in case it is something simple:

function autoemailES() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ESautoemail");
  //var lastRow = sheet.getLastRow();
   var startRow = 2; // First row of data to process
  var numRows = sheet.getLastRow() -1; // Number of rows to process
  var EMAIL_SENT = 'EMAIL_SENT';
  // Fetch the range of cells A2:G
  var dataRange = sheet.getRange(startRow, 1, numRows, 30);
   // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailSent = row[7]; // 8th column
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
    var superMail = row[2]; // 3rd column
    var responseNo = row[3]; // 4th column
    var user = row[0]; // 1st column
    var firstName = row[1]; // 2nd column
    var pdfLink = row[5]; // 6th column
    var pdfID = row[6]; // 7th column
    Logger.log(responseNo) //these logger lines just display the values for each of the variables in debugging
    Logger.log(superMail)
    Logger.log(user)
    Logger.log(pdfLink)
    Logger.log(pdfID)
    var file = DriveApp.getFileById(pdfID);
    var subject = 'IBTPHEM MSF results for ' + user;
    var message = 'Dear Educational Supervisor,' + "\n" + 
    'This is an automated notification that your trainee, ' + firstName + ', has now received a quorate number of MSF responses.'+"\n"+
    'The anonymised summary of responses is attached in PDF format, or can be downloaded from the link below.'+"\n" + pdfLink +"\n"+
    'Further submissions will be added automatically (the trainee is notified for each), but the pdf will need downloading again'+"\n"+
    'Please feel free to share this with your trainee however you see fit - we are now releasing the results via the ES in case there is any feedback that may require debriefing or handling more delicately.'+"\n"+
    'Please do let me know of any problems or concerns?'+"\n"+
    'Kind Regards,' + "\n" + 
    'IBTPHEM MSF'
    MailApp.sendEmail(superMail,subject, message,{
    name: 'IBTPHEM MSF',
    replyTo: '[email protected]',
    attachments: [file.getAs(MimeType.PDF)]});
    sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT);
    SpreadsheetApp.flush();
    }
  }
}

I would be really grateful for any help to identify my problem. thanks! dr_nick (i cannot easily share the sheet in its entirity as it contains a fair bit of senstive/confidential information - though I suppose I could make an anonymised version if this is necessary/helpful?)

Upvotes: 1

Views: 132

Answers (1)

Cooper
Cooper

Reputation: 64110

This is the same script with a try catch block. It will only log things if you have an error.

function autoemailES() {
  const ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("ESautoemail");
  var startRow = 2;
  var EMAIL_SENT = 'EMAIL_SENT';
  var dataRange = sheet.getRange(startRow, 1, sheet.getLastRow() - startRow + 1, 30);
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    try{
    var row = data[i];
    var emailSent = row[7];
    if (emailSent !== EMAIL_SENT) {
      var superMail = row[2];
      var responseNo = row[3];
      var user = row[0];
      var firstName = row[1];
      var pdfLink = row[5];
      var pdfID = row[6];
      var file = DriveApp.getFileById(pdfID);
      var subject = 'IBTPHEM MSF results for ' + user;
      var message = 'Dear Educational Supervisor,' + "\n" +
        'This is an automated notification that your trainee, ' + firstName + ', has now received a quorate number of MSF responses.' + "\n" +
        'The anonymised summary of responses is attached in PDF format, or can be downloaded from the link below.' + "\n" + pdfLink + "\n" +
        'Further submissions will be added automatically (the trainee is notified for each), but the pdf will need downloading again' + "\n" +
        'Please feel free to share this with your trainee however you see fit - we are now releasing the results via the ES in case there is any feedback that may require debriefing or handling more delicately.' + "\n" +
        'Please do let me know of any problems or concerns?' + "\n" +
        'Kind Regards,' + "\n" +
        'IBTPHEM MSF'
      MailApp.sendEmail(superMail, subject, message, {
        name: 'IBTPHEM MSF',
        replyTo: '[email protected]',
        attachments: [file.getAs(MimeType.PDF)]
      });
      sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT);
      SpreadsheetApp.flush();
    }
    }
    catch(e){
      Logger.log('responseNo: %s\nsuperMail: %s\nuser: %s\npdfLink:%s\npdfID: %s',responseNo,superMail,user,pdfLink,pdfID);
      Logger.log(e);
    }
  }
}

Try setting up another spreadsheet and adjust the data so that it doesn't send data to your clients and see if it will generate any errors. Also if you could share a copy of the new spreadsheet as a markdown table that would enable us to test it a little. But be careful about sharing private information

Upvotes: 1

Related Questions