Laravel 9 - Send SMS using Vonage/Nexmo
Laravel 9 - Send SMS using Vonage/Nexmo
In this tutorial, we’ll cover sending SMS or notifications in Laravel using Vonage, enabling reliable communication like alerts, notifications, and verification codes.
If you're a video person, feel free to skip the post and check out the video instead!
Step # 1 : Create fresh Laravel project or use an existing Laravel project.
Two commands to create fresh Laravel project.
Global Command : laravel new vonage
Or use
Non Global Command : composer create-project laravel/laravel --prefer-dist vonage
Step # 2 : Access the project.
Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
Git Bash : cd c:xampp/htdocs/vonage
Run the Laravel Vite development server. Install the required dependencies and start the Vite server for front-end assets.
Command : npm install && npm run dev
Open a new Git Bash window or tab, and navigate to the same project directory to run further Laravel commands.
Step # 3 : Create account on Vonage/Nexmo.
Create an account on Vonage by visiting Vonage Communications APIs.
link : https://www.vonage.com/communications-apis/
After confirming your email, you will have £2 in your account for testing.
Click on "Getting Started" and select "Send an SMS." Choose PHP as your programming language during the setup to find your credentials.
Step # 4 : Install the Vonage/Nexmo package.
Command : composer require vonage/client
Step # 5 : Create a controller.
Command : php artisan make:controller SmsController
To send an SMS, update your SmsController with the code below.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SmsController extends Controller
{
public function sms()
{
// Retrieve Vonage credentials from environment variables
$basic = new \Vonage\Client\Credentials\Basic(env('VONAGE_API_KEY'), env('VONAGE_API_SECRET'));
$client = new \Vonage\Client($basic);
// Replace "recipient_phone_number" with the actual phone number you want to send the SMS to
$response = $client->sms()->send(
new \Vonage\SMS\Message\SMS("recipient_phone_number", 'All About Laravel', 'A text message sent using the Nexmo SMS API')
);
$message = $response->current();
// Check if the message was sent successfully
if ($message->getStatus() == 0) {
echo "The message was sent successfully\n";
} else {
echo "The message failed with status: " . $message->getStatus() . "\n";
}
}
}
Step # 6 : Create a route.
Import the SmsController class.
use App\Http\Controllers\SmsController;
Add a route.
Route::get('/sms', [SmsController::class, 'sms']);
Step # 7 : Define the keys in .env file.
VONAGE_API_KEY=your_api_key
VONAGE_API_SECRET=your_api_secret
Using environment variables is a more secure and manageable approach, especially for production environments.
Step # 8 : It's time to test.
Command : php artisan server
Visit url 127.0.0.1:8000/sms
If you have followed the steps correctly, you should see the message
"The message was sent successfully"
And you will be able to find the Text message in the Logs -> SMS Logs.
Share this with friends!
To engage in commentary, kindly proceed by logging in or registering
Subscribe to Our Newsletter
Stay ahead of the curve! Join our newsletter to see what everyone’s talking about.
0 Comments