PHP code example of kaydee123 / msg91-laravel

1. Go to this page and download the library: Download kaydee123/msg91-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kaydee123 / msg91-laravel example snippets


use Kaydee123\Msg91Laravel\Facades\Msg91;

// Send SMS using Template
$response = Msg91::sms()
    ->template('YOUR_TEMPLATE_ID')
    ->variables(['number' => '3', 'date' => '22-12-2025'])
    ->numbers('919876543210')
    ->send();

// Send OTP
$response = Msg91::otp()
    ->template('YOUR_OTP_TEMPLATE_ID')
    ->number('919876543210')
    ->send();

// Verify OTP
$response = Msg91::otp('123456')
    ->number('919876543210')
    ->verify();

use Kaydee123\Msg91\Msg91Client;

class SmsController extends Controller
{
    protected $msg91;

    public function __construct(Msg91Client $msg91)
    {
        $this->msg91 = $msg91;
    }

    public function sendSms()
    {
        $response = $this->msg91->sms()
            ->template('YOUR_TEMPLATE_ID')
            ->numbers('919876543210')
            ->send();
    }
}

// Send SMS
$response = msg91()->sms()
    ->template('YOUR_TEMPLATE_ID')
    ->variables(['var1' => 'value1'])
    ->numbers('919876543210')
    ->send();

// Send OTP
$response = msg91()->otp()
    ->template('YOUR_OTP_TEMPLATE_ID')
    ->number('919876543210')
    ->send();

$msg91 = app('msg91.client');

// Or using the class name
$msg91 = app(\Kaydee123\Msg91\Msg91Client::class);

use Kaydee123\Msg91Laravel\Facades\Msg91;

$response = Msg91::sms()
    ->template('YOUR_TEMPLATE_ID')
    ->variables(['number' => '3', 'date' => '22-12-2025'])
    ->numbers('919876543210')
    ->send();

$response = Msg91::sms()
    ->template('YOUR_TEMPLATE_ID')
    ->numbers(['919876543210', '919876543211'])
    ->variables(['number' => '3', 'date' => '22-12-2025'])
    ->send();

$response = Msg91::sms()
    ->promotional()
    ->sender_id('YOUR_SENDER_ID')
    ->mobiles("9876543210,9876543211")
    ->dlt_template_id("YOUR_DLT_TEMPLATE_ID")
    ->message("Your promotional message here")
    ->send();

$response = Msg91::sms()
    ->transactional()
    ->sender_id('YOUR_SENDER_ID')
    ->mobiles("9876543210")
    ->dlt_template_id("YOUR_DLT_TEMPLATE_ID")
    ->message("Your transactional message here")
    ->send();

$response = Msg91::otp()
    ->template('YOUR_OTP_TEMPLATE_ID')
    ->number('919876543210')
    ->send();

$response = Msg91::otp()
    ->template('YOUR_OTP_TEMPLATE_ID')
    ->number('919876543210')
    ->length(6)        // OTP length (optional)
    ->expiry(10)      // Expiry in minutes (optional)
    ->send();

$response = Msg91::otp('123456')
    ->number('919876543210')
    ->verify();

// Retry as text SMS
$response = Msg91::otp()
    ->number('919876543210')
    ->viaText()
    ->retry();

// Retry as voice call
$response = Msg91::otp()
    ->number('919876543210')
    ->viaVoice()
    ->retry();

// ✅ Correct - Template ID provided for India
$response = Msg91::otp()
    ->template('YOUR_DLT_TEMPLATE_ID')  // Required for India
    ->number('919876543210')  // Indian number (starts with 91)
    ->send();

// ❌ Will throw error - Template ID missing for Indian number
$response = Msg91::otp()
    ->number('919876543210')  // Indian number
    ->send();  // Missing template() - will throw InvalidArgumentException

use Kaydee123\Msg91\Exceptions\Msg91Exception;
use Kaydee123\Msg91\Exceptions\ApiException;

try {
    $response = Msg91::sms()
        ->template('YOUR_TEMPLATE_ID')
        ->numbers('919876543210')
        ->send();
} catch (ApiException $e) {
    // API-specific errors
    echo "API Error: " . $e->getMessage();
    echo "Status Code: " . $e->getStatusCode();
    print_r($e->getResponse());
} catch (Msg91Exception $e) {
    // General MSG91 errors
    echo "Error: " . $e->getMessage();
} catch (\Exception $e) {
    // Other errors
    echo "Unexpected error: " . $e->getMessage();
}
bash
php artisan vendor:publish --tag=msg91-config