Thursday, November 21, 2024
Google search engine
HomeLanguagesLaravel 8 Send SMS to Mobile with Nexmo Example

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:

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

In this step, 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

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:

<?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

Recommended Laravel Posts

RELATED ARTICLES

Most Popular

Recent Comments