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.
Link to a sample Google spreadsheet
A | B | C | D | E | F | |
---|---|---|---|---|---|---|
1 | People | Send | Subject | Body | Blind Carbon Copy | |
2 | name01 | ☑ | email01 | subject01 | body01 | common_bcc_address |
3 | name02 | ☑ | email02 | subject02 | body02 | |
4 | name03 | ☐ | email03 | subject03 | body03 |
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})
}
}
}
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});
}
}
}
i > values.length
to i < values.length
to ensure the loop iterates through each row of datass.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)i
from 1
instead of 0
to skip the header row