Sending Confirmation Emails with a Google Docs Form

I love Google Apps. Gmail, Google Docs, Google Voice–all brilliant, indispensable products in my opinion. Being able to create forms in Google Docs for quick polls and surveys is also very handy, but the extremely limited feature set has been frustrating to me. One surprising lack is that of being able to send a confirmation email to the user. Granted, not all forms necessarily even have a field for the user to enter their email.

With a quick script, you can rectify this and customize an email that will be sent to the user. Don’t be intimidated by the code–if you can handle writing raw HTML, you will be able to modify this script to suit your needs.

However, if you’ve never used a Google Form before, you probably want to play around with that and get comfortable with that first. The rest of this assumes that you have created a form and are viewing the attached spreadsheet.

Update: make SURE you are looking at the spreadsheet, as shown in the screenshot below. If you are viewing the form, you are in the wrong place. The script needs to be attached to the spreadsheet. Thanks to t1dude for pointing this out!

The first step, in Google Docs, is to click on “Tools” and then “Script Editor.” A new window will open, with an empty function called “myFunction.” Overwrite the contents of this window with the code you want to use. Below I’ve added a complete example that you can rework to your needs.

See the first section, with “e.values”? Those refer to the form fields. “e.values[0]” will always be the timestamp. “e.values[1]” will be the first field in your form, and so on.

Feel free to change the variable names. If the first form field isn’t the respondent’s name, but is actually their favorite color instead, you can certainly change it from yourName to faveColor. Just make sure to change all occurrences of the identifier.

If you have fewer form fields, remove the lines you don’t need. If you have more, copy and paste the existing lines, making sure to increment the numbers… so “e.values[27]” would be the 27th form field, and so on. You do not need to reference every form field in your script–just have lines for the ones you want to include in the email.

Next, we put together some strings (a string of words) to be put into the email. If the text is in quotation marks, it will appear just like you entered it. If the text is not in quotation marks, then it refers to one of the variables we just set up. Plus signs concatenate those strings, and every line needs to end in a semicolon.

“\n” is special. In quotation marks, this refers to a newline character… a line break. Add that whenever you would press “enter” in the email if you were writing it by hand.

Modify the “subject” line as necessary. Yours might be simpler or more complex.

Modify the body section as necessary. This was broken up into multiple lines just for readability. It could all be on one line. Just make sure to have plus signs to connects different strings of text, and only to have one semicolon (at the end).

The “optAdvancedArgs” section is special. It refers to special settings (anything beyond the basic to, subject and body settings) for an email. You don’t have to use it. You can simply remove that, changing that line to:
MailApp.sendEmail(toAddress, subject,
emailBody);

Want to set other advanced arguments, like a cc line, an HTML body for the email and more? View the MailApp Script Reference and scroll to the bottom of the page for a list of what you can set and examples of how to use them.

A few notes on HTML emails:

If you provide an HTML version of the email, also provide plain text for those email clients that can’t see the HTML version.

Emails don’t work like web pages. Writing HTML for an email is like making a website in the 90’s. Read the linked article or do some Googling to find out what tags you should and shouldn’t use.

Email clients tend to be narrow. Don’t count on having much horizontal space.

When you are writing a Google Docs script and need to refer to a quote character, such as when writing attributes in an HTML tag, use a backslash before the single or double quote, or else Google Docs will think you’re ending the string. Check the example for when a font color is added to “your name”

Modify the script, and then save it. However, there is one more important step to take to get this to work!
In the script editing window (not the normal Google Docs window), click on the “Resources” menu. Click “Current Script’s Triggers…” and then “Add a new trigger.”
The first field should be “onFormSubmit,” the second “From Spreadsheet” and the third “On form submit.” Change these if necessary and add the trigger. You will need to give your approval for Google to allow the form to send emails.

If you do not set the trigger, the script will never run. The trigger is necessary to run the code whenever a form is submitted.

There! Now try submitting a form and make sure the email looks the way you want.

229 Comments

Marcin - November 2, 2013

Thank you! Very helpful and easy solution.

Kalyan Sharma - November 30, 2013

Hi,

This is a great post. Thanks.

While the MailApp.sendEmail works great as described, I am finding that its advanced arguments do not allow for the use of an authorized alias as a from address, instead of the gmail address of the user owning the script.

The GMailApp.send email method allows for this, but when I try to use the GmailApp instead of the MailApp class, I get the error – ReferenceError: “GMailApp” is not defined.

Can you suggest what I can do here?

Thanks a ton!

Kiki - December 4, 2013

I have set up triggers to send a confirmation email onForm submit…they are their, I save them, I do a test and they work, but then they stop working, if I go back in my triggers are either greyed out or gone. I add them again, save everything, exit, reopen immediately, they are still good. I go to my next form do the same thing, but when I return to the previous form my triggers are off again??? Why is this happening?

Thanks for your help!
Kristen

Kiki - December 4, 2013

I have set up triggers to send a confirmation email onForm submit…they are their, I save them, I do a test and they work, but then they stop working, if I go back in my triggers are either greyed out or gone. I add them again, save everything, exit, reopen immediately, they are still good. I go to my next form do the same thing, but when I return to the previous form my triggers are off again??? Why is this happening? Any ideas? Very frustrating, as they work but then stop working for some unknown reason..

Thanks for your help!
Kristen

mark - January 12, 2014

This will not work if I have set trigger “From form”
Problem is, I dont have option to set trigger from spreadsheet! Only from form.

Gabriella - January 13, 2014

How do you add more than one email address?

Dan - January 15, 2014

Hi
Thanks for this, I’ve set up the script as you described and it works perfectly, I do have one small query though that I hope you can help with. I wanted to send a new form but having the responses for the 2nd form going to a new sheet on the original google doc. I unlinked the first form and went to create a new form from scratch.

The script seems to have run itself again and produced a second responses sheet duplicating the data from the first form and has also sent out email confirmations for a second time.

Have I set something up wrong here? Can you not create new forms for the same google doc but just have the responses on different sheets?

I understand that you can only have one form linked at any one time to a doc but I was hoping to unlink and create new forms as I went along, is that possible?

Thanks!

JEdison - January 28, 2014

Brilliant! Thanks, this provided just the solution that I was looking for.
While I’d love to add more “bells and whistles”, this certainly solves my immediately challenges with Google forms.
THANKS

I like the valuable info you provide in your articles.
I will bookmark your blog and check again here frequently.
I’m quite certain I’ll learn a lot of new stuff right here!
Good luck for the next!

Jasmine - February 16, 2014

Is there a way to do this to include a unique confirmation number? That would also be recorded into the excel sheet that you have the form designated to go to…. I need to be able to tie a unique confirmation number with the exact information they input with the form… and then email them or simply display on the submission page the confirmation number… HELP!

I do not know how to fix … can someone please, please help? I have tried deleting the form information (I would uninstall the formemailer but the directions posted on Henri’s page do not work as no delete option shows in the file menu>

Thanks!

Bruno - July 22, 2014

Hello
Thanks for the tutorial
You say ““e.values[1]” will be the first field in your form, and so on.”
Actually, e.values[1] is the first non-blank field of the form. Which can cause problem if some fields remain unfilled. Is there a way to fix this ?
Thanks

Emma - August 20, 2014

Hello,

The instruction seems straightforward enough, but when troubleshooting the script I get the following error message: “TypeError: Unable to read the property “values” from undefined. (Line 2, file: Code)”.

Emma–make sure that you are testing the script by setting up the trigger and submitting the form, so the form row data (“e”) is passed to the function. Do not run it directly from the code view; it will have no input, no “e” to use.

Pieter, take note of the error message ” Invalid email: f (line 21″
I can’t say without seeing your code, but it sounds like there’s an error on the MailApp.sendEmail line because the “toAddress” is invalid. In my code sample that address is the second form field, but it may be different for you depending on what you changed. Can you make sure that a good email address is being entered, and that it is taking the correct value for the email address?

– No longer errormails from google so that’s already ok
– The confirmation mail doesn’t arrive at the ‘toAddress’ in cells J2, J3, J4 (so the ‘9’ is correct I think). So there is still something wrong…

Could it be problematic that I don’t use a gmail adress? I’m registred with an @kvk.be adress…

Since I don’t have your exact script, it’s hard to say what exactly could go wrong.
Although you need to use a Gmail account to be able to make the form and edit the script in Google Docs, it doesn’t matter what email address you are sending notification emails to.
Here are some possible debugging steps:
1. Try sending to a different email address you can check.
2. Check the spam folder; make sure the notification email didn’t end up in there for some reason.
3. Start with the exact script in the tutorial. Make sure that works. Then change it, little by little, testing each time. If it stops working, you know it was the most recent change that broke it.
4. Simplify your script even further. In your example, you are using the 9th form field as the email address. If you are asking each user to input 9 values, it’s already far more complex than my example. Try to make the script very simple, make sure it works, then add extra features, testing each time.
5. Try a static email address. Instead of var toAddress = e.values[9]; try var toAddress = “[email protected]” (substituting one of your email addresses) and see if it works–then try to make it work using a form field. Note: if you can get it to send ANY email, then you can insert different form fields in the email for testing purposes.
Hope this helps!

The mail works! Thanks for the help!
Only one problem left: some variabels asked in the form are not requirend to enter. I’m looking for a code to detect if a variable was not entered, and if so, to give it the stringvalue “Blanco”.

So an example: A second phone number is not required in my form:

if phone2 = empty, then phone2 = “blank”. Or something like that.

I tried 4 different things but nothing works… Whats the correct way to do that?

Thank you for taking the time to post this. I would love to copy and use your script however… I don’t know which values to replace. Is it possible to show the script with the values a person would need to change in a different color like red? Also a little info on what the text represents… values 1-5… are those referring to row 1, 2, 3, 4 and 5? thank you.

ZouZou - June 10, 2015

Hi,

And first of all thank you very much for this! it’s awesome for non programmers to find these types of resources!

And now… my problem.. when I try to save the script I get a syntax error message for my line 35 :

var htmlBody = “Thank you for contacting the HPELC Parents Council on ” + timestamp +

Basically, nowhere on the code will the code “htmlBody” turn blue… none of the 3 occurences… and I can’t figure out why… any ideas?

Many thanks!

ZouZou - June 10, 2015

Hi,

And first of all thank you very much for this! it’s awesome for non programmers to find these types of resources!

And now… my problem.. when I try to save the script I get a syntax error message for my line 35 :

var htmlBody = “Thank you for contacting the Parents Council on ” + timestamp +

Basically, nowhere on the code will the code “htmlBody” turn blue… none of the 3 occurences… and I can’t figure out why… any ideas?

Many thanks!

ZouZou - June 10, 2015

I still haven’t figured out what happened, I rewrote the code and suddently it worked!
Thanks!

Matthew Wright - July 21, 2015

I need to have leading zeros included in some of my fields (if the user enters them, not by default) but it appears that they are automatically stripped.