SMS and MMS Notifications with PHP

January 10, 2017
Written by
Reviewed by
Paul Kamp
Twilion
Kat King
Twilion
Mica Swyers
Contributor
Opinions expressed by Twilio contributors are their own

sms-mms-php

Today we're going to get your server to automatically sound the (textual) alarm when something goes wrong. Using PHP and Laravel, we'll light up the phones of all of your server administrators when your code raises an exception.

Clone the application and head to the application's README to see how to run the app locally.

Let's get started!

List Your Server Administrators - And Other Lucky Folks

Here we create a list of people who should be notified if a server error occurs.

The only essential piece of data we need is a phone_number for each administrator.

This is a migrated tutorial. Clone the original code from https://github.com/TwilioDevEd/server-notifications-php/

{
    "phone_number": "+1555555555"

Next up, we'll see how to set up the Twilio client.

Configure the Twilio Client

To send a message, we'll need to create a Twilio REST client, which requires reading our TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN and a TWILIO_NUMBER from environmental variables. TWILIO_NUMBER needs not be a real number. It can be any number of your choosing and is mainly used to populate the To field in the outgoing message.

The values for your account SID and Auth Token come from the Twilio console:

Account Credentials

 

<?php

namespace App\Exceptions;

use Twilio\Rest\Client;
use Twilio\Exceptions\TwilioException;
use Exception;

class TwilioExceptionHandler extends Exception
{
    public function __construct()
    {
        @set_exception_handler(array($this, 'report'));
    }

    /**
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, Twilio, etc.
     *
     * @param  \Exception $e
     * @return void
     */
    public function report(Exception $e)
    {
        http_response_code(500);
        $this->notifyThroughSms($e);
        echo $e;
    }

    private function notifyThroughSms($e)
    {
        echo $e;
        foreach ($this->notificationRecipients() as $recipient) {
            $this->sendSms(
                $recipient->phone_number,
                '[This is a test] It appears the server' .
                ' is having issues. Exception: ' . $e->getMessage() .
                ' Go to http://newrelic.com for more details.'
            );
        }
    }

    private function notificationRecipients()
    {
        $adminsFile = './config/administrators.json';
        try {
            $adminsFileContents = file_get_contents($adminsFile);

            return json_decode($adminsFileContents);
        } catch (FileNotFoundException $e) {
            echo $e;
            return [];
        }
    }

    protected function sendSms($to, $message)
    {
        $accountSid = getenv('TWILIO_ACCOUNT_SID');
        $authToken = getenv('TWILIO_AUTH_TOKEN');
        $twilioNumber = getenv('TWILIO_NUMBER');

        $client = new Client($accountSid, $authToken);

        try {
            $client->messages->create(
                $to,
                [
                    "body" => $message,
                    "from" => $twilioNumber
                    //   On US phone numbers, you could send an image as well!
                    //  'mediaUrl' => $imageUrl
                ]
            );
            echo 'Message sent to ' + $twilioNumber;
        } catch (TwilioException $e) {
            echo  $e;
        }
    }
}

Next let's see how to handle application exceptions.

Handle Application Exceptions

Each time an exception is raised anywhere in a Laravel application an TwilioExceptionHandler will handle it.  (Appropriate name!)

This is where we hook our SMS sending code. It's important to call parent so the framework can do its regular error handling as well.

<?php

namespace App\Exceptions;

use Twilio\Rest\Client;
use Twilio\Exceptions\TwilioException;
use Exception;

class TwilioExceptionHandler extends Exception
{
    public function __construct()
    {
        @set_exception_handler(array($this, 'report'));
    }

    /**
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, Twilio, etc.
     *
     * @param  \Exception $e
     * @return void
     */
    public function report(Exception $e)
    {
        http_response_code(500);
        $this->notifyThroughSms($e);
        echo $e;
    }

    private function notifyThroughSms($e)
    {
        echo $e;
        foreach ($this->notificationRecipients() as $recipient) {
            $this->sendSms(
                $recipient->phone_number,
                '[This is a test] It appears the server' .
                ' is having issues. Exception: ' . $e->getMessage() .
                ' Go to http://newrelic.com for more details.'
            );
        }
    }

    private function notificationRecipients()
    {
        $adminsFile = './config/administrators.json';
        try {
            $adminsFileContents = file_get_contents($adminsFile);

            return json_decode($adminsFileContents);
        } catch (FileNotFoundException $e) {
            echo $e;
            return [];
        }
    }

    protected function sendSms($to, $message)
    {
        $accountSid = getenv('TWILIO_ACCOUNT_SID');
        $authToken = getenv('TWILIO_AUTH_TOKEN');
        $twilioNumber = getenv('TWILIO_NUMBER');

        $client = new Client($accountSid, $authToken);

        try {
            $client->messages->create(
                $to,
                [
                    "body" => $message,
                    "from" => $twilioNumber
                    //   On US phone numbers, you could send an image as well!
                    //  'mediaUrl' => $imageUrl
                ]
            );
            echo 'Message sent to ' + $twilioNumber;
        } catch (TwilioException $e) {
            echo  $e;
        }
    }
}

Next up let's see how to create a custom message.

Create a Custom Alert Message

Here we create an alert message to send out via text message.

You might also decide to include a picture with your alert message... perhaps a screenshot of the application when the crash happened?  A meme image to calm everyone down?

<?php

namespace App\Exceptions;

use Twilio\Rest\Client;
use Twilio\Exceptions\TwilioException;
use Exception;

class TwilioExceptionHandler extends Exception
{
    public function __construct()
    {
        @set_exception_handler(array($this, 'report'));
    }

    /**
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, Twilio, etc.
     *
     * @param  \Exception $e
     * @return void
     */
    public function report(Exception $e)
    {
        http_response_code(500);
        $this->notifyThroughSms($e);
        echo $e;
    }

    private function notifyThroughSms($e)
    {
        echo $e;
        foreach ($this->notificationRecipients() as $recipient) {
            $this->sendSms(
                $recipient->phone_number,
                '[This is a test] It appears the server' .
                ' is having issues. Exception: ' . $e->getMessage() .
                ' Go to http://newrelic.com for more details.'
            );
        }
    }

    private function notificationRecipients()
    {
        $adminsFile = './config/administrators.json';
        try {
            $adminsFileContents = file_get_contents($adminsFile);

            return json_decode($adminsFileContents);
        } catch (FileNotFoundException $e) {
            echo $e;
            return [];
        }
    }

    protected function sendSms($to, $message)
    {
        $accountSid = getenv('TWILIO_ACCOUNT_SID');
        $authToken = getenv('TWILIO_AUTH_TOKEN');
        $twilioNumber = getenv('TWILIO_NUMBER');

        $client = new Client($accountSid, $authToken);

        try {
            $client->messages->create(
                $to,
                [
                    "body" => $message,
                    "from" => $twilioNumber
                    //   On US phone numbers, you could send an image as well!
                    //  'mediaUrl' => $imageUrl
                ]
            );
            echo 'Message sent to ' + $twilioNumber;
        } catch (TwilioException $e) {
            echo  $e;
        }
    }
}

Let's now take a look at how to load the list of administrators.

Read the Administrators from the JSON File

We read the lucky people from our JSON file and send alert messages to each of them with the private send_message method.

<?php

namespace App\Exceptions;

use Twilio\Rest\Client;
use Twilio\Exceptions\TwilioException;
use Exception;

class TwilioExceptionHandler extends Exception
{
    public function __construct()
    {
        @set_exception_handler(array($this, 'report'));
    }

    /**
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, Twilio, etc.
     *
     * @param  \Exception $e
     * @return void
     */
    public function report(Exception $e)
    {
        http_response_code(500);
        $this->notifyThroughSms($e);
        echo $e;
    }

    private function notifyThroughSms($e)
    {
        echo $e;
        foreach ($this->notificationRecipients() as $recipient) {
            $this->sendSms(
                $recipient->phone_number,
                '[This is a test] It appears the server' .
                ' is having issues. Exception: ' . $e->getMessage() .
                ' Go to http://newrelic.com for more details.'
            );
        }
    }

    private function notificationRecipients()
    {
        $adminsFile = './config/administrators.json';
        try {
            $adminsFileContents = file_get_contents($adminsFile);

            return json_decode($adminsFileContents);
        } catch (FileNotFoundException $e) {
            echo $e;
            return [];
        }
    }

    protected function sendSms($to, $message)
    {
        $accountSid = getenv('TWILIO_ACCOUNT_SID');
        $authToken = getenv('TWILIO_AUTH_TOKEN');
        $twilioNumber = getenv('TWILIO_NUMBER');

        $client = new Client($accountSid, $authToken);

        try {
            $client->messages->create(
                $to,
                [
                    "body" => $message,
                    "from" => $twilioNumber
                    //   On US phone numbers, you could send an image as well!
                    //  'mediaUrl' => $imageUrl
                ]
            );
            echo 'Message sent to ' + $twilioNumber;
        } catch (TwilioException $e) {
            echo  $e;
        }
    }
}

Now let's look at how to send a text message.

Send a Text Message

There are three parameters needed to send an SMS using the Twilio REST API: From, To, and Body.

After the message is sent, we print out the phone number we're texting. US and Canadian phone numbers can also send an image with the message.  (Other countries will have a shortened URL appended pointing to the image.)

<?php

namespace App\Exceptions;

use Twilio\Rest\Client;
use Twilio\Exceptions\TwilioException;
use Exception;

class TwilioExceptionHandler extends Exception
{
    public function __construct()
    {
        @set_exception_handler(array($this, 'report'));
    }

    /**
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, Twilio, etc.
     *
     * @param  \Exception $e
     * @return void
     */
    public function report(Exception $e)
    {
        http_response_code(500);
        $this->notifyThroughSms($e);
        echo $e;
    }

    private function notifyThroughSms($e)
    {
        echo $e;
        foreach ($this->notificationRecipients() as $recipient) {
            $this->sendSms(
                $recipient->phone_number,
                '[This is a test] It appears the server' .
                ' is having issues. Exception: ' . $e->getMessage() .
                ' Go to http://newrelic.com for more details.'
            );
        }
    }

    private function notificationRecipients()
    {
        $adminsFile = './config/administrators.json';
        try {
            $adminsFileContents = file_get_contents($adminsFile);

            return json_decode($adminsFileContents);
        } catch (FileNotFoundException $e) {
            echo $e;
            return [];
        }
    }

    protected function sendSms($to, $message)
    {
        $accountSid = getenv('TWILIO_ACCOUNT_SID');
        $authToken = getenv('TWILIO_AUTH_TOKEN');
        $twilioNumber = getenv('TWILIO_NUMBER');

        $client = new Client($accountSid, $authToken);

        try {
            $client->messages->create(
                $to,
                [
                    "body" => $message,
                    "from" => $twilioNumber
                    //   On US phone numbers, you could send an image as well!
                    //  'mediaUrl' => $imageUrl
                ]
            );
            echo 'Message sent to ' + $twilioNumber;
        } catch (TwilioException $e) {
            echo  $e;
        }
    }
}

That's all, folks!

We've just implemented an automated server notification system that can push out server alerts if (ahem, when) anything goes wrong.  Now let's look at some other great features you might like to add.

Where to Next?

PHP and Twilio - such a great combination.  Here are just two other excellent tutorials for you to try out:

Two-Factor Authentication

Increase the security of your login system by verifying a user's mobile phone in addition to their password.

Appointment Reminders

Send your customers a text message when they have an upcoming appointment - this tutorial shows you how to do it from a background job.

Did this help?

Thanks for checking out this tutorial! Tweet @twilio to let us know what you think.