PHP code example of rayzenai / laravel-sms

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

    

rayzenai / laravel-sms example snippets


// config/laravel-sms.php
'user_model' => [
    'enabled' => true,
    'class' => \App\Models\User::class,
    'phone_field' => 'phone', // The field that contains the phone number
    'name_field' => 'name',   // The field to display as user name
],

'phone' => '

use Rayzenai\LaravelSms\Services\SmsService;
use Rayzenai\LaravelSms\Facades\Sms;

// Method 1: Using the Facade with fluent interface (recommended)
$sentMessage = Sms::to('+9779801002468')
    ->message('Hello from Laravel SMS!')
    ->send();

// Method 2: Using the Facade with direct method call
$sentMessage = Sms::send('+9779801002468', 'Hello from Laravel SMS!');

// Method 3: Using dependency injection
public function sendSms(SmsService $smsService)
{
    try {
        $sentMessage = $smsService->send('+1234567890', 'Hello from Laravel SMS!');
        
        // Access sent message details
        echo "Message ID: " . $sentMessage->provider_message_id;
        echo "Status: " . $sentMessage->status;
    } catch (\Exception $e) {
        // Handle error
        Log::error('SMS sending failed: ' . $e->getMessage());
    }
}

// Method 3: Using service container
$smsService = app(SmsService::class);
$sentMessage = $smsService->send('+1234567890', 'Your message here');

use Rayzenai\LaravelSms\Facades\Sms;
use Rayzenai\LaravelSms\Services\SmsService;

// Method 1: Using the Facade with fluent interface (recommended)
$recipients = [
    '+9779801002468',
    '+9779812345678',
    '+9779898765432'
];

$sentMessages = Sms::to($recipients)
    ->message('Bulk message to all recipients!')
    ->sendBulk();

// Method 2: Using the service directly
$smsService = app(SmsService::class);

try {
    $sentMessages = $smsService->sendBulk($recipients, 'Bulk message to all recipients!');
    
    foreach ($sentMessages as $message) {
        echo "Recipient: {$message->recipient} - Status: {$message->status}\n";
    }
} catch (\Exception $e) {
    Log::error('Bulk SMS failed: ' . $e->getMessage());

use Illuminate\Foundation\Auth\User as Authenticatable;
use Rayzenai\LaravelSms\Concerns\Smsable;
use Rayzenai\LaravelSms\Contracts\HasSmsNumber;

class User extends Authenticatable implements HasSmsNumber
{
    use Smsable;

    public function smsPhoneNumber(): ?string
    {
        // Return a sendable number (E.164 like +9779801002468 recommended),
        // or null if this user can't be reached by SMS.
        return $this->phone;
        // e.g. concat: '+' . ltrim($this->country_code, '+') . $this->phone
    }
}

// Straight off the model — returns a SentMessage, or null if it has no number
$user->sendSMS('Your appointment is confirmed.');

// Send via a specific provider
$user->sendSMS('Sent via AakashSMS', 'aakash');

// Through the facade — models and plain strings can be mixed
Sms::to($user)->message('Hi')->send();
Sms::to($users)->message('Clinic closed tomorrow')->sendBulk();

use Rayzenai\LaravelSms\Facades\Sms;

// Send this one message through AakashSMS regardless of the default provider
Sms::provider('aakash')->send('+9779801002468', 'Sent via AakashSMS');

Sms::provider('swift')
    ->to(['+9779801002468', '+9779812345678'])
    ->message('Sent via SwiftSMS')
    ->sendBulk();

use Rayzenai\LaravelSms\Facades\Sms;

$balance = Sms::provider('aakash')->balance();
// ['credit' => 1234, 'response' => [...]]

echo "Remaining credit: {$balance['credit']}";

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Rayzenai\LaravelSms\Services\SmsService;

class NotificationController extends Controller
{
    private SmsService $smsService;
    
    public function __construct(SmsService $smsService)
    {
        $this->smsService = $smsService;
    }
    
    public function sendWelcomeSms(Request $request)
    {
        $request->validate([
            'phone' => 'ion $e) {
            return response()->json([
                'success' => false,
                'error' => 'Failed to send SMS'
            ], 500);
        }
    }
}

'routes' => [
    'enabled' => env('SMS_ROUTES_ENABLED', true),
    'prefix' => env('SMS_ROUTES_PREFIX', 'api'),
    'middleware' => ['api', 'auth:sanctum', 'throttle:60,1'],
    'max_recipients' => env('SMS_MAX_BULK_RECIPIENTS', 1000),
],

use Rayzenai\LaravelSms\LaravelSmsPlugin;

// In app/Providers/Filament/AdminPanelProvider.php or your panel provider:
public function panel(Panel $panel): Panel
{
    return $panel
        ->default()
        ->plugins([
            LaravelSmsPlugin::make(),
        ]);
}

'default' => env('SMS_PROVIDER', 'http'),

'providers' => [
    'http' => [
        'class' => \Rayzenai\LaravelSms\Providers\HttpProvider::class,
        'api_base_url' => env('SMS_API_BASE_URL', 'https://api.example.com'),
        'api_key' => env('SMS_API_KEY', ''),
    ],
    'twilio' => [
        'class' => \Rayzenai\LaravelSms\Providers\TwilioProvider::class,
        'account_sid' => env('TWILIO_ACCOUNT_SID'),
        'auth_token' => env('TWILIO_AUTH_TOKEN'),
        'from' => env('TWILIO_FROM_NUMBER'),
    ],
    'swift' => [
        'class' => \Rayzenai\LaravelSms\Providers\SwiftSmsProvider::class,
        'organisation_code' => env('SWIFT_SMS_ORGANISATION_CODE'),
        'username' => env('SWIFT_SMS_USERNAME'),
        'password' => env('SWIFT_SMS_PASSWORD'),
    ],
    'aakash' => [
        'class' => \Rayzenai\LaravelSms\Providers\AakashSmsProvider::class,
        'auth_token' => env('AAKASH_SMS_AUTH_TOKEN'),
    ],
    'sparrow' => [
        'class' => \Rayzenai\LaravelSms\Providers\SparrowSmsProvider::class,
        'token' => env('SPARROW_SMS_TOKEN'),
        'from' => env('SPARROW_SMS_FROM'),
    ],
],

'channels' => [
    // ...
    'sms' => [
        'driver' => 'daily',
        'path' => storage_path('logs/sms.log'),
        'level' => 'info',
        'days' => 14,
    ],
],

namespace App\Sms\Providers;

use Illuminate\Support\Facades\Http;
use Rayzenai\LaravelSms\Providers\AbstractSmsProvider;

class CustomProvider extends AbstractSmsProvider
{
    public function send(string $recipient, string $message): array
    {
        $response = Http::timeout($this->timeout)
            ->withToken($this->config('api_key'))
            ->post($this->config('api_url'), [
                'to' => $recipient,
                'body' => $message,
                'from' => $this->sender,
            ]);

        return [
            'sid' => $response->json('id'),
            'status' => $response->successful() ? 'sent' : 'failed',
            'response' => $response->json(),
        ];
    }
}

'providers' => [
    'custom' => [
        'class' => \App\Sms\Providers\CustomProvider::class,
        'api_url' => env('CUSTOM_SMS_URL'),
        'api_key' => env('CUSTOM_SMS_KEY'),
    ],
],
bash
php artisan vendor:publish --provider="Rayzenai\LaravelSms\LaravelSmsServiceProvider" --tag="config"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Rayzenai\LaravelSms\LaravelSmsServiceProvider" --tag="filament-resources"