"Failure to submit to convention precedes success due to innovation"

Code Snippet (Send Email through Google form)

Posted by Jeremy D. Stakich on January 14, 2016

The following describes how to get the questions and responses and send email from a Google Form.

This is an example of using an App Script that gets the questions and responses and then sends an email from a Google Form.

Step-by-step guide

1. Click Tools.

2. Click Script Editor...

3. Paste the following code in.

4. Also Note that when keying your questions on the forms , be sure not to include special characters such as “ ' ”.

5. !!!!!!!!!!!! Be sure to assign a trigger to the function 'onFormSubmit' by clicking Resources , clicking Current Project Triggers.

var docTemplate = "1ZSqmId2BBjtz6PmgQEmusjnkHGsFKD1CBSq0rrQk6Kc"; var docName = "Google Quiz"; function onFormSubmit(e) { var em = EmailFormSubmission(e); } function EmailFormSubmission(e) { //************************* Application variables to change ********************************** var Survey_Name = "Google Quiz"; var EmailAddressOf_Maint = "jeremystakich2@gmail.com"; //********************************************************************************************* var form = FormApp.getActiveForm(); var responses = e.response; // var EmailAddressOfActiveUser = Session.getActiveUser().getEmail(); //************************* Reads the first ten responses on the form ************************* var EmailAddressOfActiveUser = responses.getItemResponses()[0].getResponse(); var Answer_Zero = responses.getItemResponses()[0].getResponse(); var Answer_One = responses.getItemResponses()[1].getResponse(); var Answer_Two = responses.getItemResponses()[2].getResponse(); var Answer_Three = responses.getItemResponses()[3].getResponse(); var Answer_Four = responses.getItemResponses()[4].getResponse(); var Answer_Five = responses.getItemResponses()[5].getResponse(); var Answer_Six = responses.getItemResponses()[6].getResponse(); var Answer_Seven = responses.getItemResponses()[7].getResponse(); var Answer_Eight = responses.getItemResponses()[8].getResponse(); var Answer_Nine = responses.getItemResponses()[8].getResponse(); var Answer_Ten = responses.getItemResponses()[10].getResponse(); //********************************************************************************************* //************************* Reads the first ten questions on the form ************************* var Question_Zero = responses.getItemResponses()[0].getItem().getTitle(); var Question_One = responses.getItemResponses()[1].getItem().getTitle(); var Question_Two = responses.getItemResponses()[2].getItem().getTitle(); var Question_Three = responses.getItemResponses()[3].getItem().getTitle(); var Question_Four = responses.getItemResponses()[4].getItem().getTitle(); var Question_Five = responses.getItemResponses()[5].getItem().getTitle(); var Question_Six = responses.getItemResponses()[6].getItem().getTitle(); var Question_Seven = responses.getItemResponses()[7].getItem().getTitle(); var Question_Eight = responses.getItemResponses()[8].getItem().getTitle(); var Question_Nine = responses.getItemResponses()[9].getItem().getTitle(); var Question_Ten = responses.getItemResponses()[10].getItem().getTitle(); //********************************************************************************************* Logger.log(EmailAddressOfActiveUser); Logger.log(Answer_Zero); Logger.log(Answer_One); Logger.log(Answer_Two); Logger.log(Answer_Three); Logger.log(Answer_Four); Logger.log(Answer_Five); Logger.log(Answer_Six); Logger.log(Answer_Seven); Logger.log(Answer_Eight); Logger.log(Answer_Nine); Logger.log(Answer_Ten); Logger.log(Question_One); Logger.log(Question_Two); Logger.log(Question_Three); Logger.log(Question_Four); Logger.log(Question_Five); Logger.log(Question_Six); Logger.log(Question_Seven); Logger.log(Question_Eight); Logger.log(Question_Nine); Logger.log(Question_Ten); Logger.log(Survey_Name); //************************* This is what appears after the user fills out the form ************************* form.setConfirmationMessage('Thank you for your response! An email has been sent to you .' ); Logger.log(form.getConfirmationMessage()); //********************************************************************************************************** //************************* Email to user of the form after the form has been filled out ********************* var Body = "Thank you for your response to the survey. " + "The survey - " + Survey_Name + " was filled out by " + EmailAddressOfActiveUser + " . " + '\n\n' + Question_One + " = " + '\n\n' + Answer_One + ". " + '\n\n' + Question_Two + " = " + '\n\n' + Answer_Two + ". " + '\n\n' + Question_Three + " = " + '\n\n' + Answer_Three + ". " + '\n\n' + Question_Four + " = " + '\n\n' + Answer_Four + ". " + '\n\n' + Question_Five + " = " + '\n\n' + Answer_Five + ". " + '\n\n' + Question_Six + " = " + '\n\n' + Answer_Six + ". " + '\n\n' + Question_Seven + " = " + '\n\n' + Answer_Seven + ". " + '\n\n' + Question_Eight + " = " + '\n\n' + Answer_Eight + ". " + '\n\n' + Question_Nine + " = " + '\n\n' + Answer_Nine + ". " + '\n\n' + Question_Ten + " = " + '\n\n' + Answer_Ten + ". " ; var Subject = "Thank you for your response to the survey - " + Survey_Name + " - " + EmailAddressOfActiveUser + " ." ; MailApp.sendEmail(EmailAddressOfActiveUser, Subject, Body); //********************************************************************************************************** //************************* Email to Owner of the form after the form has been filled out ****************** var Body_Maint = "A survey was submitted. The survey - " + Survey_Name + " was filled out by " + EmailAddressOfActiveUser + " . " + '\n\n' + Question_One + " = " + '\n\n' + Answer_One + ". " + '\n\n' + Question_Two + " = " + '\n\n' + Answer_Two + ". " + '\n\n' + Question_Three + " = " + '\n\n' + Answer_Three + ". " + '\n\n' + Question_Four + " = " + '\n\n' + Answer_Four + ". " + '\n\n' + Question_Five + " = " + '\n\n' + Answer_Five + ". " + '\n\n' + Question_Six + " = " + '\n\n' + Answer_Six + ". " + '\n\n' + Question_Seven + " = " + '\n\n' + Answer_Seven + ". " + '\n\n' + Question_Eight + " = " + '\n\n' + Answer_Eight + ". " + '\n\n' + Question_Nine + " = " + '\n\n' + Answer_Nine + ". " + '\n\n' + Question_Ten + " = " + '\n\n' + Answer_Ten + ". " ; var Subject_Maint = "A survey was submitted. by " + EmailAddressOfActiveUser + " ." ; MailApp.sendEmail(EmailAddressOf_Maint,Subject_Maint, Body_Maint); //********************************************************************************************************** //************************* Write data to database ********************************************************** var m = WriteToDb(e); //*********************************************************************************************************** }

Thats all there is to it.

Photographs and ScreenShots by Jeremy Stakich.