Sending Confirmation Emails with a Google Docs Form

Posted on Jun 18, 2011 in Tech Tips & Tricks | 219 comments

Screenshot of a Google Apps Script

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.

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!

Screenshot - how to open the Script Editor in Google Docs
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/>&nbsp;<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);
}

Screenshot - Script Editor after pasting sample script
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!
Screenshot - Triggers Menu
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.

219 Comments

  1. Thank you! Very helpful and easy solution.

  2. 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!

  3. 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

  4. 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

  5. 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.

  6. How do you add more than one email address?

  7. 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!

  8. 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

  9. Hi, this is a great script and it works perfectly. But can anyone explain how to modify it to include inline images? Or do I have to rewrite the script entirely using ‘function inlineImage’?

  10. 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!

  11. 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!

  12. Thank you! This was most useful and very easy to use.

  13. Hello,
    I keep getting a formemailer error: Your actual quota is: 80
    It is below the warning threshold: 150

    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!

  14. 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

  15. 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)”.

    What does this mean and how can this error be fixed?

  16. 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.

  17. I have the same problem as Emma and I can’t solve it with your explination. The form is saved.

    The current project’s triggers are set as followed:

    Run: onFormSubmit
    Events: fromSpreadsheet on Form Submit
    Notifications: via e-mail at ********@kvk.be immediately

    When I click on run I have the same message as Emma and I receive following mail:

    Your script, Bevestigingsmail, has recently failed to finish successfully. A summary of the failure(s) is shown below. To configure the triggers for this script, or change your setting for receiving future failure notifications, click here.
    The script is used by the document KVK Ind**********tage (krok*****ie) (Reacties).
    Details:
    Start Function Error Message Trigger End
    12/18/14 3:58 PM onFormSubmit Invalid email: f (line 21, file “Code”) formSubmit 12/18/14 3:58 PM
    Sincerely,
    Google Apps Script

    Need help? Visit the Google Apps Script documentation. Please do not reply to this message. (c) 2014 Google

  18. 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?

  19. That was a mistake… I realised it after posting the problem.
    Now I solved that. I have reduced to code to the basic information in order to test if everything works:

    function myFunction(e) {
    var toAddress = e.values[9];
    var subject = “CAP Confirmation”
    var message = “Thank you”

    MailApp.sendEmail(toAddress, subject, message)}

    – 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…

Trackbacks/Pingbacks

  1. Forms confirmation | Info007cleanin - [...] Sending Confirmation Emails with a Google Docs Form | AlamoxieJun 18, 2011 … One surprising lack is that of …
  2. Project 2, Day 1 – Codecademy and Javascript | An Hour Each Day - [...] no quick and easy way to do this in Google Docs forms, but there’s a great little tutorial here …
  3. Using Google docs to send email « News Apps Blog - [...] I thought it was a pretty cool solution and sounds like something that would be helpful to other folks, …
  4. Send Confirmation Emails upon submission of Google Form…. » Open School Sites - [...] http://alamoxie.com/blog/tech-tips/sending-confirmation-emails-google-docs-form/ [...]
  5. Google app script – how to count number of google form response | Stackforum.com - [...] copied code from a tutorial. But need help to get the count of form responses. Here is how it …

Submit a Comment

Manage your comment subscriptions