How to Send SMS using Twilio in Laravel 11

Hello, laravel web developers! In this article, we'll see how to send SMS using Twilio in laravel 11. Here, we'll send an SMS to a phone number using Twillio in laravel 11. Twilio Messaging provides reliable messaging, enabling businesses to reach customers around the globe.

Twilio also provides programmable communication tools for making and receiving phone calls, sending and receiving text messages, and performing other communication functions using its web service APIs.

Here, we'll install the php twilio/sdk composer package and send SMS to specific phone numbers.

Laravel 11 Send SMS using Twilio

laravel 11 send SMS using twilio

 

Step 1: Install Laravel 11 Application

We'll install the laravel 11 application using the following command in this step.

composer create-project laravel/laravel example-app

 

Step 2: Create a Twilio Account

Next, we'll create a Twilio account. get Twilio credentials from the Twilio dashboard. Head over to your dashboard and grab your account_sid and auth_token. Then, navigate to the Phone Number section to get your SMS enabled phone number.

.env

TWILIO_SID=XXXXXXXXXXXXXXXXX
TWILIO_TOKEN=XXXXXXXXXXXXX
TWILIO_FROM=+XXXXXXXXXXX

 

Step 3: Install twilio/sdk Package

Then, we'll install twilio/sdk composer package using the following command.

composer require twilio/sdk

 

 

Step 4: Define Route

Now, we'll define the routes into the web.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\TwilioSMSController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('sendSMS', [TwilioSMSController::class, 'index']);

 

Step 5: Create Controller

Next, we'll create a controller and add the Twilio SMS send code.

app/Http/Controllers/TwilioSMSController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Exception;
use Twilio\Rest\Client;
  
class TwilioSMSController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $receiverNumber = "RECEIVER_NUMBER";
        $body = "This is test message from Techsolutionstuff";
  
        try {
  
            $account_sid = getenv("TWILIO_SID");
            $auth_token = getenv("TWILIO_TOKEN");
            $twilio_number = getenv("TWILIO_FROM");
  
            $client = new Client($account_sid, $auth_token);
            $client->messages->create($receiverNumber, [
                'from' => $twilio_number, 
                'body' => $body
            ]);
  
            dd('SMS Sent Successfully.');
  
        } catch (Exception $e) {
            dd("Error: ". $e->getMessage());
        }
    }
}

 

Step 6: Run the Laravel 11 Application

Now, run the laravel 11 application using the following command.

php artisan serve

 


You might also like :

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS