Sending SMS Using Twilio in Laravel

In this tutorial, we will see how to send SMS messages using Twilio in Laravel 10. Twilio is a powerful cloud communication platform that enables developers to integrate SMS and voice capabilities into their applications.

By the end of this tutorial, you will have a clear understanding of how to leverage Twilio’s API to send SMS messages in your Laravel application.

Before we begin, you need to create a Twilio account if you don’t have one already. Simply visit the Twilio website and sign up for an account. Once you’re registered, you’ll have access to your Twilio Account SID and Auth Token, which we will use later in the tutorial.

Install the Twilio PHP SDK

Twillo provides packages to interact with Twilio’s API, we need to install the Twilio PHP SDK. Open your terminal and navigate to your Laravel project’s root directory.

Run the following command to install the Twilio PHP SDK using Composer:

composer require twilio/sdk

The composer will automatically fetch and install the required packages for you.

Configure Twilio Credentials in Laravel

After installation, we need to configure our Twilio credentials in Laravel. Open the .env file in your project’s root directory and add the following lines:

TWILIO_SID=your_twilio_account_sid
TWILIO_AUTH_TOKEN=your_twilio_auth_token
TWILIO_PHONE_NUMBER=your_twilio_phone_number

Here, Replace your_twilio_account_sid, your_twilio_auth_token, and your_twilio_phone_number with your actual Twilio credentials.

Now, open config/app.php file and add the below lines at last:

...
'twilio_sid' => env('TWILIO_SID'),
'twilio_auth_token' => env('TWILIO_AUTH_TOKEN'),
'twilio_phone_number' => env('TWILIO_PHONE_NUMBER'),

Creating a Controller and a Route

Here, we will take a simple example where we will send an SMS message to a single user statically.

Let’s create a new controller that will handle sending SMS messages. Run the following command in your terminal to generate a new controller:

php artisan make:controller SMSController

Open the SMSController.php file located in the app/Http/Controllers directory. Add the following method to send an SMS:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Twilio\Rest\Client;

class SMSController extends Controller
{
    public function sendSMS()
    {
        $twilioSid = config('app.twilio_sid');
        $twilioToken = config('app.twilio_auth_token');
        $twilioPhoneNumber = config('app.twilio_phone_number');

        $client = new Client($twilioSid, $twilioToken);

        $client->messages->create(
            'recipient_phone_number',
            [
                'from' => $twilioPhoneNumber,
                'body' => 'Hello from Twilio!'
            ]
        );

        return "SMS sent successfully!";
    }
}

Here, we have created a function for sending SMS to the user. Make sure to replace recipient_phone_number with the actual phone number you want to send the SMS to.

Now, let’s define a route that will trigger the sendSMS() method. Open the web.php file located in the routes directory and add the following route:

Route::get('/send-sms', [SMSController::class, 'sendSMS']);

Testing SMS Sending Functionality

After creating a route, you need to start your Laravel development server by running the following command in your terminal:

php artisan serve

Once the server starts, open your browser and navigate to http://127.0.0.1:8000/send-sms. If everything is set up correctly, you should see the “SMS sent successfully!” message, and the recipient phone number should receive the SMS with the message “Hello from Twilio!”

Conclusion

In this tutorial, we learned how to send SMS messages using Twilio in Laravel 10. We covered the initial setup of a Twilio account, installing the Twilio PHP SDK via Composer, configuring Twilio credentials in Laravel, creating a controller and route, and testing the SMS sending functionality.

Here, we have just taken a simple example to send a message to one user statically. However, you can customize it for getting phone numbers from the table and send them SMS messages using Twillo or as per your requirements.