February 26, 2012

From Google Docs Forms to Email inbox

Another way to add email confirmation script to Google Forms from: http://www.labnol.org/internet/google-docs-email-form/20884/

Should you wish to receive Google Docs form data in an email message as soon as a user submits the form, there’s an easy workaround as explained in the following video.
The trick is that you associate a send mail routine with your Google Docs form that triggers as soon as a “form submit” action happens. And this routine, written using Google Apps Script, does all the magic – it reads the form values that were just submitted and sends them all in one message to a pre-defined address.
Here’s how you can add email capabilities to your Google Forms step by step:
  1. Create a new form in Google Docs (or use any of your existing forms) and switch to the Spreadsheet view.
  2. Go to Tools –> Script Editor and copy-paste the following code in that code editor window. Replace the value of variable “email” with your own email address.
  3. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    function sendFormByEmail(e)
    {
      // Remember to replace XYZ with your own email address
      var email = "XYZ";
      // Optional but change the following variable
      // to have a custom subject for Google Docs emails
      var subject = "Google Docs Form Submitted"
      // The variable e holds all the form values in an array.
      // Loop through the array and append values to the body.
      var message = "";
      for(var field in e.namedValues) {
        message += field + ' :: '
                   + e.namedValues[field].toString() + "\n\n";
      }   
      // This is the MailApp service of Google Apps Script
      // that sends the email. You can also use GmailApp here.
      MailApp.sendEmail(email, subject, message);
      // Watch the following video for details
      // http://youtu.be/z6klwUxRwQI
      // By Amit Agarwal - www.labnol.org
    }
  4. Next go to Triggers –> Current Script’s Triggers and associate the Send Mail function with “On Form Submit” event.
  5. Save the Google script, authorize Google Docs to access your Gmail account (for sending email) and you’re done.
Advanced users can further customize the script to have custom email subject lines that match one of the form fields. Alternatively, you can specify the form submitter’s email address as the replyto address and thus you can directly respond to the user by replying to that email notification.

No comments:

Post a Comment