How to add confirmation emails to google forms

Have you ever used google forms to get some input from your users? It is really easy to set up a google form, add the required input fields and share the form with your users. Here’s an example of a google form.

After the users fill in the form, they will see a short confirmation message on the screen and their responses will be stored. You might also want your users to get a confirmation email after filling out the form. This is not the default behavior of google forms though so you will have to add a small script to do so.

If you fill in and submit the example form you will receive such a confirmation email and see what we are going to set up.

Here’s what you have to do:

  • from the form edit screen choose the tools menu option
  • pick script manager and click the new button
  • you will be redirected to a new page for adding the script
  • pick form in the popup
  • add a name for your script file
  • clear the code if there is any example code in there
  • add the following code:
function onFormSubmit(e) {
     var response = e.response;
     var itemResponses = response.getItemResponses();
     var name = itemResponses[0].getResponse();
     var emailAddress = itemResponses[1].getResponse();
     var comments = itemResponses[2].getResponse();
     var subject = "Form confirmation";
     var emailBody = "Hello " + name + "!" +
     "\n\n This is a confirmation email!" +
     "\n\n Your details: " +
     "\n\t Name: " + name +
     "\n\t Email: " + emailAddress +
     "\n\t Comments " + comments +
     "\n\n Thanks for filling out the form!";
     MailApp.sendEmail(emailAddress, subject, emailBody);
} 

The code is pretty straightforward: it received the user response as a parameter, it gets an array with the individual answers for each question (itemResponses), it gets the response for each question, sets the subject and the body of the email to be sent and sends the email to the user.

There’s is only one more step to go, we need to set a trigger for this function:

  • go to resources from the menu
  • pick current projects triggers
  • you will notice there are no triggers set up. click on click here to add one now
  • you need to select the method to run (onFormSubmit) and the trigger event from form and on form submit
  • you can also set the notification options – the default is to receive a daily email with the errors that come up, but for testing purposes you could set it to send error messages immediately
  • click save and authorize the app as requested

And that’s it! you now have the needed script. You need to go back to the forms edit page, from the scripts manager popup you need to refresh the scripts list and pick the newly added onFormSubmit script.

And you’re done! You’re ready to test out your form and send it out to your users!

Let me know if you have any questions! :)

11 thoughts on “How to add confirmation emails to google forms

  1. Hi! Thanks for this. I get the error message: TypeError: Cannot read properly “response” from undefined. What does that mean?

    • Hey :) When are you getting this error? Double check that the trigger for this method was set up properly as described ;)

  2. I set up everything as you said (i think). I did it again now, and I get the same error message. I pops up when I try to a run the onFormSubmit script from the script manager.

  3. Hi, I am trying out this code as well, but is met with the following error. Read through these threads, but still can’t seem to fix it.

    I need help!

    function onFormSubmit(e) {
    var response = e.response;
    var itemResponses = response.getItemResponses();
    var timestamp = itemResponses[0].getResponse();

    This is the error I get

    TypeError: Cannot call method “getItemResponses” of undefined. (line 3, file “Code”)

    I believe it has something to do with this line
    var itemResponses = response.getItemResponses();

    What is wrong? :(

    • Hey!

      Please double check that you set the correct trigger for this action :) There might be something wrong set there.

      Also, how did you test this? Did you fill in the form or did you run it from the script managed? It’s easier to test by filling in the form and submitting it.

  4. Hi Aniri,

    I tested it by filling in the form and submitting it.

    Current project’s triggers as such

    Run onFormSubmit – Events “from spreadsheet, on form submit”

    That’s correct right?

    • Hey

      I think the problem might be the fact that you are using a spreadsheet instead of a form. Can you check with a form?

      I have the following:
      Run onFormSubmit – Events “from form, on form submit” -

  5. Hi there, thanks very much for a great little script. This is the first time I’ve ever done anything with a Google script though, so am having a little trouble debugging this problem:

    The script successfully sends e-mails, but only if all fields were filled out. e.g. let’s say that field 2 was optional. That means the hard-coded indices for the fields after that no longer correspond to their expected contents. e.g. if the e-mail address should have been itemResponses[3], if the second item wasn’t entered, then the e-mail address is now itemResponses[2]. This means that the script gets the wrong values, and can end up trying to send to invalid e-mail addresses and not correctly populating the body of the message. And you can get an out-of-bounds problem when trying to access the last value(s).

    Any hints on how to deal with this? I’m surprised it happens, as I assumed that blank entries would get null values, rather than changing the number of response values in the array.

    • Hey Michael!

      That’s an interesting issue! I’ve never ran into it though since I only used forms with mandatory questions.

      But I guess you could go through the array and check which answer corresponds to which question. The ItemResponse class also has a method getItem() that can be used to retrieve the data about the question the answer belongs to. You can check the documentation here: https://developers.google.com/apps-script/reference/forms/item-response

      I hope this helps!

      • Hi Irina, thanks so much for your considered reply and the link to the correct documentation: I wouldn’t have known where to start otherwise.

        Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>