PHP code example of 1naturalway / laravel-sms

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

    

1naturalway / laravel-sms example snippets


use OneNaturalWay\Sms\Facades\Sms;

$result = Sms::send('+15559876543', 'Your verification code is 123456');

if ($result->successful()) {
    // Store $result->messageId for tracking
    // Check $result->status
    // For MMS: $result->hasMedia(), $result->mediaUrls
}

use OneNaturalWay\Sms\Exceptions\SmsException;

try {
    $result = Sms::send('+15551234567', 'Hello!');
} catch (SmsException $e) {
    if ($e->isBlacklisted()) {
        // Number opted out / on a blacklist — stop sending to this number
    }

    if ($e->isInvalidNumber()) {
        // Bad phone number format — flag for correction
    }

    // All SmsExceptions carry context:
    // $e->errorType     — 'blacklisted', 'invalid_number', or 'provider_error'
    // $e->phoneNumber   — the "to" number that failed
    // $e->providerCode  — the raw error code from Twilio (e.g., '21610')
    // $e->getPrevious() — the original Twilio RestException
}

$result = Sms::send('+15559876543', 'Hello!', [
    'from'     => '+15550001111',  // Override the default "from" number
    'mediaUrl' => 'https://example.com/image.jpg',  // Twilio MMS
]);

// Use a specific driver for this call
$result = Sms::driver('log')->send('+15559876543', 'This goes to the log');

// Use the default driver
$result = Sms::send('+15559876543', 'This uses the configured default');

use OneNaturalWay\Sms\SmsManager;

class NotificationService
{
    public function __construct(
        protected SmsManager $sms,
    ) {}

    public function sendWelcome(string $phone): void
    {
        $result = $this->sms->send($phone, 'Welcome to our service!');

        if ($result->successful()) {
            logger()->info('Welcome SMS queued', ['sid' => $result->messageId]);
        }
    }
}

use OneNaturalWay\Sms\Facades\Sms;

public function test_sends_verification_sms(): void
{
    $fake = Sms::fake();

    // ... trigger your code that sends SMS ...

    $fake->assertSentTo('+15559876543');
    $fake->assertSentTo('+15559876543', 'verification code');
    $fake->assertSentCount(1);
}

public function test_no_sms_sent_on_invalid_input(): void
{
    $fake = Sms::fake();

    // ... trigger code that should NOT send SMS ...

    $fake->assertNothingSent();
}

public function test_sms_body_matches_pattern(): void
{
    $fake = Sms::fake();

    // ... trigger your code ...

    $fake->assertSentToWithBody('+15559876543', function (SmsResult $result) {
        return preg_match('/\d{6}/', $result->body) === 1;
    });
}

use OneNaturalWay\Sms\Facades\Sms;
use OneNaturalWay\Sms\Contracts\SmsProvider;

Sms::extend('vonage', function ($app, $config) {
    return new VonageSmsProvider(
        apiKey: $config['api_key'],
        apiSecret: $config['api_secret'],
        from: $config['from'] ?? $app['config']['sms.from'],
    );
});

'drivers' => [
    // ... existing drivers ...
    'vonage' => [
        'api_key'    => env('VONAGE_API_KEY'),
        'api_secret' => env('VONAGE_API_SECRET'),
        'from'       => env('VONAGE_FROM'),
    ],
],

use OneNaturalWay\Sms\Contracts\SmsProvider;

use OneNaturalWay\Sms\SmsResult;

class VonageSmsProvider implements SmsProvider
{
    public function send(string $to, string $body, array $options = []): SmsResult
    {
        // Your implementation — return an SmsResult with the provider's response data
    }
}
bash
php artisan vendor:publish --tag=sms-config
dotenv
SMS_DRIVER=log
SMS_LOG_CHANNEL=stack