PHP code example of mr-rijal / laravel-sms

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

    

mr-rijal / laravel-sms example snippets


$result = Sms::to('+15551234567')->message('Your code is 4821')->sendNow();

$result?->firstId(); // the provider's message ID (null if an SmsSending listener cancelled the send)

use MrRijal\LaravelSms\Facades\Sms;

// Default provider
Sms::to('+15551234567')->message('Hello')->send();

// Several recipients, a specific provider and sender
Sms::provider('sinch')
    ->to(['+15551234567', '+15557654321'])
    ->from('ACME')
    ->message('Maintenance tonight at 10pm')
    ->send();

// Templates
Sms::to('+919812345678')->template('6512a...', ['otp' => 4821])->send();

// Build a message once and send it anywhere
$message = SmsMessage::create('Hello')->to('+15551234567');
Sms::sendNow($message);

$result = Sms::to($numbers)->message('Hi')->sendNow();

$result->provider;     // "twilio"
$result->recipients(); // accepted numbers
$result->ids();        // provider message IDs
foreach ($result->messages as $sent) {
    $sent->to; $sent->id; $sent->status;
}

use MrRijal\LaravelSms\Exceptions\DeliveryException;

try {
    Sms::to($numbers)->message('Hi')->sendNow();
} catch (DeliveryException $e) {
    $e->failedRecipients;        // numbers that were not accepted
    $e->partial?->recipients();  // numbers that were accepted before the failure
}

Sms::to($numbers)->message('Hi')->sendLater();
Sms::to($numbers)->message('Hi')->sendLaterAt(now()->addMinutes(10));

use MrRijal\LaravelSms\SmsMessage;

class OrderShipped extends Notification implements ShouldQueue
{
    use Queueable;

    public function via(object $notifiable): array
    {
        return ['sms'];
    }

    public function toSms(object $notifiable): SmsMessage
    {
        return SmsMessage::create("Order #{$this->order->id} has shipped.");
    }
}

public function routeNotificationForSms(): string
{
    return $this->phone;
}

Notification::route('sms', '+15551234567')->notify(new OrderShipped($order));

$user->notify(new SmsNotification(message: 'Welcome!'));
$user->notify(new SmsNotification(templateId: 'OTP', variables: ['otp' => 4821], provider: 'msg91'));

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use MrRijal\LaravelSms\Drivers\HttpDriver;
use MrRijal\LaravelSms\SentMessage;
use MrRijal\LaravelSms\SmsMessage;
use MrRijal\LaravelSms\SmsResult;

class AcmeDriver extends HttpDriver
{
    protected string $label = 'Acme';

    protected function validateConfig(): void
    {
        $this->$message), 'text' => $text]),
            function (Response $response, string $to): SentMessage {
                if (! $response->successful()) {
                    $this->fail($response, 'error.message');
                }

                return new SentMessage($to, $this->jsonString($response, 'id'));
            },
        );
    }
}

Sms::extend('acme', fn ($app, array $config) => new AcmeDriver($config));

use MrRijal\LaravelSms\Facades\Sms;
use MrRijal\LaravelSms\SmsMessage;

public function test_it_sends_the_login_code(): void
{
    Sms::fake();

    // ... run your code ...

    Sms::assertSentTo('+15551234567');
    Sms::assertSent(fn (SmsMessage $sms) => str_contains($sms->getText(), 'code'));
    Sms::assertSentCount(1);
    Sms::assertQueued(2);
}

public function test_it_sends_nothing_for_unverified_users(): void
{
    Sms::fake();

    // ... run your code ...

    Sms::assertNothingSent();
    Sms::assertNothingQueued();
}
bash
php artisan sms:test +15551234567