Send SMS with Google Scripts and Twilio

S
Published in: Google Apps Script - SMS

The Twilio service helps you send SMS and MMS programmatically. They have a REST API that you can call through Google Apps Script and send SMS text messages from your Google Apps based projects. For instance, you can get a text notification on a mobile phone when a Google Form is submitted. Or you can send short text messages to multiple people from a Google Spreadsheet similar to Mail Merge.

To get started, you need to register for an account at Twilio (they have trial accounts too) and send text any phone number in the world via Google Scripts. You will use your Twilio account SID as the username and your auth token as the password for HTTP Basic authentication.

/*

Send SMS via #AppsScript
=========================

Written by Amit Agarwal
Website: ctrlq.org
Email: amit@labol.org
Twitter: @labnol

*/

function sendSMS(toNumber, fromNumber, smsText) {

  if (smsText.length > 160) {
    Logger.log("The text should be limited to 160 characters");
    return;
  }

  var accountSID = "ctrlq.sid";
  var authToken = "ctrlq.token";

  var url = "https://api.twilio.com/2010-04-01/Accounts/" + accountSID + "/Messages.json";

  var options = {
    method: "POST",
    headers: {
      Authorization: "Basic " + Utilities.base64Encode(accountSID + ":" + authToken)
    },
    payload: {
      "From" : fromNumber,
      "To"   : toNumber,
      "Body" : smsText
    },
    muteHttpExceptions: true
  };

  var response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());

  if (response.hasOwnProperty("sid")) {
    Logger.log("Message sent successfully.");
  }

  Utilities.sleep(1000);

}

Sending SMS with Twilio - Notes

  1. The recipient’s phone number should be formatted with a ’+’ and always include the country code e.g., +16175551212 (E.164 format). 2. The SMS body should be less than 160 characters else Twillo would split the text into multiple messages. 3. The sender’s phone number should be a valid Twilio phone number. You cannot put just any mobile phone number to prevent spoofing.

It is important to add sleep between consecutive SMS sending calls as Twilio will only send out messages at a rate of one message per phone number per second.

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - SMS

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube