How to Send SMS Notification to Mobile Phone in Laravel 8 with Vonage (Nexmo)

Laravel 8 send sms notification to mobile phone tutorial. In this post, you will find out how to profoundly send sms messages to mobile using the Vonage (nexmo) package in the laravel app.

Vonage was previously recognized as Nexmo, and It offers straightforward SMS API that you can use to send messages to mobile phones.

Apart from SMS API, you also get powerful support for scalable voice messaging, video, and data capabilities via unified communications and impeccable communications APIs.

SMS means short message service; It is a simple method to send text messages. It was discovered around the 1980s and integrated into GSM standards in 1985. Similarly, notifications are the message which provides the vital message to the user, and they are also used for the simple reminder and keep the user engagement intact.

Send SMS Messages to Mobile in Laravel 8 with Vonage Example

  • Step 1: Install Laravel App
  • Step 2: Add Nexmo Package in Laravel
  • Step 3: Set Up Controller
  • Step 4: Create Route
  • Step 5: Send SMS Notification to Mobile in Laravel

Install Laravel App

In general, the first step begins with laravel app installation, hence use the recommended command in the terminal:

composer create-project --prefer-dist laravel/laravel demo-app

Add Nexmo Package in Laravel

Secondly, create an account at Vonage to get the nexmo API for sending the SMS messages.

You have to provide your email id and phone number for the facile signup; after that, you land into the Vonage dashboard, from here; you can get the nexmo API Key and API secret.

Also, select the PHP platform from the programming list options, also copy the function that you will need in a while for sending SMS messages in laravel.

Also, you have to copy the code key and secret from the dashboard.

How to Send SMS Notification to Phone in Laravel 8

Not only but also install the nexmo client in laravel with the help of the suggested command:

composer require nexmo/client

Set Up Controller

In the next step, create a controller hence move to console and execute the offered command:

php artisan make:controller SmsMsgController

Subsequently, open app/Http/Controllers/SmsMsgController.php file and insert the following code within:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SmsMsgController extends Controller
{
    public function sendSmsToMobile()
    {
        $basic  = new \Nexmo\Client\Credentials\Basic('Nexmo API Key', 'Nexmo API Secret');
        $client = new \Nexmo\Client($basic);
 
        $message = $client->message()->send([
            'to' => '9191234****',
            'from' => 'Herry',
            'text' => 'SMS notification sent using Vonage SMS API'
        ]);
 
        dd('SMS has sent.');
    }
}

As you can see, we have added the Vonage API code that we copied primarily associated with the PHP platform.

Create Route

Next, you have to create a new route which will make the request to invoke the sendSmsToMobile() method declared in SmsMsgController, therefore open resources/web.php file and insert the suggested code:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SmsMsgController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/


Route::get('send-sms-message', [SmsMsgController::class, 'sendSmsToMobile']);

Send SMS Notification to Mobile in Laravel

Ultimately, you need to start the laravel app and you can do it with the given below simple command:

php artisan serve

You can send SMS message to phone with below url:

http://127.0.0.1:8000/send-sms-message

Summary

This quick tutorial is ended; we have learned how to use SMS API to send sms messages to the mobile phone in laravel. Moreover, we looked at how to get nexmo sms API mainly app key and secret from Vonage dashboard and integrated into the laravel project.