Laravel 10 - Twilio Send SMS using Twilio Tutorial Example Tutorial

Sovary January 6, 2024 396
3 minutes read

I will guide you through a step-by-step tutorial on how to send SMS using Twilio in Laravel 10. Additionally, I will demonstrate how to implement Twilio SMS notifications in Laravel 10, explaining each step thoroughly. If you have any inquiries regarding sending SMS to mobile using Twilio in Laravel 10, I will provide a simple example with a solution to address your concerns.

Founded in 2008 and based in San Francisco, California, Twilio is a leading cloud communications platform. It provides developers with the ability to enhance their applications by integrating various communication channels, such as voice, SMS, and video. Twilio's Application Programming Interfaces (APIs) serve as a bridge, allowing developers to easily incorporate these communication channels into their applications. This empowers developers to effortlessly create applications that can send and receive SMS messages, make and receive phone calls, and offer a wide range of other communication functionalities.

Laravel 10 - Twilio Send SMS using Twilio Tutorial Example Tutorial

Following the steps below to completed the task

  • Step 1 - Install Laravel App
  • Step 2 - Create Twilio Account
  • Step 3 - Install Twilio Package
  • Step 4 - Add View (Blade File)
  • Step 5 - Create Controller
  • Step 6 - Adding Route
  • Step 7 - Run Laravel App

Step 1 - Install Laravel App

Suppose you have installed Laravel framework. If you are not yet, please read this to know how to install Laravel project.

Step 2 - Create Twilio Account

Firstly we have to create and add a phone number and you will obtain the account SID, Token, and Number. You can create an account by visiting the following website: www.twilio.com. Next, add a Twilio phone number. Finally, retrieve the account SID, Token, and Number, and add them to the .env file as shown below:

TWILIO_SID=XXXXXXXXXXXXXXXXX
TWILIO_TOKEN=XXXXXXXXXXXXX
TWILIO_FROM=+XXXXXXXXXXX

Step 3 - Install Twilio Package

In this step, we will install twilio/sdk composer package to use Twilio API. Let's run below command:

composer require twilio/sdk

Step 4: Create Controller

We are going to create TwilioController and write some logic that we can send sms. Let's run below command to generate controller file

php artisan help make:controller TwilioController

Open file app -> Http -> Controllers -> TwilioController.php

<?php
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Twilio\Rest\Client;
use Exception;
  
class TwilioController extends Controller
{
    public function index()
    {
        $receiverNumber = "RECEIVER_NUMBER";
        $message = "This is your OTP password xxxx do not share with other.";
        try 
        {
            $acc_sid = getenv("TWILIO_SID");
            $auth_token = getenv("TWILIO_TOKEN");
            $twilio_number = getenv("TWILIO_FROM");
            $client = new Client($acc_sid, $auth_token);
            $client->messages->create($receiverNumber, [
                'from' => $twilio_number, 
                'body' => $message]);
            dd('SMS Sent');
        } catch (Exception $e) 
        {
            dd("Error: ". $e->getMessage());
        }
    }
}

Step 5: Add Route

We have create controller file then we have to match URL with class controller in route file. so let's add new route to web.php file as bellow:

Open file routes -> web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\TwilioController;
  
/*
|--------------------------------------------------------------------------
| 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('/send', [TwilioController::class, 'index']);

Step 6 - Run Laravel App

Finally, we can run artisan command bellowing we are going to start the Laravel server with following command.

php artisan serve

Go to URL http://localhost:8000/ will making request and send to the receiver phone number.

Hope it would help have a nice day

You might also like

Laravel  PHP  Laravel 10 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him