Laravel 9 - Send SMS using Vonage/Nexmo

Touseef Afridi
01 Sep 24

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!


Quick Overview

In this guide, we walk you through integrating Vonage SMS functionality into a Laravel 9 application. We begin by setting up a fresh Laravel project and configuring the development environment with Vite. Next, we create a free Vonage account to obtain test credits and API credentials. After installing the official Vonage PHP SDK, we build a dedicated SmsController to handle the message sending logic using secure .env variables. The controller connects to Vonage, sends a test SMS, and displays a status message based on the response. This allows you to validate the integration in a straightforward way. We then define a simple route to trigger the SMS, and test the setup by accessing the URL in the browser. If successful, you'll see a confirmation message and the SMS will appear in your Vonage Dashboard under SMS Logs.

Step # 1 : Initialize Your Laravel 9 Application.

To begin integrating Vonage into your Laravel project, you’ll need a working Laravel 9 setup. You can either start with a fresh installation or continue with your existing project. To create a new Laravel project using the Laravel installer, run.
laravel new vonage
Or, if you’re using Composer.
composer create-project laravel/laravel --prefer-dist vonage
This prepares your development environment for adding Vonage’s messaging and other features. Make sure everything is working before moving to the next step.

Step # 2 : Navigate to Your Project and Set Up Vite.

Once your Laravel project is ready, open your terminal (like Git Bash or your preferred CLI) and move into the project directory.
cd c:xampp/htdocs/vonage
Next, install the necessary frontend dependencies and start the Vite development server. This step ensures your frontend assets like CSS and JavaScript are compiled and ready.
npm install && npm run dev
You can now open another terminal window or tab, navigate to the same folder, and continue running Laravel-related commands from there as needed.

Step # 3 : Create an Account on Vonage (formerly Nexmo).

Head over to Vonage Communications APIs (https://www.vonage.com/communications-apis/) and sign up for a new account. After verifying your email address, you’ll receive £2 in test credits to try out their services. Once you're in, click on "Getting Started", then choose "Send an SMS" and select PHP as your programming language. This will guide you to your API credentials and setup instructions.

Step # 4 : Install the Vonage PHP Package.

To send SMS messages from your Laravel application using Vonage, you’ll need to install the official Vonage PHP SDK, which provides a clean and simple API for interacting with Vonage’s communication services. Open your terminal, navigate to your Laravel project directory, and run the following command.
composer require vonage/client
This will install the core Vonage PHP client along with all required dependencies into your project’s vendor directory. The package includes everything you need to authenticate with the Vonage platform and use its SMS functionality. Once installed, Laravel will be able to communicate with the Vonage API using the provided client methods. This step is essential before you can start building the logic to send SMS messages from your controllers.

Step # 5 : Create a Controller to Send SMS.

To handle the SMS sending logic, you’ll need to create a dedicated controller. Open your terminal and run the following command.
php artisan make:controller SmsController
This will generate a new controller file located at app/Http/Controllers/SmsController.php. Next, open that file and replace its contents with the following code.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SmsController extends Controller
{
    public function sms()
    {
        // Set up Vonage client using API credentials from .env
        $basic = new \Vonage\Client\Credentials\Basic(env('VONAGE_API_KEY'), env('VONAGE_API_SECRET'));
        $client = new \Vonage\Client($basic);
        // Send an SMS message
        $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();
        // Output message status
        if ($message->getStatus() == 0) {
            echo "The message was sent successfully\n";
        } else {
            echo "The message failed with status: " . $message->getStatus() . "\n";
        }
    }
}
We’re setting up a simple controller that connects to Vonage using the credentials from your .env file. Inside the sms() method, we use the sms()->send() function to send a test message to a specific phone number. Just make sure to replace "recipient_phone_number" with a real number in international format (like +1234567890). The second parameter ('All About Laravel') acts as the sender name, and the third is the actual message content. This setup forms the basic logic for sending SMS directly from your Laravel app, and in the next step, we’ll trigger this method through a route to test the integration.

Step # 6 : Create a route.

Start by importing the SmsController at the top of your web.php file.
use App\Http\Controllers\SmsController;
Then, define a route that points to the sms() method.
Route::get('/sms', [SmsController::class, 'sms']);
This route will allow you to trigger the SMS sending function by visiting /sms in your browser. It's a simple way to test your setup and ensure the SMS functionality is working correctly.

Step # 7 : Add your Vonage API keys to the .env file.

Open your .env file and add the following lines.
VONAGE_API_KEY=your_api_key
VONAGE_API_SECRET=your_api_secret
Make sure to replace your_api_key and your_api_secret with the actual values from your Vonage account. Keeping sensitive credentials in environment variables is a best practice, it keeps your codebase clean and your secrets safe, especially in production.

Step # 8 : Test the SMS functionality.

Start your Laravel development server by running.
php artisan server
Then open your browser and visit: http://127.0.0.1:8000/sms. If you have followed the steps correctly, you should see the message.
"The message was sent successfully"
You’ll also be able to find the text message under the Logs SMS Logs section in your Vonage Dashboard.

Conclusion

By following this guide, you’ve successfully integrated Vonage SMS functionality into your Laravel 9 project using the official Vonage PHP SDK. You created a Vonage account, configured your API credentials securely via the .env file, set up a controller to handle SMS logic, defined a test route, and verified the setup in your browser. This foundational setup opens the door for adding real-time communication features to your Laravel applications. Your Laravel app is now capable of sending SMS messages directly through Vonage.
For more advanced features like two-way messaging, delivery receipts, or using branded sender IDs, refer to the official Vonage PHP SDK documentation and Vonage API reference.
Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering