Laravel 10 Send SMS to Mobile using Nexmo Tutorial Example

Laravel 10 Send SMS to Mobile using Nexmo Tutorial Example

If want to send text sms to mobile or phone from your Laravel web application. So from this tutorial, you will learn how to send sms text notifications to mobile using Nexmo in Laravel 10 web apps.

Laravel 10 Send SMS to Mobile using Nexmo Tutorial Example

By using the following steps, you can send text sms to mobile using Nexmo in laravel 10 apps:

  • Step 1 – Installing Laravel 10 Application
  • Step 2 – Configure Nexmo and Database to App
  • Step 3 – Installing nexmo Package
  • Step 4 – Adding Routes
  • Step 5 – Creating Controller By Artisan Command
  • Step 6 – Run Development Server

Before starting, If you do not have a Nexmo account, first create an account by clicking on this link https://dashboard.nexmo.com/sign-in.

Step 1 – Installing Laravel 10 Application

Firstly, Open your terminal or command prompt.

Then execute the following command to download or install Laravel 10 new setup into your server:

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

Step 2 – Configure Nexmo and Database to App

In this step, Configure database with your apps.

So, visit your app root directory and find .env file. Then configure database details as follows:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database-name
DB_USERNAME=database-user-name
DB_PASSWORD=database-password

Then add nexmo client id and secret in .env file:

NEXMO_KEY=XXXXX
NEXMO_SECRET=XXXXXXXXXXX

Step 3 – Installing nexmo Package

In this step, open again your command prompt or terminal.

Then execute the following command into it to install nexmo sms package:

composer require nexmo/client

Step 4 – Adding Routes

In this step, Visit your routes directory and open web.php file in any text editor.

And then add the following routes into the web.php file:

  use App\Http\Controllers\NexmoSMSController;

  Route::get('send-sms', [NexmoSMSController::class, 'index']);

Step 5 – Creating Controller By Artisan Command

In this step, execute the following command on the terminal/command prompt/command line to create controller file for your Laravel applications; is as follow:

php artisan make:controller NexmoSMSController

Now, visit your laravel directory app/http/controllers and open NexmoSMSController.php file.

And add the following code into it:

<?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());
        }
    }
}

Step 6 – Run Development Server

Finally, open the command prompt and run the following command to start development server:

php artisan serve

Then open your browser and hit the following url on it:

http://127.0.0.1:8000/send-sms

Conclusion

That’s it. In this tutorial, you have learned how to send sms text notifications to mobile using Nexmo in Laravel 10 web apps.

Recommended Laravel Posts

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *