MailApp.sendEmail not firing [duplicate]

2019-02-27 16:18发布

问题:

This question already has an answer here:

  • Not allowed to execute sendEmail() from custom function, but OK in script editor 1 answer

My function below doesn't seem to be firing off any sort of email when the function is called in onEdit(event). I have tried both EmailApp and GmailApp, any help on the matter would be greatly appreciated.

I have also tried to use my personal @gmail.com account and neither will send an email to it again.

function sendAlert() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = ss.getActiveCell().getA1Notation()

  var subject = 'Update to '+sheet.getName();
  var body = sheet.getName() + ' has been updated.  Visit ' + ss.getUrl() + ' to view the changes on cell ' + cell;
  GmailApp.sendEmail('s....@project-la.com', subject, body);
};

回答1:

Is your function sendAlert() called from an onEdit() function?

The simple onEdit function is unable to send mails, as is described in the documentation. Therefore, use an installable onEdit trigger instead. This last one will ask for authorization and then execute as expected.

The triggers are in the ressource tab in the script editor, it looks like this:

Note: If you are re-using an existing onEdit() function, remember to rename it so it will no longer act as a simple trigger function.