Get Started

SMS Message Notifications and Alerts

SMS messages are a common form of communication for mobile phone users and a great way to distribute information to several people at once. With Twilio SMS, developers can easily send and receive SMS messages programmatically from Twilio phone numbers.

Check out the Twilio SMS Quickstart for a step by step introduction to using Twilio SMS.

Concepts

Twilio exposes outbound SMS messaging functionality through the Twilio REST API. Making a POST request to the SMS Messages resource will send an SMS message to the phone number you specify.

This HowTo utilizes a Twilio Helper Library to interact with the REST API.

If you have further questions about Twilio SMS be sure to check out the SMS FAQ.

Download

sms-notification.zip

Implementation

  • 1

    • Say that we run an IT department and want to notify all server admins if a server fails.

      Below, we construct an array which contains the names and numbers of our server admins. This array could easily be replaced with a database query to adapt this example for a real-world application.

      • howtos/sms-notification/sendnotifications.php
        <?php
        	/* Send an SMS using Twilio. You can run this file 3 different ways:
        	 *
        	 * - Save it as sendnotifications.php and at the command line, run 
        	 *        php sendnotifications.php
        	 *
        	 * - Upload it to a web host and load mywebhost.com/sendnotifications.php 
        	 *   in a web browser.
        	 * - Download a local server like WAMP, MAMP or XAMPP. Point the web root 
        	 *   directory to the folder containing this file, and load 
        	 *   localhost:8888/sendnotifications.php in a web browser.
        	 */
        	// Include the PHP Twilio library. You need to download the library from 
        	// twilio.com/docs/libraries, and move it into the folder containing this 
        	// file.
        	require "Services/Twilio.php";
        	
        	// Set our AccountSid and AuthToken from twilio.com/user/account
        	$AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        	$AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
        
        	// Instantiate a new Twilio Rest Client
        	$client = new Services_Twilio($AccountSid, $AuthToken);
        
        	/* Your Twilio Number or Outgoing Caller ID */
        	$from = 'NNNNNNNNNN';
        
        	// make an associative array of server admins. Feel free to change/add your 
        	// own phone number and name here.
        	$people = array(
        		"4158675309" => "Johnny",
        		"4158675310" => "Helen",
        		"4158675311" => "Virgil",
        	);
        
        	// Iterate over all admins in the $people array. $to is the phone number, 
        	// $name is the user's name
        	foreach ($people as $to => $name) {
        		// Send a new outgoing SMS */
        		$body = "Bad news $name, the server is down and it needs your help";
        		$client->account->sms_messages->create($from, $to, $body);
        		echo "Sent message to $name";
        	}
        ?>
            

      To send a new SMS message we POST to the Twilio SMS/Messages list resource. The SMS list resource only requires three pieces of information. A "To" phone number that will receive the message, a "From" number that will be the sender of the message, and a "Body", which is the actual message. The "From" number must be a Twilio number.

      NOTE: If you are sending SMS from a free trial, you can only send SMS messages to phone numbers you have previously validated with Twilio. This restriction does not exist for upgraded accounts.

  • 2

    • Twilio queues the SMS for delivery, and responds with

      • howtos/sms-notification/smsmessage.xml
        <?xml version="1.0"?>
        <TwilioResponse>
            <SMSMessage>
                <Sid>SM0ab15a14bca78fa582ed7a509f0bde31</Sid>
                <AccountSid>AC4bf2dafb92341f7caf8650403e422d23</AccountSid>
                <DateCreated>Fri, 08 Jan 2010 11:46:42 -0800</DateCreated>
                <DateUpdated>Fri, 08 Jan 2010 11:46:42 -0800</DateUpdated>
                <DateSent/>
                <To>4158675309</To>
                <From>YYYYYYYYYY</From>
                <Body>Bad news Johnny, the server is down and it needs your help</Body>
                <Status>queued</Status>
                <Flags>4</Flags>
                <Price/>
            </SMSMessage>
        </TwilioResponse>
            

      The response from Twilio indicates the message is queued and waiting to be sent. To get a notification when the message is successfully sent or fails, specify a URL for the StatusCallback parameter with your request. See the SMS Message documentation for more information.

Other Information

For an overview of all the Twilio SMS API features, check out the SMS Quickstart, examples in the Twilio API wrapper libraries, and the SMS FAQ.