Search code examples
arraysemailgoogle-sheetsgoogle-apps-scriptscripting

Apps script using data from Sheets fails to send from Gmail account without returning any errors


I need to automate sending emails to a list of recipients stored in a Google spreadsheet. I'm new to Apps Script and Javascript but made some simple code based on tutorials that fit what I need.

My sheet includes columns for the email address, subject, and body text.

There is also a single cell, F2, for a common BCC address that is used by all rows.

I have a column with a checkbox to indicate if a row should be included.

When I run the script to send an email to myself, it executes without error, but I never receive the email.

Sample Data

Link to a sample Google spreadsheet

A B C D E F
1 People Send Email Subject Body Blind Carbon Copy
2 name01 email01 subject01 body01 common_bcc_address
3 name02 email02 subject02 body02
4 name03 email03 subject03 body03
  1. The same BCC address, located in F2, is used for all messages, not just the first row.
  2. For each row, an email should only be sent if column B is checked.
  3. The sheet is named People1

Script

function sendEmails() {
  var ss  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('People1');
  var values = ss.getDataRange().getValues();
  var ebcc = ss.getRange(2, 6).getValue()

  for (var i = 0; i > values.length ;i++){
    if (values[i][3] == true) {  
      var theEmail = ss.getRange(i,3).getValue();
      var theSubject = ss.getRange(i,4).getValue();
      var theBody = ss.getRange(i,5).getValue();
      GmailApp.sendEmail(theEmail, theSubject, theBody, {bcc: ebcc})
      }
  }
}

Solution

  • try:

    function sendEmails() {
      var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('People1');
      var values = ss.getDataRange().getValues();
      var ebcc = ss.getRange(2, 6).getValue();
    
      for (var i = 1; i < values.length; i++) {
        if (values[i][3] == true) {  
          var theEmail = values[i][0];
          var theSubject = values[i][1];
          var theBody = values[i][2];
          GmailApp.sendEmail(theEmail, theSubject, theBody, {bcc: ebcc});
        }
      }
    }
    
    • changed i > values.length to i < values.length to ensure the loop iterates through each row of data
    • changed ss.getRange(i, 3).getValue() to values[i][0], values[i][1], and values[i][2] respectively, ensuring data is correctly accessed from the values array (assuming email, subject, and body are in the first, second, and third columns respectively)
    • adjusted to start i from 1 instead of 0 to skip the header row