Google script, send email with form details trigge

2019-08-31 16:38发布

function sendFormByEmail (e)
{
  var email = "inputemail@address";
  var txt = "";
  for (var field in e.namedValues) {
    txt += field + ' :: ' + e.namedValues[field].toString() + "\n\n";
  }

  MailApp.sendEmail(email, "input email subject", txt);
}

I believe the following worked once (when filled with a working email address). I then copied and pasted exact same script below the above & added a different email in the hopes that form details could be emailed to two separate people. Then the above didn't work. Tried deleting the second copy of it. This didn't resolve problem. Now am getting the following error when I test run it: TypeError: Cannot read property "namedValues" from undefined. (line 5, file "Email submitted form"). I'm utterly perplexed as I'm pretty sure this is the same script as what was working previously. I must disclose this is the first time I've tried scripting anything in over a decade. I am not a programmer - am a novice, never formally studied coding. Would anyone be able to explain what might be happening in layman's terms to me? Or correct the script if appropriate? Thanks in advance for your time.

1条回答
霸刀☆藐视天下
2楼-- · 2019-08-31 17:35

The TypeError: Cannot read property "namedValues" from undefined, happens when you attempt to run the function from within the Apps Script Editor. There is no event (e) parameter being passed to the function.

You will need to ensure there is an On form submit trigger set for your function, and you will have to execute it once in the Apps Script Editor (if you are not prompted when setting the trigger), to accept the use of sending email from your account.

If you are looking to send the mail message to multiple people, just comma separate the list;

e.g.

// broken up for clarity
var email = "inputemail@address";
email += ",";
email += "secondrecipient@address";
// add the email address collected from the form
email += ",";
email += e.namedValues['Username'];

enter image description here

enter image description here

查看更多
登录 后发表回答