Home > Back-end >  Script is not sending an email upon running the code
Script is not sending an email upon running the code

Time:11-09

I am using this script to send an auto email when the date = current date then run but it is not sending the email i do not know why.

Your help will be much appreciated.

https://docs.google.com/spreadsheets/d/1wxDa9D9a1wddqu9Fh4r3uZAX0m1NKNj2H7g_uS141-0/edit#gid=0

function sendEmail() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Mysheet");
  var startRow = 2;  // First row of data to process
  var numRows = sheet.getLastRow()-1;   // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  Logger.log(data)

  for (i in data) {
    var row = data[i];
    var date = new Date();
    var sheetDate = new Date(row[1]);
    Sdate=Utilities.formatDate(date,'GMT 0200','yyyy:MM:dd')
    SsheetDate=Utilities.formatDate(sheetDate,'GMT 0200', 'yyyy:MM:dd')
    Logger.log(Sdate ' =? ' SsheetDate)
    if (Sdate == SsheetDate){
      var emailAddress = row[0];  // First column
      var message = row[2];       // Second column
      var subject = "Sending emails from a Spreadsheet";
//      MailApp.sendEmail(emailAddress, subject, message);
     Logger.log('SENT :' emailAddress '  ' subject '  ' message)
    }    
  }
}

CodePudding user response:

function sendEmail() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Mysheet");
  const startRow = 2;   
  const subject = "Sending emails from a Spreadsheet";
  const vs = sh.getRange(startRow, 1, sh.getLastRow() - startRow   1, sh.getLastColumn()).getValues();
  vs.forEach(row =>  {
    let dt = new Date();
    let dtv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
    let sdt = new Date(row[1]);
    let sdtv = new Date(sdt.getFullYear(),sdt.getMonth(),sdt.getDate()).valueOf();
    if (sdtv == dtv){
      MailApp.sendEmail(row[0], subject, row[2]);
    }    
  });
}
  • Related