Laravel 10/9 send sms using nexmo example; Through this tutorial, i am going to show you how to send sms notification to mobile using nexmo in Laravel 10/9 app.
What is Nexmo? Nexmo connects your apps directly to carriers around the world. Integrate SMS and Voice messages using one simple API. Use Nexmo Verity to register users, verify transactions, and implement two factor authentication. Nexmo is a tool in the Voice and SMS category of a tech stack.
Create nexmo account; so visit this link https://dashboard.nexmo.com/sign-in. Get client id and secret from nexom account.
How to Send SMS Messages in Laravel 10/9
Use below given steps to send sms messages in Laravel 10/9 apps:
- Step 1 – Download Laravel 10/9 Application
- Step 2 – Connecting App to Database
- Step 3 – Install SMS Package
- Step 4 – Create Route
- Step 5 – Create Controller By Artisan Command
- Step 6 – Run Development Server
Step 1 – Download Laravel 10/9 Application
Now open terminal and type the following command to install new Laravel 10/9 app into your machine:
composer create-project --prefer-dist laravel/laravel blog
Step 2 – Connecting App to Database
Setup database with your downloded/installed Laravel 10/9 app. So, you need to find .env file and setup database details as following:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database-name DB_USERNAME=database-user-name DB_PASSWORD=database-password
And add nexmo client id and secret in .env file:
NEXMO_KEY=XXXXX NEXMO_SECRET=XXXXXXXXXXX
Step 3 – Install SMS Pakcage
Open again your command prompt and execute the following command on it. To install nexmo sms package:
composer require nexmo/client
Then visit nexmo website and create nexmo account from here : https://www.nexmo.com. When you will be done account creation. This will give you the app id and secret key.
Step 4 – Create Routes
In this step, open web.php file from routes direcotry. And update the following routes into web.php file:
use App\Http\Controllers\NexmoSMSController; Route::get('send-sms', [NexmoSMSController::class, 'index']);
Step 5 – Create Controller By Artisan Command
Run the following command on command prompt to create controller file:
php artisan make:controller NexmoSMSController
After that, go to app/http/controllers and open NexmoSMSController.php file. And update the following code into it:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Exception; class NexmoSMSController extends Controller { /** * Write code on Method * * @return response() */ public function index() { try { $basic = new \Nexmo\Client\Credentials\Basic(getenv("NEXMO_KEY"), getenv("NEXMO_SECRET")); $client = new \Nexmo\Client($basic); $receiverNumber = "91846XXXXX"; $message = "This is testing from ItSolutionStuff.com"; $message = $client->message()->send([ 'to' => $receiverNumber, 'from' => 'Vonage APIs', 'text' => $message ]); dd('SMS Sent Successfully.'); } catch (Exception $e) { dd("Error: ". $e->getMessage()); } } }
Step 6 – Run Development Server
Open command prompt and run the following command to start developement server:
php artisan serve
Then open your browser and hit the following url on it:
http://127.0.0.1:8000/send-sms
Be First to Comment