PHP Quickstart: Send SMS and MMS Text Messages

In this PHP SMS Quickstart, we're going to throw a monkey party! We'll use our PHP skills to invite our friends to the party via SMS and MMS text messages.

To send an outgoing SMS message with Twilio, perform an HTTP POST to the Messages Resource URI in Twilio's REST API. We can also use the Twilio PHP Helper Library to make REST requests.

If you are on a Twilio Trial account, your outgoing SMS messages are limited to phone numbers that you have verified with Twilio. Phone numbers can be verified via your Twilio Console's Verified Caller IDs. Note the 'From' parameter will need to be a phone number you have purchased from Twilio.

Loading Code Samples...
Language
SDK Version:
  • 4.x
  • 5.x
<?php
    /* Send an SMS using Twilio. You can run this file 3 different ways:
     *
     * 1. Save it as sendnotifications.php and at the command line, run 
     *        php sendnotifications.php
     *
     * 2. Upload it to a web host and load mywebhost.com/sendnotifications.php 
     *    in a web browser.
     *
     * 3. 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.
     */

    // Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
    // and move it into the folder containing this sendnotifications.php file.
    require "Services/Twilio.php";

    // Step 2: set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $AuthToken = "your_auth_token";

    // Step 3: instantiate a new Twilio Rest Client
    $client = new Services_Twilio($AccountSid, $AuthToken);

    // Step 4: make an array of people we know, to send them a message. 
    // Feel free to change/add your own phone number and name here.
    $people = array(
        "+15558675309" => "Curious George",
        "+15558675308" => "Boots",
        "+15558675307" => "Virgil",
    );

    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->sendMessage(

            // Step 6: Change the 'From' number below to be a valid Twilio number 
            // that you've purchased
            "+15017250604", 

            // the number we are sending to - Any phone number
            $number,

            // the sms body
            "Hey $name, Monkey Party at 6PM. Bring Bananas!"
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
<?php
    /* Send an SMS using Twilio. You can run this file 3 different ways:
     *
     * 1. Save it as sendnotifications.php and at the command line, run 
     *         php sendnotifications.php
     *
     * 2. Upload it to a web host and load mywebhost.com/sendnotifications.php 
     *    in a web browser.
     *
     * 3. 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.
     */

    // Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
    // following the instructions to install it with Composer.
    require_once "vendor/autoload.php";
    use Twilio\Rest\Client;
    
    // Step 2: set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $AuthToken = "your_auth_token";

    // Step 3: instantiate a new Twilio Rest Client
    $client = new Client($AccountSid, $AuthToken);

    // Step 4: make an array of people we know, to send them a message. 
    // Feel free to change/add your own phone number and name here.
    $people = array(
        "+15558675309" => "Curious George",
        "+15558675308" => "Boots",
        "+15558675307" => "Virgil"
    );

    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
                // Step 6: Change the 'From' number below to be a valid Twilio number 
                // that you've purchased
                'from' => "+15017250604", 
                
                // the sms body
                'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!"
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
Send SMSes with Twilio's PHP Helper Library

Send SMS Messages in PHP

Sound like a plan? Good - let's look at the details:

  • First, head over to the Twilio website and log into your Twilio Console page. On the Dashboard there is a section labeled "Account Summary". There you will find your Account SID and Auth Token. Copy those values and paste them into Account SID and Auth Token variables.
  • Next, we instantiate a new client object, set the request method to POST, fill the 'To', 'From' and 'Body' parameters into an associative array, and make the REST API request to Twilio. The 'From' parameter should be your Sandbox phone number for a trial account or a purchased Twilio phone number for upgraded accounts.

If your REST request was successful, the SMS has been successfully queued for transmission! The SMS will be sent as soon as possible, at a maximum rate of 1 message per second per 'From' phone number.

By default, your application does not get any notification when an SMS is actually sent or when there is a message delivery failure. To get a callback when an SMS is sent or when there is a failure, you need to include the "StatusCallback" parameter with your REST request. This should point to a Twilio-visible URL which can handle status updates.

Send MMS Messages in PHP

It's also easy to send an outgoing MMS using Twilio and PHP. To send an MMS, you also make an HTTP POST request to the Messages resource but this time specify one or more image Media URLs.

MMS messages can only be sent (and received) by numbers with MMS capability. You can check the capabilities of numbers in the console or query the Available Phone Numbers resource to search for Twilio numbers that are MMS enabled.

Modify the foreach loop as follows:

Loading Code Samples...
Language
SDK Version:
  • 4.x
  • 5.x
<?php
    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->sendMessage(

            // Step 6: Change the 'From' number below to be a valid Twilio number
            // that you've purchased
            "YYY-YYY-YYYY",

            // the number we are sending to - Any phone number
            $number,

            // the sms body
            "Hey $name, Monkey Party at 6PM. Bring Bananas!",

            // Step 7: Add a url to the image media you want to send
            array("https://demo.twilio.com/owl.png", "https://demo.twilio.com/logo.png")
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
<?php
    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
                // Step 6: Change the 'From' number below to be a valid Twilio number 
                // that you've purchased
                'from' => "+15017250604", 

                // the sms body
                'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!",
                
                // Step 7: Add url(s) to the image media you want to send
                'mediaUrl' => array("https://demo.twilio.com/owl.png", 
                    "https://demo.twilio.com/logo.png")
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
Send MMSes with Twilio's PHP Helper Library

For more information about sending SMS and MMS messages - with PHP or another language - see the REST documentation.

Next Quickstart: Replying to SMS and MMS Messages in PHP»

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

1 / 1
Loading Code Samples...
SDK Version:
  • 4.x
  • 5.x
<?php
    /* Send an SMS using Twilio. You can run this file 3 different ways:
     *
     * 1. Save it as sendnotifications.php and at the command line, run 
     *        php sendnotifications.php
     *
     * 2. Upload it to a web host and load mywebhost.com/sendnotifications.php 
     *    in a web browser.
     *
     * 3. 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.
     */

    // Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
    // and move it into the folder containing this sendnotifications.php file.
    require "Services/Twilio.php";

    // Step 2: set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $AuthToken = "your_auth_token";

    // Step 3: instantiate a new Twilio Rest Client
    $client = new Services_Twilio($AccountSid, $AuthToken);

    // Step 4: make an array of people we know, to send them a message. 
    // Feel free to change/add your own phone number and name here.
    $people = array(
        "+15558675309" => "Curious George",
        "+15558675308" => "Boots",
        "+15558675307" => "Virgil",
    );

    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->sendMessage(

            // Step 6: Change the 'From' number below to be a valid Twilio number 
            // that you've purchased
            "+15017250604", 

            // the number we are sending to - Any phone number
            $number,

            // the sms body
            "Hey $name, Monkey Party at 6PM. Bring Bananas!"
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
<?php
    /* Send an SMS using Twilio. You can run this file 3 different ways:
     *
     * 1. Save it as sendnotifications.php and at the command line, run 
     *         php sendnotifications.php
     *
     * 2. Upload it to a web host and load mywebhost.com/sendnotifications.php 
     *    in a web browser.
     *
     * 3. 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.
     */

    // Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
    // following the instructions to install it with Composer.
    require_once "vendor/autoload.php";
    use Twilio\Rest\Client;
    
    // Step 2: set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $AuthToken = "your_auth_token";

    // Step 3: instantiate a new Twilio Rest Client
    $client = new Client($AccountSid, $AuthToken);

    // Step 4: make an array of people we know, to send them a message. 
    // Feel free to change/add your own phone number and name here.
    $people = array(
        "+15558675309" => "Curious George",
        "+15558675308" => "Boots",
        "+15558675307" => "Virgil"
    );

    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
                // Step 6: Change the 'From' number below to be a valid Twilio number 
                // that you've purchased
                'from' => "+15017250604", 
                
                // the sms body
                'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!"
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
SDK Version:
  • 4.x
  • 5.x
<?php
    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->sendMessage(

            // Step 6: Change the 'From' number below to be a valid Twilio number
            // that you've purchased
            "YYY-YYY-YYYY",

            // the number we are sending to - Any phone number
            $number,

            // the sms body
            "Hey $name, Monkey Party at 6PM. Bring Bananas!",

            // Step 7: Add a url to the image media you want to send
            array("https://demo.twilio.com/owl.png", "https://demo.twilio.com/logo.png")
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
<?php
    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
                // Step 6: Change the 'From' number below to be a valid Twilio number 
                // that you've purchased
                'from' => "+15017250604", 

                // the sms body
                'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!",
                
                // Step 7: Add url(s) to the image media you want to send
                'mediaUrl' => array("https://demo.twilio.com/owl.png", 
                    "https://demo.twilio.com/logo.png")
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }