in Web and Tech

Google Forms Auto-reply Solution

A colleague posted a Google form to gather details online about planned climbs. Another colleague replied that it would be nice if the data submitter can receive an email about the data he/she has just submitted. Kind of like an auto-reply. My curiosity kicked in and I Googled for a solution. Most dealt with auto notifications to the form owner, but the search was fruitful. I did find a pretty good tutorial.

Here’s the text:


Google Docs contact form is one of the most popular contact form on the web and a lot of people are not aware that you can also send automatic reply to the contact form messages using some Google API code.

There is a simple way to Send the contact form in the form of email to your inbox described here. However, if you want a automated reply email sent to your customers it needs to be done using some JavaScript function which can be easily configured to be called inside Google Docs. Below are the steps to configure a automated reply email for your contact form.

Why Send Auto Reply

The default Google contact form feature provides a google docs spreadsheet with user responses, and responding to them needs to be done using manual emails. Sometimes its not feasible to reply back to people immediately, I have found some of the main reasons as listed below

  • In case your site is really popular and lot of people are trying to reach you.
  • There may be lot of spam contact messages.
  • There may be worldwide customers trying to reach you during odd working hours/weekends.
  • You may be on vacation.
  • You need some time to do research before you respond.
  • Some of the categories need not be attended since your answers can be canned. e.g. you want to thank your customer for providing feedback or some help.

Below are simple steps to setup the auto reply email.

  • Create Contact Form

In case you have not created the contact form before, don’t worry. Its really easy and free. Checkout this simple Youtube Video about “How To Create Contact Form on Google Docs“. This example assumes you have at least four fields in your contact form (Name, Email, Subject and content/message). Checkout our contact form for details.

  • Add JavaScript Function for Auto Reply Email

Below is the JavaScript code which can be used to send automated email reply. This code uses Google MailApp utility to send reply. Follow these steps and copy the below code Tools – Script Editor – Copy Below code into the text editor. – Save.

function sendAutoReply(e) {

    // Remember to replace abc@youmail.com with your own email address
    // This will be used to send you a email notification in case of error on form
    // for example if someone entered a wrong email address.

    var myemail = Session.getActiveUser().getEmail();

    //Leave this field blank - it will be auto populated from "Email" field in 
    // your contact form. Make sure you have a "Email" Field in your contact form.
    var email = "";

    // This is going to be the subject of reply email
    // You can change it as per your requirements

    var subject = "Re: ";

    // The variable e holds all the form values in an array.
    // Loop through the array and append values to the body.

    var message = "Thanks for contacting FromDev.com. We will get in touch with you shortly. \n\n-----\n\n";
    var columns, mysheet;

    try {
        mysheet = SpreadsheetApp.getActiveSheet();
        columns = mysheet.getRange(1, 1, 1, mysheet.getLastColumn()).getValues()[0];
        if (e) {
            // This is the submitter's email address
            var em = e.namedValues["Email"];
            if(em) {
              email = em.toString();
            } else {
              throw {'message':'Email Field is required. Please add a field called Email in your sheet.'}
            }
            var sub = e.namedValues["Subject"];
            if(sub) {
              subject += sub.toString();
            }
            // Only include form values that are not blank
            for (var keys in columns) {
                var key = columns[keys];
                if (e.namedValues[key]) {
                    message += key + ' :: ' + e.namedValues[key] + "\n\n";
                }
            }

            // We are using JavaScript MailApp util of Google Apps Script
            // that sends the email.

            MailApp.sendEmail(email, subject, message, {
                replyTo: myemail
            });
        } else {
            var err = 'Required Input params are passed only during live form submission. Please try the script by submitting the live form.';
            Logger.log(err);
            throw {
                'message': err
            };
        }

    } catch (e) {
        MailApp.sendEmail(myemail, "Error in Auto replying to contact form submission. No reply was sent.", e.message + ' \n\ncolumns: ' + columns + ' \n\nmessage: ' + message);
    }
}

Special Caution

Please make sure you take care of following things when you create a form and update any code in script.

  • Field names are case sensitive. Please make sure the field names are exactly matching in the script. (e.g. Email, Subject)
  • Make sure to update the script with your own email address on this line

    var myemail = “abc@youmail.com”;

  • In case you want the auto reply content to be really big make sure you split it into multiple lines (and use Javasctipt append) like below instead of keeping all text in one double quote.

    var message = “Thanks for contacting FromDev.com. We will get in touch with you shortly. \n\n—–\n\n”
    + “Another line \n”
    + “Another line”;

  • Add a Trigger

The next steps is to add a trigger to run the script on form submission. The trigger is a simple configuration to tell google docs to listen to specific event and take action on it. We are simply going to create a trigger at the event of form submission and execute our auto reply email script in the action response to that event. Follow these steps

  • Tools – Script Editor – Resources – Current Script’s Triggers
  • Select Function name “sendAutoReply”
  • Select “From Spreadsheet”
  • Select “On Form Submit”
  • Save
  • Authorize Script

You need to Authorize Script to be run by Google API. The authorization step is really important since Google API will not run the script unless its authorized by the creator. Therefore make sure you are creating the script using the same account which owns the contact form.

  • Lets Test our Script

I believe in test driven development, and as always try to make sure your setup is working fine. This is really simple, just do the form submission using a email address and see if the auto reply email is received as required.

  • Something went wrong?

Your script is still not working? There can be following reasons – Please make sure you have the exact same field names in your contact form. (Specially for Email and Subject). Checkout our contact form. – If someone enters a invalid email address or other error happens on your form, you would be notified with a email similar to below snapshot.


My comments:

  • The post mentioned abc@youmail.com but this was not really in the script. I believe the author missed that he already replaced the value of myemail with something more apt and dynamic Session.getActiveUser().getEmail()
  • In setting up the Trigger, it did not mention that you would have to access the Script Editor via the open page of the linked spreadsheet. Otherwise, you would not have the data source option of “From Spreadsheet”

 

Here’s the link to the test form I created which has the script enabled:
Live Test Form

Here’s a link to the original guide:
http://www.fromdev.com/2012/06/how-to-send-auto-reply-email-on.html

Write a Comment

Comment