Laravel 10 Send SMS using Twilio Tutorial Example

By Hardik Savani November 5, 2023 Category : Laravel

Hello Dev,

I will explain step by step tutorial send sms using twilio in laravel 10. I’m going to show you about laravel 10 - twilio sms notifications. step by step explain laravel 10 sms notification twilio. If you have a question about laravel 10 send sms to mobile with twilio then I will give a simple example with a solution.

Twilio is a cloud communications platform that allows developers to add various communication channels, such as voice, SMS, and video, to their applications. The company was founded in 2008 and is headquartered in San Francisco, California. Twilio's platform provides Application Programming Interfaces (APIs) that developers can use to integrate various communication channels into their applications. This allows developers to easily create applications that can send and receive SMS messages, make and receive phone calls, and more.

In this example, we will simply install twilio/sdk composer package and send sms to specific number using Twilio API and Secret Key.

let's follow bellow steps:

Step 1: Install Laravel

first of all we need to get fresh Laravel version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Twilio Account

First you need to create and add phone number. then you can easily get account SID, Token and Number.

Create Account from here: www.twilio.com.

Next add Twilio Phone Number

Next you can get account SID, Token and Number and add on .env file as like bellow:

.env

TWILIO_SID=XXXXXXXXXXXXXXXXX

TWILIO_TOKEN=XXXXXXXXXXXXX

TWILIO_FROM=+XXXXXXXXXXX

Step 3: Install twilio/sdk Package

In this step, we need to install twilio/sdk composer package to use twilio api. so let's run bellow command:

composer require twilio/sdk

Step 4: Create Route

now we will create one route for calling our example, so let's add new route to web.php file as bellow:

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

in this step, we will create TwilioSMSController and write send sms logic, so let's add new route to web.php file as bellow:

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";

$message = "This is testing from ItSolutionStuff.com";

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' => $message]);

dd('SMS Sent Successfully.');

} catch (Exception $e) {

dd("Error: ". $e->getMessage());

}

}

}

Now you can run and check.

I hope it can help you...

Shares