0

I have a simple script that executes once form submit. It is a simple test script applied html for email.

function sendEmail(e) {
  
  //response
  //getRespondentEmail()
  var html = HtmlService.createTemplateFromFile("email.html");
  var htmlText = html.getCode();
  
  var emailTo = e.response.getRespondentEmail();
  var subject = "Thanks for participating";
  var textBody = "This email requries HTML support, please maek sure you open with a client taht support.";
  var options = { htmlBody: htmlText };
  
  
  if(emailTo !== undefined){
    GmailApp.sendEmail(emailTo,subject,textBody,options);
  //(recipient, subject, body, options)
  }
}

But it always noticed TypeError: Cannot read property 'response' of undefined.

TypeError: Cannot read property 'response' of undefined (line 8, file "Code")

either the respose from my host email said 'Failed to send email: no recipient' enter image description here

I do not know why it does not work, but I see someone successfully applied through above google app script.

UD.Cole
  • 45
  • 8
  • Where is your script? For example, which is your script the container-bound script of Spreadsheet or From? – Tanaike Nov 18 '21 at 23:59
  • @Tanaike Hey I've been enjoying studying some of you Array.reduce code. Some of it is pretty challenging to figure out. Thanks – Cooper Nov 19 '21 at 01:18
  • @Tanaike Hey, sorry, I should make it clear, it is Google Form based script. – UD.Cole Nov 19 '21 at 02:46
  • @Cooper If you have any questions, feel free to tell me them. – Tanaike Nov 19 '21 at 05:59
  • 1
    @UD.Cole Thank you for replying. When you use your script with the container-bound script of Google Form, when you run the function `sendEmail` by the OnSubmit trigger, I think that there is no error like `Cannot read property 'response' of undefined`. So, can I ask you about the method for executing your function? – Tanaike Nov 19 '21 at 06:00

1 Answers1

1

'e' undefined:

TypeError: Cannot read property 'response' of undefined

This error means the parameter e is undefined. Which means this function is not executing via trigger.

When functions are called by triggers, the function is passed an event object as argument. But in other contexts (for example, when executing this manually, from the editor) this argument is not passed.

Therefore, this function is supposed to get executed automatically when users submit the form. Don't try to execute it manually from the editor, if you are using e inside the function.

No recipient:

Failed to send email: no recipient

This means emailTo is not defined, which means getRespondentEmail() is not returning anything.

This could happen for two reasons:

  • Either a form response was created by the script, but not submitted. Since this is getting executed via a form submission trigger, though, it's not your case.
  • The other reason, which is most likely what's going here, is that you didn't enable the option to collect the email addresses. You can do that programmatically, by calling Form.setCollectEmail(collect), or through the UI, by following the guide in Collect respondents' email addresses.

Related:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27