Laravel 10 Send SMS Notification to Mobile Phone Tutorial

Last Updated on by in Laravel

Throughout this comprehensive tutorial, you will find out how to send an SMS notification to mobile phone with the help of Vonage (nexmo) package in laravel application.

Laravel ecosystem offers sublime features to send notification messages, and this laravel notification tutorial helps you send sms messages using nexmo sms api.

We are using Nexmo SMS API for sending messages to phone; this package offers global communication support with the top-notch programmable solution. Not just that, it also provides:

  • Robust and straightforward assistance for scalable voice Messaging
  • Video and data capabilities across Unified Communications
  • Communications APIs

Laravel 10 Send SMS Notification or Messages to Phone Example

  • Step 1: Create Laravel Project
  • Step 2: Install Nexmo SMS Package
  • Step 3: Generate and Configure Controller
  • Step 4: Set up Route
  • Step 5: Run Laravel App

Create Laravel Project

If you haven’t installed the Laravel application yet, then head over to console screen, use the composer command with a create-project flag to create the brand new laravel app:

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

Right after the installation process is over, get inside the project’s root:

Setting Up Vonage (Nexmo) SMS Package

Vonage (formerly known as Nexmo) is the notable communication service provider, Laravel works smoothly with Vonage and makes the SMS sending to phone process smooth. Thereupon, we need to register a new account with Vonage and get the proper API access.

Creating a Voyage account is relatively simple, you need to fill in your name and email; once you complete the account creating process right after that, you can access the SMS API dashboard.

Initially, It will credit some balance in your account that you can use to send SMS to your phone, you also have to choose the programming language on which you are developing the sms sending app.

Also, install the nexmo client using the composer command:

composer require nexmo/client

To establish the consensus between the laravel app and Vonyage client, you need to have the Nexmo key and secret. Hence, click on the Getting started link from the left sidebar. And, copy both key and secret from the Vonyage API dashboard.

Laravel Send SMS Notification to Mobile

Generate and Configure Controller

Now, generate a new controller using the compoer command:

php artisan make:controller NotificationController

Execution of the previous command generated a controller, hence open and add the provided code in app/Http/Controllers/NotificationController.php file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class NotificationController extends Controller
{
    public function sendSmsNotificaition()
    {
        $basic  = new \Nexmo\Client\Credentials\Basic('Nexmo key', 'Nexmo secret');
        $client = new \Nexmo\Client($basic);
 
        $message = $client->message()->send([
            'to' => '9197171****',
            'from' => 'John Doe',
            'text' => 'A simple hello message sent from Vonage SMS API'
        ]);
 
        dd('SMS message has been delivered.');
    }
}

The $basic property holds the nexmo app key and secret so add both the credentials here to send the sms to mobile phone.

Set up Route

To make the GET request for sending an SMS message to mobile, make a new route. Import controller and create a route with route get method in resources/web.php file:

<?php

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

Route::get('send-sms-notification', [NotificationController::class, 'sendSmsNotificaition']);

Run Laravel App

You need to start the laravel application. So, invoke the PHP artisan serve command from the console, it lets you evoke the PHP development server, and you can test the feature you have just created:

php artisan serve

The app can be started similarly tested on:

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

Conclusion

The Laravel Send SMS messages to Mobile tutorial is over. This extensive guide explained bit by bit how to send SMS messages to Mobile phone in Laravel app using the Nexmo package.

Not just that, we shared how to create nexmo account and use this plugin to send sms messages to mobile in laravel ecosystem.