I am looking at having automated emails trigger on submission of a google form. It is working fine, however I am facing 3 issues with the code below
- Two Emails are getting triggered for every response whereas I only want One Email to go
- Emails are getting sent from "Owner"'s ID instead of the one submitting the response on google form.
- It is supposed to capture the status of all successfully sent email as "EMAIL SENT" in col 7, but, its not doing so.
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow(); // Number of rows to process
var dataRange = sheet.getRange(numRows, 2, 1, sheet.getLastColumn());
// 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[5]; // First column
var message = 'Hello,'+ "\n"
+ "\n"
+ 'We have received an inquiry from your customer in Inbound'+ ".\n"
+ "\n"
+ 'Lead No is' + " - "
+ row[1] + "\n"
+ "\n"
+ 'Kindly arrange a callback'+ "\n"
+ "\n"
+ 'Regards,'+ "\n"
+ 'Team Inbound' + "\n"
+ "\n"
+ 'This is an auto-generated email'; // Second column
var emailSent = row[7]; // Third column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Inbound Inquiry"+ " - " + row[1];
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}