How to Send SMS Using Nexmo in Laravel?

Sep 20, 2022 . Admin



Hello Friends,

Here, I will show you how to work how to send sms using nexmo in laravel. I would like to share with you laravel send sms to mobile with nexmo. you'll learn laravel sms notification nexmo. Here you will learn to send sms using nexmo in laravel.

In this example, I will give you a very simple example of sending SMS using nexmo/vonage API in laravel app. you can easily use this code in laravel 6, laravel 7, laravel 8 and laravel 9 app.

You have just to follow the below step and you will get the layout as below:

Step 1: Install Laravel

first of all we need to get a fresh Laravel version application using the bellow command, So open your terminal OR command prompt and run the bellow command:

composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Nexmo Account

First you need to create an account on nexmo. then you can easily get the client id and secret.

Create Account from here: https://dashboard.nexmo.com/sign-in.

you can register and get the client id and secret as below:

Add on env file as like bellow:

.env
NEXMO_KEY=XXXXX
NEXMO_SECRET=XXXXXXXXXXX
Step 3: Install nexmo/client Package

In this step, we need to install nexmo/client composer package to use nexmo API. so let's run below command:

composer require nexmo/client
Step 4: Create Route

now we will create one route for calling our example, so let's add a new route to the web.php file as below:

routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\NexmoSMSController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('sendSMS', [NexmoSMSController::class, 'index']);
Step 5: Create Controller

in this step, we will create NexmoSMSController and write send SMS logic, so let's add a new route to the web.php file as below:

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

Now you can run and check.

I hope it can help you...

#Laravel