Raviya Technical
Published in

Raviya Technical

Laravel with Packages | Laravel Send SMS to Mobile with Nexmo Example

composer create-project --prefer-dist laravel/laravel blog
NEXMO_KEY=XXXXXNEXMO_SECRET=XXXXXXXXXXX
composer require nexmo/client
<?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']);
<?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());
}
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store