Reputation: 1
I am using this script to send reminder emails. It works when I run it manually, but when I try to run it on a trigger, it fails.
var EMAIL_SENT = 'EMAIL_SENT';
function sendEmail() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Job Checkup Tool');
var startRow = 4; // First row of data to process
var numRows = 13; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 3);
// 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 emailAddress = row[0]; // First column
var message = row[1]; // Second column
var emailSent = row[2]; // Third column
if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
var subject = 'Job Checkup Reminder';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
The problem appears to be a result of the script finding no email address in column 1.
When I run it manually, though it has an error, it will successfully send the emails even if there isn't an email value in column 1.
When the trigger runs it, it shows the same error
Exception: Failed to send email: no recipient
but doesn't send any emails.
If I adjust the range to focus only on rows with an email address in column 1, it runs great manually and via the trigger.
Is there a good way to request the script to send the email IF there is an email present within column 1 but then ignore the other rows that don't have an email yet?
Upvotes: 0
Views: 59
Reputation: 38469
Replace
if (emailSent !== EMAIL_SENT) {
by
if (emailAddress !== '' && emailSent !== EMAIL_SENT) {
Upvotes: 2