Wednesday, May 8, 2013

Send a custom email from a Google Form Submission

Below are detailed steps on how you can send a customized email from a Google Docs form submission.

1)  Create a Google Form and set the form to automatically post the results to a spreadsheet.  For this example code will be based upon this tutorial on how to Create a Basic Google Form.


2)  In your Google Drive open the spreadsheet where the form responses are being stored.
Tip:  You can access you google drive by going to https://drive.google.com


3)  After the form is open go to the script manager by doing the following.  
Tools > Script Editor.



Click on "Blank Project"


4)  DELETE ALL of the code appearing in the Code.gs box.


5)  Replace it with all of the following code..

function sendMessage(e) {
   //Capture the items from the form
   var firstName = e.values[1]; //declare a variable storing the first name
   var lastName = e.values[2]; //declare a variable storing the last name
   var emailAddress = e.values[3]; //declare a variable storing the user's email address
   var wantsEmail = e.values[4]; //declare a variable storing whether or not they want an email
   var birthday = e.values[5]; //declare a variable storing the user's birthday

   //set up variables to be used to send out the email
   var fromName = "Company USA Inc."; //the "From" name you want to be displayed in the email
   var replyTo = "Custom@YourDomain.Com"; //you can specify a reply to address if you like
   var Subject = "Form Submission response"; //the subject of the email to be sent out

   //start building the body of the message
   //There is no error checking here and will just assume all fields contain values
   var Body = "Dear " + firstName + " " + lastName + ",\n\n" +
       "Thank you for filling out our web form.\n" +
       "We hope you enjoy your birthday on " + birthday + "\n" +
       "We look forward to speaking with you soon.\n\n" +
       "Regards,\n" +
       "Your Name Here";

   //Do a very basic if statement to see if an email should actually be sent
   //(In theory ALL of the above items associated with the email should be INSIDE this If statement.
   //However, this is just a very basic learing tutorial for you to start with
   if (wantsEmail == "Yes")
   {
       //build and send out the email
       MailApp.sendEmail(emailAddress, Subject, Body,
       {name:fromName, replyTo:replyTo});
   } //end of the if statement

}//end of the function


6)  Save and then name the file





7)  Set up a trigger to make the code run whenever someone submits a form.



Then verify you have the following settings...



Finally authorize the script to run



8)  An email will be automatically sent to every user that submits the form.  In gmail it will look similar to this.


FINISHED!  Nicely done.  Be sure to check out the rest of the blog for tips on writing custom code for Google Docs and Google Apps.

Be sure to post any questions / comments below.

No comments:

Post a Comment

Pages