Laravel 8 Send SMS to Mobile with Nexmo Example
Laravel 8 send sms using nexmo example. In this tutorial, you will learn how to send sms notification to mobile using nexmo in laravel 8 app.
This tutorial will guide you step by step on how to send send sms to mobile with nexmo in laravel 8 app. Now, You need to follow the some step to done laravel nexmo message.
First of all, visit the following link https://dashboard.nexmo.com/sign-in and create nexmo account. Get client id and secret from nexom account.
How to Send SMS Messages in Laravel 8
- Step 1 – Download Laravel 8 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 8 Application
First of all download or install laravel 8 new setup. So, open terminal and type the following command to install new laravel 8 app into your machine:
composer create-project --prefer-dist laravel/laravel blog
Step 2 – Connecting App to Database
In this step, setup database with your downloded/installed laravel 8 app. So, you need to find .env file and setup database details as following:
1 2 3 4 5 6 | DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=database-nameDB_USERNAME=database-user-nameDB_PASSWORD=database-password |
And add nexmo client id and secret in .env file:
1 2 | NEXMO_KEY=XXXXXNEXMO_SECRET=XXXXXXXXXXX |
Step 3 – Install SMS Pakcage
In this step, open again your command prompt and execute the following command on it. To install nexmo sms package:
composer require nexmo/clientThen 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:
1 2 3 4 | use App\Http\Controllers\NexmoSMSController;Route::get('send-sms', [NexmoSMSController::class, 'index']); |
Step 5 – Create Controller By Artisan Command
In this step, 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <?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
Finally, 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

0 comments:
Post a Comment
Thanks