Rate this page:

Track Delivery Status of Messages in PHP

In this guide, we'll show you how to track the delivery status of messages you send with Programmable SMS in your PHP web application. Twilio can notify you about the status of your SMS and MMS messages via a webhook and you can notify Twilio about messages you have confirmed to be delivered.

The code snippets in this guide are written using PHP language version 5.3 or higher, and make use of the Twilio PHP SDK.

What is a Webhook?

Webhooks are user-defined HTTP callbacks. They are usually triggered by some event, such as receiving an SMS message or an incoming phone call. When that event occurs, Twilio makes an HTTP request (usually a POST or a GET) to the URL configured for the webhook.

To handle a webhook, you only need to build a small web application that can accept the HTTP requests. Almost all server-side programming languages offer some framework for you to do this. Examples across languages include ASP.NET MVC for C#, Servlets and Spark for Java, Express for Node.js, Django and Flask for Python, and Rails and Sinatra for Ruby. PHP has its own web app framework built in, although frameworks like Laravel, Symfony and Yii are also popular.

Whichever framework and language you choose, webhooks function the same for every Twilio application. They will make an HTTP request to a URI that you provide to Twilio. Your application performs whatever logic you feel necessary - read/write from a database, integrate with another API or perform some computation - then replies to Twilio with a TwiML response with the instructions you want Twilio to perform.

Receive Status Events in your Web Application

Statuses for which you can receive notifications include: queued, failed, sent, delivered, and undelivered. For a full description of these and other message statuses, see the API reference.

To get Twilio to call your webhook, you need to provide a URL to your application in the StatusCallback parameter of each message for which you want the status callbacks. Below is an example of how you can specify this parameter. Normally, you would use a URL in your application, but let's use PostBin so we can debug what Twilio sends your webhook.


        To get this code sample to run, first fill in your Account SID and Auth Token (found on the Twilio Console dashboard), replace the 'From' phone number with one of your Twilio numbers, and replace the 'To' number with your mobile number. Then, head over to PostBin and create a new bin. Replace the StatusCallback parameter in the snippet to the URL of your new bin.

        When you run the code, you should receive your text message. In addition, you should see at least one request come into your PostBin similar to this:

        PostBin Example

        This request shows the MessageStatus of sent. Assuming all was successful, you should see that followed up by another request with a status of delivered (it may take a few minutes to show up).

        Once you get the hang of how the StatusCallback works, you're ready to handle the callback in your own application. Below is an example of how you might do this, logging the status for each message.


              Getting Status Events for TwiML Generated Messages

              When you reply to an incoming message with the <Message> verb, you can also provide an action attribute to specify the URL for your callback webhook. The callback can be the same as those explored in the previous sections.

              <?xml version="1.0" encoding="UTF-8"?>
                  <Message action="">This message will be tracked!</Message>

              Provide Message Delivery Feedback to Twilio

              The Twilio Message Feedback API enables you to programmatically report back to Twilio critical deliverability information. Actions that indicate a message was received can then be used by Twilio to identify network issues and improve deliverability of your messages. For example, a user action could be a person entering their two-factor authentication code or clicking a unique link in a message.

              NOTE: You must contact our support team to enable the Message Feedback API on your account. Requests to this endpoint will 404 until you do.

              Why do we want this feedback?

              We already have many sources of data to optimize overall delivery deliverability. We use direct feedback from our carrier partners, deliverability data from some our larger customers as well as various other monitoring techniques. But nothing trumps the live collective data of our customers. For your messages, we trust what you tell us the most.

              By looking at both aggregate data and data specific to your account, we have a better chance at improving the delivery of your messages.

              How It Works

              Submitting Message Feedback to Twilio is a three step process:

              1. When you send a message, let Twilio know that you are expecting a trackable user action by including the new ProvideFeedback=true parameter in the initial HTTP POST.
              2. Save the Message SID from the POST response. You’ll need this later.
              3. When the user takes the expected action, such as entering a PIN code, let Twilio know the message was received by sending a POST request to the Message Feedback instance subresource with Outcome=confirmed parameter. If the user does not take an action, do not POST anything.

              Let's look at some code. First, here's some code that sends a message with a unique URL, setting the ProvideFeedback parameter to true and also saving the Message SID for later recall.


                    Next, here's the code we might run when the user clicks the unique URL. Based on the unique URL, we know the Message SID and can report back to Twilio that the message was successfully delivered.


                          Examples of Trackable User Actions

                          Examples of user actions include:

                          • A user receives a passcode via SMS and enters it into a web site or app
                          • A user enters a temporary password.
                          • A user replies to a message with a call or SMS.
                          • A user clicks on a unique link contained in the message.

                          Once a user takes an action, you know for certain that they received the message. This information is submitted to Twilio, and we take care of the rest.

                          Kat King Maylon Pedroso David Prothero Andrew Baker Paul Kamp
                          Rate this page:

                          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.


                                Thank you for your feedback!

                                We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

                                Sending your feedback...
                                🎉 Thank you for your feedback!
                                Something went wrong. Please try again.

                                Thanks for your feedback!

                                Refer us and get $10 in 3 simple steps!

                                Step 1

                                Get link

                                Get a free personal referral link here

                                Step 2

                                Give $10

                                Your user signs up and upgrade using link

                                Step 3

                                Get $10

                                1,250 free SMSes
                                OR 1,000 free voice mins
                                OR 12,000 chats
                                OR more