iyern_99
iyern_99

Reputation: 13

Google Sheet - Auto Email

I am looking at having automated emails trigger on submission of a google form. It is working fine, however I am facing 1 major issue with the code below

Emails are getting sent from My Gmail instead of the one submitting the response on google form. Any help will be much appreciated

  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();
    }
  }
}```


Upvotes: 0

Views: 111

Answers (1)

Дмитро Булах
Дмитро Булах

Reputation: 3847

You can't send emails on behalf an account other than authorized the script to run. Meaning, if you have set up the script under your account, the emails would be sent from only your address.

You'll have to explicitly request authorization from another account to send an email, and supposedly you can't do that from the Google Form.

The workaround might be using the replyTo parameter with a submission email value with your MailApp.sendEmail():

Upvotes: 2

Related Questions