Laravel 11 Send SMS using Twilio Tutorial Example
In this tutorial, I will show you how to send text SMS to a mobile number using Twilio API in the laravel 11 application.
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. you just need to follow the below steps to done example.
Step for Laravel 11 Send SMS to Mobile Number using Twilio API
- Step 1: Install Laravel 11
- Step 2: Create Twilio Account
- Step 3: Install twilio/sdk Package
- Step 4: Create Route
- Step 5: Create Controller
- Run Laravel App
Step 1: Install Laravel 11
This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
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());
}
}
}
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/sendSMS
Now you can run and check.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 11 Import Large CSV File into Database Example
- Laravel 11 Custom User Login and Registration Tutorial
- Laravel 11 Generate PDF and Send Email Tutorial
- Laravel 11 Install Tailwind CSS Step by Step
- Laravel 11 JQuery Load More Data on Scroll Example
- Laravel 11 Notification | Send Notification in Laravel 11
- Laravel 11 Custom Validation Error Message Example
- Laravel 11 User Roles and Permissions Tutorial
- Laravel 11 Livewire Wizard Multi Step Form Tutorial
- Laravel 11 Livewire Pagination Tutorial Example
- Laravel 11 Livewire Form Validation Example
- How to Create Custom Validation Rules in Laravel 11?