0

I have a Google Form with one field as name and I want to sent a email when user submit the form. So how can I retrieve users names from that text field named name

function conformationEmail(e)
{
  var htmlBodytemplate = HtmlService.createHtmlOutputFromFile("confirm").getContent();
  var responseUserEmail = e.response.getRespondentEmail();
  //var nameofuser = e.namedValues['name'][0];
  var name = "name";
  
  MailApp.sendEmail ({
    to:responseUserEmail,
    subject:nameofuser,
    htmlBody:htmlBodytemplate
  });
  Logger.log("hello");
}

I had used var nameofuser = e.namedValues['name']; but it doesn't seem to be working

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • The questions doesn't include enough details and the included image has a lot of "noise". Apparently your script is bounded to a Google Form, you should mention that, also you should mention how the function is called. – Rubén Jun 27 '21 at 21:53
  • 1
    Does this answer your question? [How to extract response items from a Google Form](https://stackoverflow.com/questions/26253706/how-to-extract-response-items-from-a-google-form) – Rubén Jun 27 '21 at 22:04
  • Anyway, it's very likely that the script is bounded to a Google Form, in such case the form submit event object doesn't include a `namedValues` property, that is the reason of why this isn't working, instead you should use the response object to grab the response to the corresponding form item. On the Mogdads's answer to [this question](https://stackoverflow.com/q/26253706/1595451) there are two possible approaches. – Rubén Jun 27 '21 at 22:06

1 Answers1

0

Your not creating a template with createHtmlOutputFromFile and you don't use getContent() from a template you must first evaluate the template to get html output

Code:

function onSubmitSend(e) {

  let t = HtmlService.createTemplateFromFile("ah2");
  t.data = [e.namedValues['Email Address'][0],e.namedValues['COL1'][1],e.namedValues['COL2'][1]]
  let html = t.evaluate().getContent();
  MailApp.sendEmail({to: e.namedValues['Email Address'][0],subject: "Submission Confirmation",htmlBody: html });
}

html template:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <title> </title>
  </head>
  <body>
   This is to confirm that we have received your form submission.  Your responses were  <?= data[1] ?> and <?= data[2] ?>.
  </body>
</html>

image of form:

enter image description here

Cooper
  • 59,616
  • 6
  • 23
  • 54