Example Of Sending SMS To Mobile With Nexmo In Laravel

Hi Artisan,

In this tutorial, I will show you laravel send sms to mobile with nexmo. You know that sometimes we need to create opt verification system in laravel or we need to send bulk sms option in Laravel application.

So I am here to share with you how to send sms using nexmo in laravel. This laravel nexmo will give you simple example of send sms using nexmo in laravel. I will show you step by step so that you can understand laravel sms notification nexmo.

You just need to follow some steps to done laravel nexmo message. In this example, I will give you a very simple example of sending sms using nexmo/vonage api in laravel app.

let's follow below steps:

 

Step 1: Install Laravel

In the first step of how to integrate sms gateway in laravel tutorial, we need to download a fresh laravel project. So download it via below command.

composer create-project laravel/laravel laravel_nexmo

 

Step 2: Create Nexmo Client ID

In this step, we need a nexmo account to get client credentials to get its features. Create an account from here: Nexmo.

Add update your .env file as like bellow:

.env

NEXMO_KEY=XXXXX
NEXMO_SECRET=XXXXXXXXXXX

 

Step 3: Install nexmo/client Package

In this step, we have to install nexmo/client composer package to use nexmo api. So let's run below command to install this package.

composer require nexmo/client

 

Step 4: Create Route

Almost set to go, just we will create one route for creating our sending receiving sms laravel nexmo example:

routes/web.php

Route::get('sendSMS', [NexmoSMSController::class, 'index']);

 

Step 5: Create Controller

in this fifth step, we will create NexmoSMSController and write login to send sms using nexmo.

app/Http/Controllers/NexmoSMSController.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 your favorite codecheef";
  
            $message = $client->message()->send([
                'to' => $receiverNumber,
                'from' => 'Vonage APIs',
                'text' => $message
            ]);
  
            dd('SMS Sent Successfully.');
              
        } catch (Exception $e) {
            dd("Error: ". $e->getMessage());
        }
    }
}

 

Now all are set to go. So you can check it. hope it can help you.

 

#laravel #laravel-8x #laravel-nexmo #laravel-sms-gateway