Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Send SMS and MMS Messages in PHP


In this guide, we'll show you how to send SMS and MMS messages in your PHP web application using Twilio's Programmable Messaging(link takes you to an external page).

(warning)

Warning

While you can send text-only SMS messages almost anywhere on the planet(link takes you to an external page), sending media is currently only available in the US and Canada. Learn more in this support article(link takes you to an external page).

The code snippets in this guide target PHP versions 5.x and 7.x and make use of the Twilio PHP Helper Library(link takes you to an external page). Keep an eye on currently supported PHP releases(link takes you to an external page) and always use a version which still receives security updates.


Sign up for (or log in to) a Twilio Account

sign-up-for-or-log-in-to-a-twilio-account page anchor
(information)

Info

Already have a Twilio account with a phone number? You're all set - feel free to skip this section.

(warning)

Warning

If you are sending SMS to the U.S. or Canada, before proceeding further please be aware of updated restrictions on the use of Toll-Free numbers for messaging, including TF numbers obtained through Free Trial. Please click here(link takes you to an external page) for details.

Before you can receive phone calls and send messages, you'll need to sign up for a Twilio account(link takes you to an external page) and purchase a Twilio phone number(link takes you to an external page).

If you're brand new to Twilio, you can sign up for a free trial account(link takes you to an external page) to get started. Once you've signed up, head over to your Console(link takes you to an external page) and grab your Account SID and your Auth Token. You will need those values for the code samples below.


Get a phone number with SMS (and MMS) capabilities

get-a-phone-number-with-sms-and-mms-capabilities page anchor

Sending messages requires a Twilio phone number with SMS capabilities. If you don't currently own a Twilio phone number with SMS capabilities, you'll need to buy one. After navigating to the Buy a Number page(link takes you to an external page), check the 'SMS' box and click 'Search':

Buy a SMS capable number.

If you live in the US or Canada and also wish to send MMS messages, you can select the 'MMS' box. When viewing the search results, you can see the capability icons in the list of available numbers:

Click Buy Button to Purchase an SMS-capable Number.

Find a number you like and click "Buy" to add it to your account.

(warning)

Warning

If you're using a trial account, first verify your personal phone number via the console(link takes you to an external page) so that you can test sending SMSes to yourself. (Learn more about how to work with your free trial account.)

Now that you have a Twilio phone number you can start sending messages to mobile devices.


Send an SMS message in PHP via the REST API

send-an-sms-message-in-php-via-the-rest-api page anchor

To send an outgoing SMS message from your Twilio account, you'll need to make an HTTP POST to Twilio's Message resource.

Using the Twilio PHP Helper Library, you can create a new instance of the Message resource and specify the To, From, and Body parameters for your message.

Install Composer and the Twilio PHP Helper Library

install-composer-and-the-twilio-php-helper-library page anchor

If you don't already have the PHP Helper Library installed, you can install it using Composer(link takes you to an external page). If you need to install Composer, follow these instructions for your platform:

There are two ways to install the PHP Helper library - which is easier depends on your platform.

From a terminal, you can run the following command in your project directory:


_10
composer require twilio/sdk

Alternatively, you can create a file named composer.json. In that file, add:


_10
{
_10
"require": {
_10
"twilio/sdk": "^5.0"
_10
}
_10
}

Then run


_10
composer install

Save and Run the PHP SMS/MMS Code

save-and-run-the-php-smsmms-code page anchor

Next, open a file named send-sms.php. Paste in the following content:

Send an SMS using the Programmable Messaging API

send-an-sms-using-the-programmable-messaging-api page anchor
PHP

_23
<?php
_23
_23
// Update the path below to your autoload.php,
_23
// see https://getcomposer.org/doc/01-basic-usage.md
_23
require_once '/path/to/vendor/autoload.php';
_23
_23
use Twilio\Rest\Client;
_23
_23
// Find your Account SID and Auth Token at twilio.com/console
_23
// and set the environment variables. See http://twil.io/secure
_23
$sid = getenv("TWILIO_ACCOUNT_SID");
_23
$token = getenv("TWILIO_AUTH_TOKEN");
_23
$twilio = new Client($sid, $token);
_23
_23
$message = $twilio->messages
_23
->create("+15558675310", // to
_23
[
_23
"body" => "This is the ship that made the Kessel Run in fourteen parsecs?",
_23
"from" => "+15017122661"
_23
]
_23
);
_23
_23
print($message->sid);

Output

_24
{
_24
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"api_version": "2010-04-01",
_24
"body": "This is the ship that made the Kessel Run in fourteen parsecs?",
_24
"date_created": "Thu, 30 Jul 2015 20:12:31 +0000",
_24
"date_sent": "Thu, 30 Jul 2015 20:12:33 +0000",
_24
"date_updated": "Thu, 30 Jul 2015 20:12:33 +0000",
_24
"direction": "outbound-api",
_24
"error_code": null,
_24
"error_message": null,
_24
"from": "+15017122661",
_24
"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"num_media": "0",
_24
"num_segments": "1",
_24
"price": null,
_24
"price_unit": null,
_24
"sid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"status": "queued",
_24
"subresource_uris": {
_24
"media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
_24
},
_24
"to": "+15558675310",
_24
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json"
_24
}

Replace the placeholder values for sid and token with your unique values for Account SID and Auth Token. You can find these in your Twilio console(link takes you to an external page).

(error)

Danger

Please note: it's okay to hardcode your credentials to test, but use environment variables to keep them secret before deploying to production. Check out how to set environment variables(link takes you to an external page) for more information.

You'll tell Twilio which phone number to use to send this message by replacing the from number with the Twilio phone number you purchased earlier. In the line with the // to comment (line 11 in our sample code), put the recipient number (likely your cell phone number).

Both of these parameters must use E.164 formatting ("+" and a country code, e.g., +16175551212)

Once you've updated the code, save it. You can then run it from the command line:

php send-sms.php

Then you should quickly see a blinking or beeping phone with the SMS - neat!

(warning)

Warning

If you're using a trial account, you'll notice that any messages you send will always begin with "Sent from a Twilio trial account." Once you upgrade your account, you will no longer see this message. Learn more about sending SMS and MMS messages from a trial account.

Let's take a moment to understand what's going on behind the scenes when you send this request to Twilio.

When Twilio receives your request to send an SMS via the REST API, it will check that you've included a valid Twilio phone number in the from field. Twilio will then either queue the SMS or return this HTTP error in its response to your request.

Outgoing SMS Diagram.

Assuming your request didn't result in any errors, Twilio's HTTP response will include the SID of the new message. This unique identifier will help us reference this message later: in the code above, we printed that SID to the terminal.

Twilio's JSON response includes a robust amount of data about your message. A sample response might look like this:


_24
{
_24
"sid": "SMxxxxxxxxxxxxxxx",
_24
"date_created": "Thu, 09 Aug 2018 17:26:08 +0000",
_24
"date_updated": "Thu, 09 Aug 2018 17:26:08 +0000",
_24
"date_sent": null,
_24
"account_sid": "ACxxxxxxxxxxxxxxxx",
_24
"to": "+15558675310",
_24
"from": "+15017122661",
_24
"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"body": "This is the ship that made the Kessel Run in fourteen parsecs?",
_24
"status": "queued",
_24
"num_segments": "1",
_24
"num_media": "0",
_24
"direction": "outbound-api",
_24
"api_version": "2010-04-01",
_24
"price": null,
_24
"price_unit": "USD",
_24
"error_code": null,
_24
"error_message": null,
_24
"uri": "/2010-04-01/Accounts/ACxxxxxxxxx/Messages/SMxxxxxxxxxxxx.json",
_24
"subresource_uris": {
_24
"media": "/2010-04-01/Accounts/ACxxxxxxxx/Messages/SMxxxxxxxxxxxxx/Media.json"
_24
}
_24
}

You can access any of these attributes from your PHP code, much like we did when we printed the sid.

Try adding a print statement like print($message->status);. Save the file, then run the code with php send-sms.php one more time. You should see the status of your message, "queued", printed to your terminal.

If you receive an error in response from Twilio or never receive the message, you may want to check out these tips for troubleshooting undelivered messages(link takes you to an external page).


_24
{
_24
"sid": "SMxxxxxxxxxxxxxxx",
_24
"date_created": "Thu, 09 Aug 2018 17:26:08 +0000",
_24
"date_updated": "Thu, 09 Aug 2018 17:26:08 +0000",
_24
"date_sent": null,
_24
"account_sid": "ACxxxxxxxxxxxxxxxx",
_24
"to": "+15558675310",
_24
"from": "+15017122661",
_24
"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"body": "This is the ship that made the Kessel Run in fourteen parsecs?",
_24
"status": "queued",
_24
"num_segments": "1",
_24
"num_media": "0",
_24
"direction": "outbound-api",
_24
"api_version": "2010-04-01",
_24
"price": null,
_24
"price_unit": "USD",
_24
"error_code": null,
_24
"error_message": null,
_24
"uri": "/2010-04-01/Accounts/ACxxxxxxxxx/Messages/SMxxxxxxxxxxxx.json",
_24
"subresource_uris": {
_24
"media": null
_24
}
_24
}

(information)

Info

Want to track the status of your messages in real-time? You'll need to set up a StatusCallback URL. Learn more in our guide on tracking the message status of outbound messages.

Sending multiple messages or a message to multiple recipients

sending-multiple-messages-or-a-message-to-multiple-recipients page anchor

To send more than one message, simply call


_10
$twilio->messages->create(...

in a loop - there is no need for you to add a delay in your logic. You can send as many messages as you'd like as quickly as you can and Twilio will queue them up for delivery at your prescribed rate limit(link takes you to an external page). This will create a new Message instance for each phone number in the list.

You may find that it's helpful to organize your account and message logs into separate Messaging Services. See our guide on how to set up and send messages from a messaging service for more tips.


Send a message with media (MMS) in PHP

send-a-message-with-media-mms-in-php page anchor

To include media in your Twilio-powered text message, you need to make one small addition to the code we wrote above. To send an MMS, you will still make an HTTP POST request to the Message resource but this time specify a parameter for the URL of media, such as an image.

Create a file called send-mms.php and include the following code:

Send a Message with an Image URL

send-a-message-with-an-image-url page anchor
PHP

_24
<?php
_24
_24
// Update the path below to your autoload.php,
_24
// see https://getcomposer.org/doc/01-basic-usage.md
_24
require_once '/path/to/vendor/autoload.php';
_24
_24
use Twilio\Rest\Client;
_24
_24
// Find your Account SID and Auth Token at twilio.com/console
_24
// and set the environment variables. See http://twil.io/secure
_24
$sid = getenv("TWILIO_ACCOUNT_SID");
_24
$token = getenv("TWILIO_AUTH_TOKEN");
_24
$twilio = new Client($sid, $token);
_24
_24
$message = $twilio->messages
_24
->create("+15558675310", // to
_24
[
_24
"body" => "This is the ship that made the Kessel Run in fourteen parsecs?",
_24
"from" => "+15017122661",
_24
"mediaUrl" => ["https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg"]
_24
]
_24
);
_24
_24
print($message->sid);

Output

_24
{
_24
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"api_version": "2010-04-01",
_24
"body": "This is the ship that made the Kessel Run in fourteen parsecs?",
_24
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"direction": "outbound-api",
_24
"error_code": null,
_24
"error_message": null,
_24
"from": "+15017122661",
_24
"num_media": "0",
_24
"num_segments": "1",
_24
"price": null,
_24
"price_unit": null,
_24
"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"sid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"status": "queued",
_24
"subresource_uris": {
_24
"media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
_24
},
_24
"to": "+15558675310",
_24
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json"
_24
}

Again, update the from and // to commented line to use your Twilio phone number and your mobile phone.

The new mediaUrl parameter in this code tells Twilio where to go to fetch our included image. This must be a publicly accessible URL: Twilio will not be able to reach any URLs that are hidden or require authentication.

As when you send a simple SMS, Twilio will send data about the message in its response to your request. The JSON response will contain the unique SID and URI for your media resource:


_10
"subresource_uris": {"media": "/2010-04 01/Accounts/ACxxxxxxxx/Messages/SMxxxxxxxxxxxxx/Media.json"}

When the Twilio REST API creates your new Message resource, it will save the image found at the specified media_url as a Media resource. Once created, you can access this resource at any time via the API.

Save the file and run it from the command line:


_10
php send_mms.php

In just a moment you should receive a text message with an image - looks good, right?


What's next: Going further with PHP, MMS, and SMS

whats-next-going-further-with-php-mms-and-sms page anchor

Want to build more messaging functionality into your PHP Application? Also, try our PHP SMS Quickstart, and see how to receive and reply to messages in PHP.


Rate this page: