LEARN BY EXAMPLE‎ > ‎Other Examples‎ > ‎

Tutorial: Link a Gmail Filter to Google Calendar SMS Notifications

This tutorial was previously available in the official Google Apps Script documentation but there were concerns about how the tutorial used Google Calendar as a backdoor for sending SMS messages. The decision was made to remove the tutorial so as not to further encourage that usage of the product.

Editor's Note: Romain Vialard is a Google Apps Script Top Contributor. He has contributed interesting articles and blog posts about Apps Script. - Jan Kleinert

January 2012

Goal

This tutorial shows how to create a specific filter in Gmail and be notified by SMS when you receive a very important email.

Editor's Note: Since there are limits in place to the number of SMS notifications that can be sent via Google Calendar in a given time period, this process should only be used for a small number of very important emails.

Time to Complete

Approximately 5 minutes

Prerequisites

Before beginning this tutorial, you should feel comfortable using the Script Editor.

Overview

This tutorial is divided into the following sections:

  1. Running a simple example
  2. Using a time-driven trigger
  3. How the script works
  4. Summary

Running a simple example

  1. In Google Calendar, register your mobile phone to enable SMS notifications.
  2. In Gmail, create a new label named 'Send text'.
  3. In Gmail, create a filter to apply the label 'Send text' to important emails. For example, you may want consider emails from your boss containing the word 'urgent' as important.
  4. Send an email to yourself to try this filter.
  5. In Google Docs, open a new spreadsheet.
  6. Choose the menu Tools > Script Editor.
  7. Copy and paste the following script:
    function sendText() {
      var label = GmailApp.getUserLabelByName('Send text');
      var threads = label.getThreads();
      var now = new Date().getTime();
      for(i in threads){
        CalendarApp.createEvent('IMP- '+threads[i].getFirstMessageSubject(),
                                new Date(now+60000),
                                new Date(now+60000)).addSmsReminder(0);
      }
      label.removeFromThreads(threads);
    }
  8. Save the script.
  9. Click the Run icon. A pop-up opens asking you for your authorization to access the Gmail and Google Calendar services.
  10. Click the Authorize button.
  11. Click the Run icon again.
  12. After one minute, you should receive a text on your mobile device, containing the subject of the email you wrote and sent in Step 4.

Using a time-driven trigger

A time-driven trigger is useful to check the label ‘Send text’ periodically and send you a text each time a new email is found with this label. The function sendTextcan be automatically triggered every day, hour or even minute.

To install a trigger:

  1. In the script editor, open the Resources menu and click Current project's triggers... The Current project's triggers dialog opens.
  2. Click on the link that says No triggers set up. Click here to add one now. A dialog opens, asking you to set up triggers.
  3. Ensure that the function sendText and the event 'Time-driven' are selected.
  4. Select the appropriate time depending on your needs. The script can be automatically triggered every minute, hour, day, etc.
  5. Click the Save button.

How the script works

Most of the work (filtering emails and sending SMS notifications) is handled directly by Gmail and Google Calendar; the script is only here to make the right connection between those two services.

First, the script retrieves all the threads (conversations) found with the label ‘Send text’. Then, for each of those threads, it creates an event in your calendar, starting one minute later and adds an SMS reminder to this event. To finish, the script removes the ‘Send text’ label from those threads.

Summary

Congratulations, you've completed this tutorial. You should now be able to receive SMS notifications from Gmail.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies.

Last updated June 3, 2013.