Posted on Jun 18, 2011 in Tech Tips & Tricks | 175 comments
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.
[Original post 6/18/11. Edited 10/29/11 to add HTML email to the example. Edited 4/21/12 to add more screenshots and correct "Triggers menu" to "Resources menu".]
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.

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.
function onFormSubmit(e) {
var timestamp = e.values[0];
var yourName = e.values[1];
var toAddress = e.values[2];
var eventName = e.values[3];
var visitDate = e.values[4];
var visitComments = e.values[5];
var subject = "CAP Confirmation - " + eventName + " " + visitDate;
var emailBody = "Thank you for your Club Ambassador Program report submitted on " + timestamp +
"\n\nThe details you entered were as follows: " +
"\nYour Name: " + yourName +
"\nYour Email: " + toAddress +
"\nClub or Event Name: " + eventName +
"\nVisit Date: " + visitDate +
"\nVisit Comments: " + visitComments;
var htmlBody = "Thank you for your <b>Club Ambassador Program</b> report submitted on <i>" + timestamp +
"</i><br/> <br/>The details you entered were as follows: " +
"<br/><font color=\"red\">Your Name:</font> " + yourName +
"<br/>Your Email: " + toAddress;
var optAdvancedArgs = {name: "Club Ambassador Program", htmlBody: htmlBody};
MailApp.sendEmail(toAddress, subject,
emailBody, optAdvancedArgs);
}

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.
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.
There! Now try submitting a form and make sure the email looks the way you want.
Thanks, LM! Appreciate it.
What I find strange, Donnie, is that your script would work at first and then stop working. Try completely deleting it (saving the text of the script somewhere safe) and then going through the blog post instructions again from the beginning, pasting in your old script. See if that works.
What if the user does not enter any email address? Is is possible to anyhow find out such persons email address (even manually) and send that person an email?
Nisarg, I’m afraid not… Google Forms were designed with anonymous surveys in mind. You don’t get to see the user’s name, email, IP address, nothing identifying–unless you add form fields and they fill them in. You can make the email field required, but that wouldn’t stop them from entering an invalid email address.
While I have been using a script like this for some of my clients, they often hit the ‘reply’ on their email – which sends me the message (having created the form) and not the person who submitted the form.
Are you able to share how to put the users email (they entered in the form) into some sort of ‘reply to’ so when a message comes they can hit ‘reply’?
Thanks!
I simply wanted to say thanks once more. I am not sure the things that I would have accomplished without the entire strategies documented by you directly on such situation. It was before a real alarming case in my opinion, however , spending time with your expert manner you managed it made me to weep with joy. I am just thankful for this advice and in addition sincerely hope you recognize what an amazing job you are always doing instructing others using a blog. Probably you haven’t got to know any of us.
| Reasonable Juicer Reviews Solutions – Some Basic Tips
I am wondering if it is possible to include within the confirmation email the link that a respondent can use to edit their responses. I have a long form that users may want to complete in two separate sessions. Expecting users to copy and save a link and then remember to use that link instead of the original email invitation to the form seems problematic. It would be fantastic if I could automatically send them an email with the edit link.
Ant insight would be appreciated.
Thanks,
Mark
Sure, Scott. See https://developers.google.com/apps-script/reference/mail/mail-app. You can just add “replyTo” as one of the arguments to optAdvancedArgs, in the same way that the tutorial shows how to add name and htmlBody.
Mark, the new feature showing people a link to edit their responses is handy–but you’re right, not very useful since there seems to be no way to find that link again once the window is closed. Unfortunately, I don’t know of a way to find that link in Google Apps Script so it can be emailed. If anyone else finds an answer, I hope they will add it here!
Thanks for the reply. It is unfortunate that link is only made available to Google Apps users. Apparently the confirmation email that they can receive through simple configuration settings can be set to include the response link for editing…so Google is aware that the link is valuable…
Thanks,
Mark
A point of clarification–if I recall correctly, Google Apps users can easily edit their response for forms used within their own domain, yes. So that’s useful for forms within a company. That feature has been around for a while.
However, just within the past few weeks Google Docs changed the UI for creating/editing forms–for everyone, not just Google Apps users. When you create a form, at the very bottom you can set a confirmation message and set several checkboxes. One of them says “Allow responders to edit responses after submitting”. If that box is checked, then users that submit a form will see a link to edit their form answers. They can manually save/copy/bookmark that link for later use, but not many users will do that. So the functionality is there, but it’s not as intuitive as convenient as it should be–and as I said, I unfortunately don’t know of a way to programmatically retrieve/generate that link in Google Apps Script. Hopefully this is a feature that will be improved upon soon!
Thanks for the reply but is still isn’t putting the Reply To address (the users entered email) in my ‘reply to’ on Gmail.
The email turns up with the users data fine but if I hit reply it is still sending the reply to my address, not the users entered address.
What’s strange is my gmail shows the following when you click the little down arrow next to the words ‘to me’ in the email header
from: [email protected]
reply-to: [email protected]
to: [email protected]
date: 24 April 2013 16:32
subject: Contact about: website
mailed-by: maestro.bounces.google.com
Signed by: gmail.com
yet, as I say, if I click ‘reply’ it creates a message for [email protected] not [email protected]
can you see anything wrong with this script?
function onFormSubmit(e) {
var timestamp = e.values[0];
var name = e.values[1];
var email = e.values[2];
var subject = e.values[3];
var question = e.values[4];
var to = “[email protected]”;
var replyTo = email;
var subject = “Contact about: ” + subject;
var message = “\n\nName: ” + name +
“\n\nEmail: ” + email +
“\n\nSubject?: ” + subject +
“\n\nComment: ” + question;
MailApp.sendEmail(to, replyTo, subject, message);
}
Thanks!
Nice post. I learn something new and challenging on blogs I stumbleupon on a daily basis.
It will always be helpful to read through content from other authors and practice a little something from other web sites.
Scott, I’m afraid I don’t see any obvious problem from what you’ve shown here. It looks like your script is correct. If the mail client is showing the correct reply-to address, then when you click reply it should be using the reply-to address. Try some different email accounts to experiment with, and make sure the email variable is correct–since you have it showing “Email: ” and the email whenever it sends out and email, that should be easy. I don’t see any obvious problem however. You can also check the mail headers, at least in Gmail, by clicking the drop-down arrow in the upper right corner and clicking “Show original”
I ran into this exact situation a couple of months back when creating an online order form (using the example from this site thank you very much). Google will no longer allow you to use a reply-to address that isn’t in your owned domain (EG: you can’t use gmail.com because you don’t own it). You can set your reply-to to anything you like, but it’s always going to actually reply to your address or an address you specify in your owned domain. If you’re creating your script for someone else, you need to create it using a reply-to address in their domain so you can hand it off to them when you’re finished (I’m speculating a bit, but that was the gist of my understanding as I actually haven’t done this yet).
HTH, and I’m very interested to learn how you eventually solve it.
Hi, Can anyone help me to create a script in google documents form for sending a confirmation email along with the Reference number for every submission of a form….
I am getting an error in line 17 and then in line 27
i don’t understand why those Vars aren’t working. any chance you could help?
thanks,
mark
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function onFormSubmit(e) {
var timestamp = e.values[0];
var firstName = e.values[1];
var lastName = e.values[2];
var toAddress = e.values[3];
var Organization = e.values[4];
var phoneNumber = e.values[5];
var subject = “Marin EdCamp Confirmation – ” + firstName + ” ” + lastName +
var emailBody = “Thank you for your Marin EdCamp Registration submitted on ” + timestamp +
“\n\nThe details you entered were as follows: ” +
“\nYour Name: ” + firstName + ” ” + lastName +
“\nYour Email: ” + toAddress +
“\nOrganization: ” + Organization +
var htmlBody = “Thank you for your Marin EdCamp registration submitted on ” + timestamp +
“ The details you entered were as follows: ” +
“Your Name: ” + yourName + ” ” + lastName +
“Your Email: ” + toAddress;
“ We wil look forward to seeing you at Ross School on August 1
“~EdCampCommittee
var optAdvancedArgs = {name: “Marin EdCamp”, htmlBody: htmlBody};
MailApp.sendEmail(toAddress, subject,
emailBody, optAdvancedArgs);
}
You’re trying to concatenate on line 15 & 25 instead of closing those vars out with semi-colons. This:
var subject = “Marin EdCamp Confirmation – ” + firstName + ” ” + lastName +
var emailBody = “Thank you for your Marin EdCamp Registration submitted on ” + timestamp +
“\n\nThe details you entered were as follows: ” +
“\nYour Name: ” + firstName + ” ” + lastName +
“\nYour Email: ” + toAddress +
“\nOrganization: ” + Organization +
var htmlBody = “Thank you for your Marin EdCamp registration submitted on ” + timestamp +
Should be changed to this:
var subject = “Marin EdCamp Confirmation – ” + firstName + ” ” + lastName; /*changed “+” to “;” */
var emailBody = “Thank you for your Marin EdCamp Registration submitted on ” + timestamp +
“\n\nThe details you entered were as follows: ” +
“\nYour Name: ” + firstName + ” ” + lastName +
“\nYour Email: ” + toAddress +
“\nOrganization: ” + Organization; /*changed “+” to “;” */
var htmlBody = “Thank you for your Marin EdCamp registration submitted on ” + timestamp +
got it, thanks for the script.
Once i started using the Hoodia Cactus Slimming a few weeks ago, I dropped over fifteen lbs . and can now fit back into my skinny clothes. It is easier to take than other drugs i I have attempted because I take it with food, not wait 30 minutes before I take in.
Newbie Q: Do I supply the argument ‘e’? If I run from Script Editor it complains of undefined. If I submit a form via View Live form I don’t get an email.
Newbie A: e is supplied when triggered by a real submit so it only works live, not in the script editor.
Hi Chad –
This is an awesome script. Thank you!
I have a similar question that “Jeff” had a while back. You answered on Dec 22, 2012 with an example syntax for an If – Else statement, but I cannot access the example script you linked to to order to adapt to for my situation. Could you make the script live again so that I can see?
Right now I have…
var Delivery = e.values[6];
var Avail = e.values[7];
var Notes = e.values[8];
var Pickup = e.values[9];
if (Delivery == undefined){
throw “\n\nFor Delivery:” +
“\nDelivery Address: ” +
“\n” + Delivery +
“\nGood times for delivery: ” +
“\n” + Avail ;}
else {
throw “\n\nFor Pickup:” +
“\nGood times to pick up programs at the xyx Museum (Museum address): ” +
“\n” + Pickup ;}
Where there’s more emailbody before and after these statements…
The first problem I see is that you are calling “throw” — I think you would probably want to follow along with the example in the blog post and create a screen that you pass to the call to send an email. Other than that, what is the exact error you are seeing?