ItSolutionStuff.com

Laravel Send SMS to Mobile with Nexmo Example

By Hardik Savani • April 16, 2024
Laravel

Hi,

This tutorial shows you laravel send sms to mobile with nexmo. i would like to share with you how to send sms using nexmo in laravel. This article will give you simple example of send sms using nexmo in laravel. step by step explain laravel sms notification nexmo. You just need to some step to done laravel nexmo message.

In this example, i will give you very simple example to sending sms using nexmo/vonage api in laravel app. you can easily use this code in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 app.

let's follow bellow steps:

Step 1: Install Laravel

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

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Nexmo Account

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

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

you can register and get client id and secret as like bellow:

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 bellow command:

composer require nexmo/client

Step 4: Create Route

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

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 new route to web.php file as bellow:

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 ItSolutionStuff.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...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Livewire Click Event Example

Read Now →

Laravel Livewire Delete Confirmation Example

Read Now →

How to Get Last 30 Days Record in Laravel?

Read Now →

Laravel Telescope Installation and Configuration Tutorial

Read Now →

Laravel Carbon Check Date is in Past Date Code

Read Now →

Laravel Carbon Get Current Date Time Example

Read Now →

Laravel Send Mail using Mailgun Example

Read Now →

Laravel 8 Resize Image Before Upload Example

Read Now →

Laravel 7/6 Highcharts Tutorial

Read Now →

Laravel Create JSON File & Download From Text Example

Read Now →