Jeff Abrahams
Jeff Abrahams

Reputation: 61

How to have Google Forms send automated email with new responses

I have a Google Form which I would like to automatically email someone when a new response is submitted. So far, I have just a simple HTML page with text in the body, however I would like the email content to include the form data as well.

Currently, this is what I have written:

function sendEmail(e) {


  //response
  var html = HtmlService.createTemplateFromFile("email.html");
  var htmlText = html.evaluate().getContent();

  var emailTo = "[email protected]"
  var subject = "New SAP Role Request"
  var textBody = "This email requires HTML support. Please make sure you open it with an email client that supports HTML"
  var options = {htmlBody: htmlText};


  GmailApp.sendEmail(emailTo, subject, textBody, options);

This came from following this basic YouTube tutorial.

Is there more Google Apps Script that I can add to accomplish this? Can I do this from Forms or must I do it from within Sheets?

Upvotes: 0

Views: 115

Answers (1)

CMB
CMB

Reputation: 5163

The e.response object also contains the form data, which can be accessed by using e.response.getItemResponses().

Then to get the question, use getItem().getTitle(). To get the answer, use getResponse().

If you do not need the HTML response, then you can append the questions and answers to the textBody to display them on the email. Otherwise, you would have to add a script in your email.html using HTML scripts or google.script.run.

References:

Event Objects | onFormSubmit(e)

Class FormResponse

Upvotes: 1

Related Questions