Twilio provides PHP SDK that will help the developers to send SMS or messages to the users WhatsApp number from the Laravel 11 application.
Let’s start building an application to send WhatsApp messages to WhatsApp number using Laravel
Step 1 – Set up a Twilio Account
Open browser and type https://www.twilio.com/
, and create account to get secret id and key for whatsapp messages.
And edit .env
file and set up twilio account id and key in it:
TWILIO_SID=your_twilio_account_sid
TWILIO_AUTH_TOKEN=your_twilio_auth_token
TWILIO_WHATSAPP_NUMBER=your_twilio_whatsapp_number
Step 2 – Install twilio/sdk Package
Run the following composer command to install twilio PHP sdk for this application:
composer require twilio/sdk
Step 3 – Define Routes
Edit routes/web.php
file, and define routes in it to handle send sms requests in application:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\WhatsAppController;
Route::get('whatsapp', [WhatsAppController::class, 'index']);
Route::post('whatsapp', [WhatsAppController::class, 'store'])->name('whatsapp.post');
Step 4 – Create Controller
Run the following command to create a controller file named WhatsAppController.php:
php artisan make:controller WhatsAppController
Edit app/Http/Controllers/WhatsAppController.php
file, and implement send whatsapp sms functionality using twilio package:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Twilio\Rest\Client;
use Exception;
class WhatsAppController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
return view('whatsapp');
}
/**
* Write code on Method
*
* @return response()
*/
public function store(Request $request)
{
$twilioSid = env('TWILIO_SID');
$twilioToken = env('TWILIO_AUTH_TOKEN');
$twilioWhatsAppNumber = env('TWILIO_WHATSAPP_NUMBER');
$recipientNumber = $request->phone;
$message = $request->message;
try {
$twilio = new Client($twilioSid, $twilioToken);
$twilio->messages->create(
$recipientNumber,
[
"from" => "whatsapp:+". $twilioWhatsAppNumber,
"body" => $message,
]
);
return back()->with(['success' => 'WhatsApp message sent successfully!']);
} catch (Exception $e) {
return back()->with(['error' => $e->getMessage()]);
}
}
}
Step 5 – Create Blade File
Go to resources/views
directory and create a file named whatsapp.blade.php
, and then create a form in it to send whatsapp sms from laravel application blade view:
<!DOCTYPE html>
<html>
<head>
<title>Laravel 11 Send WhatsApp SMS Example - ItCodStuff.com</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="card">
<div class="card-header">
<h2>Laravel 11 Send WhatsApp SMS Example - ItCodStuff.com</h2>
</div>
<div class="card-body">
<form method="POST" action="{{ route('whatsapp.post') }}">
{{ csrf_field() }}
@if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<strong>{{ $message }}</strong>
</div>
@endif
@if ($message = Session::get('error'))
<div class="alert alert-danger alert-block">
<strong>{{ $message }}</strong>
</div>
@endif
<div class="mb-3">
<label class="form-label" for="inputName">Phone:</label>
<input
type="text"
name="phone"
id="inputName"
class="form-control @error('phone') is-invalid @enderror"
placeholder="Phone Number">
@error('phone')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<label class="form-label" for="inputName">Message:</label>
<textarea
name="message"
id="inputName"
class="form-control @error('message') is-invalid @enderror"
placeholder="Enter Message"></textarea>
@error('message')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<button class="btn btn-success btn-submit">Send Message</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Step 6 – Test This Application
Run the following command to start application server:
php artisan serve
Type http://localhost:8000/whatsapp
url on browser to test this application.