This comprehensive guide will show you how to use the Vonage (nexmo) package in a Laravel application to send an SMS notice to a mobile phone.
The Laravel ecosystem has several fantastic options for sending notification messages, and this laravel notification tutorial will show you how to use the Nexmo SMS API to deliver SMS messages.
For sending messages to phones, we use the Nexmo SMS API; this package provides global communication support with a top-notch configurable solution. Not only that, but it also includes:
- Support for scalable voice messaging that is both robust and simple.
- Unified Communications has video and data capabilities.
- APIs for communication
Laravel 9 Send SMS Notification or Messages to Phone Example
Table of Content
- Step 1: Create Laravel Project
- Step 2: Install Nexmo SMS Package
- Step 3: Generate and Configure Controller
- Step 4: Set up Route
- Step 5: Run Laravel App
Step 1 - Create Laravel Project
If you haven't already done so, go to the terminal screen and execute the composer command with the create-project flag to build a new Laravel application:
composer create-project --prefer-dist laravel/laravel cyber
After the installation is complete, go to the root of the project:
Step 2 - Setting Up Vonage (Nexmo) SMS Package
Vonage (previously known as Nexmo) is a well-known communication service provider; Laravel integrates seamlessly with Vonage and streamlines the SMS-to-phone procedure. Following that, we must create a new Vonage account and have sufficient API access.
You must fill in your name and email address to create a Voyage account; once you have completed the account creation process, you can access the SMS API dashboard.
It will first credit some funds to your account, which you may use to send SMS to your phone. You will also need to select the programming language in which you will construct the SMS sending app.
Also, use the composer command to install the nexmo client:
composer require nexmo/client
You'll need the Nexmo key and secret to build consensus between the Laravel app and Vonyage client. As a result, go to the left sidebar and click the Getting started link. Also, from the Vonyage API dashboard, copy both the key and the secret.
Step 3 - Generate and Configure Controller
Now, using the compoer command, create a new controller:
php artisan make:controller NotificationController
Because the preceding operation created a controller, open the file app/Http/Controllers/NotificationController.php
and add the following code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NotificationController extends Controller
{
public function sendSmsNotificaition()
{
$basic = new \Nexmo\Client\Credentials\Basic('Nexmo key', 'Nexmo secret');
$client = new \Nexmo\Client($basic);
$message = $client->message()->send([
'to' => '91**********',
'from' => 'P**********',
'text' => 'Test Message sent from Vonage SMS API'
]);
dd('SMS message has been delivered.');
}
}
Add both the credentials to send the sms to the mobile phone to the $basic property, which holds the nexmo app key and secret.
Step 4 - Set up Route
Create a new route to make the GET request for sending an SMS message to a mobile phone. In the resources/web.php
file, import the controller and build a route with the route get method:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NotificationController;
Route::get('send-sms-notification', [NotificationController::class, 'sendSmsNotificaition']);
Step 5 - Run Laravel App
The laravel application must be started. So, from the console, run the PHP artisan serve command, which will start the PHP development server and allow you to test the feature you just created:
php artisan serve
The app can be tried on the following devices:
http://127.0.0.1:8000/send-sms-notification
Conclusion
The Laravel Send SMS to Mobile tutorial is now complete. This detailed tutorial demonstrated how to deliver SMS messages to a mobile phone using the Nexmo package in a Laravel app.
Not only that, but we also demonstrated how to set up a Nexmo account and utilise the Nexmo plugin to deliver SMS messages to mobile devices in the Laravel environment.
I hope you will like the content and it will help you to learn Laravel 9 Send SMS Notification to Mobile Phone Tutorial
If you like this content, do share.