PHP code example of ghanem / laravel-smsmisr

1. Go to this page and download the library: Download ghanem/laravel-smsmisr 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/ */

    

ghanem / laravel-smsmisr example snippets


use Ghanem\LaravelSmsmisr\Facades\Smsmisr;

$response = Smsmisr::send('Hello world', '01012345678');

if ($response->isSuccessful()) {
    echo $response->message;
}

$response = app('smsmisr')->send('Hello world', '01012345678');

$response = Smsmisr::sendBulk('Hello everyone', [
    '01012345678',
    '01112345678',
    '01212345678',
]);

$response = Smsmisr::sendVerify('1234', '01012345678', null, 'your-template');

$response = Smsmisr::send(
    message: 'Happy Birthday!',
    to: '01012345678',
    scheduledAt: new DateTime('2026-04-01 09:00:00'),
);

// Also works with bulk
$response = Smsmisr::sendBulk(
    message: 'Sale starts now!',
    recipients: ['01012345678', '01112345678'],
    scheduledAt: new DateTime('2026-04-01 09:00:00'),
);

use Ghanem\LaravelSmsmisr\Facades\Smsmisr;

// Queue a single SMS
Smsmisr::queue('Hello', '01012345678');

// Queue bulk SMS
Smsmisr::queueBulk('Hello everyone', ['01012345678', '01112345678']);

// Queue verification SMS
Smsmisr::queueVerify('1234', '01012345678', null, 'your-template');

use Ghanem\LaravelSmsmisr\Jobs\SendSmsJob;
use Ghanem\LaravelSmsmisr\Jobs\SendBulkSmsJob;
use Ghanem\LaravelSmsmisr\Jobs\SendVerifySmsJob;

SendSmsJob::dispatch('Hello', '01012345678');
SendBulkSmsJob::dispatch('Hello', ['01012345678', '01112345678']);
SendVerifySmsJob::dispatch('1234', '01012345678', null, 'template');

$response = Smsmisr::balance();
echo $response->raw['balance'];

$response = Smsmisr::balanceVerify();

if (Smsmisr::health()) {
    // API is reachable and credentials are valid
}

$response->isSuccessful(); // bool
$response->isFailed();     // bool
$response->code;           // int (e.g. 1901)
$response->message;        // string
$response->raw;            // array (full API response)
$response->toArray();      // array

use Ghanem\LaravelSmsmisr\Exceptions\SmsmisrApiException;
use Ghanem\LaravelSmsmisr\Exceptions\SmsmisrAuthenticationException;
use Ghanem\LaravelSmsmisr\Exceptions\SmsmisrInsufficientBalanceException;
use Ghanem\LaravelSmsmisr\Exceptions\SmsmisrRateLimitException;

try {
    $response = Smsmisr::send('Hello', '01012345678');
} catch (SmsmisrAuthenticationException $e) {
    // Invalid credentials
} catch (SmsmisrInsufficientBalanceException $e) {
    // Not enough SMS credits
} catch (SmsmisrRateLimitException $e) {
    // Rate limit exceeded
} catch (SmsmisrApiException $e) {
    $e->getCode();       // Error code
    $e->getMessage();    // Error message
    $e->getResponse();   // Full response array
}

use Ghanem\LaravelSmsmisr\Rules\EgyptianPhoneNumber;

$request->validate([
    'phone' => ['

use Ghanem\LaravelSmsmisr\Events\SmsSent;
use Ghanem\LaravelSmsmisr\Events\LowBalance;

Event::listen(SmsSent::class, function (SmsSent $event) {
    logger("SMS sent to {$event->to}");
});

Event::listen(LowBalance::class, function (LowBalance $event) {
    // Send alert to admin
});

Schedule::command('smsmisr:check-balance')->daily();

namespace App\Notifications;

use Ghanem\LaravelSmsmisr\SmsmisrChannel;
use Ghanem\LaravelSmsmisr\SmsmisrMessage;
use Illuminate\Notifications\Notification;

class OrderShipped extends Notification
{
    public function via($notifiable): array
    {
        return [SmsmisrChannel::class];
    }

    public function toSmsmisr($notifiable): SmsmisrMessage
    {
        return (new SmsmisrMessage('Your order has been shipped!', $notifiable->phone))
            ->sender('MyApp');
    }
}

public function toSmsmisr($notifiable): SmsmisrMessage
{
    return (new SmsmisrMessage())
        ->to($notifiable->phone)
        ->asVerification('1234', 'your-template-id');
}

public function toSmsmisr($notifiable): SmsmisrMessage
{
    return (new SmsmisrMessage('Reminder: appointment tomorrow', $notifiable->phone))
        ->scheduledAt(new DateTime('2026-04-01 09:00:00'));
}

(new SmsmisrMessage(string $message, string $to))
    ->message(string $message)
    ->to(string $to)
    ->sender(string $sender)
    ->unicode(bool $unicode)
    ->asVerification(string $code, ?string $template)
    ->scheduledAt(DateTimeInterface $dateTime)

use Ghanem\LaravelSmsmisr\Facades\Smsmisr;

public function test_order_sends_sms(): void
{
    Smsmisr::fake();

    // ... trigger SMS ...

    Smsmisr::assertSent('01012345678', 'Your order has been shipped!');
    Smsmisr::assertSentCount(1);
}

Smsmisr::fake();

// Sent
Smsmisr::assertSent($to, $message);
Smsmisr::assertSentCount($count);
Smsmisr::assertNothingSent();
Smsmisr::assertSentWithSchedule($to);

// Verification
Smsmisr::assertVerifySent($to, $code);
Smsmisr::assertVerifySentCount($count);

// Bulk
Smsmisr::assertBulkSent($message);
Smsmisr::assertBulkSentCount($count);
Smsmisr::assertBulkSentTo($recipients);

// Queue
Smsmisr::assertQueued($to, $message);
Smsmisr::assertQueuedCount($count);
Smsmisr::assertVerifyQueued($to, $code);
Smsmisr::assertNothingQueued();

// Inspect
Smsmisr::getSent();
Smsmisr::getBulk();
Smsmisr::getVerified();
Smsmisr::getQueued();
bash
php artisan vendor:publish --provider="Ghanem\LaravelSmsmisr\SmsmisrServiceProvider" --tag="smsmisr-config"
bash
# Display current SMS and Verify balance
php artisan smsmisr:balance

# Check balance against threshold and dispatch LowBalance event if below
php artisan smsmisr:check-balance
php artisan smsmisr:check-balance --threshold=500