Installation:
composer require chaboksms/laravel
Publish the config file:
php artisan vendor:publish --provider="Chaboksms\Laravel\ChaboksmsServiceProvider" --tag="config"
Configuration:
.env with your Chaboksms credentials:
CHABOKSMS_API_KEY=your_api_key_here
CHABOKSMS_API_SECRET=your_api_secret_here
config/chaboksms.php file matches your setup (e.g., sandbox vs. production).First Use Case: Send a test SMS via a controller or command:
use Chaboksms\Laravel\Facades\Chaboksms;
Chaboksms::send([
'to' => '09123456789',
'message' => 'Hello from Laravel!'
]);
Sending SMS:
Chaboksms::send([
'to' => '09123456789',
'message' => 'Your OTP is 12345',
'type' => 'transactional', // Optional: 'promotional' or 'transactional'
]);
Chaboksms::sendBulk([
'to' => ['09123456789', '09987654321'],
'message' => 'Bulk message for all recipients',
]);
Handling Responses:
Chaboksms::send([
'to' => '09123456789',
'message' => 'Callback test',
], function ($response) {
// Handle success/failure
if ($response->success) {
Log::info('SMS sent:', ['id' => $response->id]);
}
});
OTP Management:
$otp = Chaboksms::generateOtp(6); // 6-digit OTP
Chaboksms::sendOtp('09123456789', $otp, 'Your verification code');
Queue Integration:
use Chaboksms\Laravel\Jobs\SendSmsJob;
SendSmsJob::dispatch([
'to' => '09123456789',
'message' => 'Queued message',
]);
Validation:
use Chaboksms\Laravel\Rules\ValidIranianPhone;
$request->validate([
'phone' => ['required', new ValidIranianPhone],
]);
Logging:
config/chaboksms.php to track SMS activity:
'log' => [
'enabled' => true,
'channel' => 'single',
],
Testing:
CHABOKSMS_ENVIRONMENT=sandbox
Chaboksms::shouldReceive('send')->andReturn(['success' => true]);
Webhooks:
Route::post('/chaboksms/webhook', [SmsWebhookController::class, 'handle']);
Phone Number Format:
09123456789 for Iran). Invalid formats will fail silently or return errors.Rate Limits:
try {
Chaboksms::send($request);
} catch (\Chaboksms\Laravel\Exceptions\RateLimitExceeded $e) {
sleep(2); // Retry after delay
Chaboksms::send($request);
}
Async Delays:
Configuration Overrides:
.env or Laravel's config system.Enable Debug Mode:
debug to true in config/chaboksms.php to log raw API responses:
'debug' => env('CHABOKSMS_DEBUG', false),
Common Errors:
Invalid API Key: Verify .env and config file.Invalid Phone Number: Check phone number format (e.g., 09123456789).Balance Insufficient: Monitor your Chaboksms balance via their dashboard.Testing Locally:
CHABOKSMS_ENVIRONMENT=sandbox
Custom Requests:
namespace App\Services;
use Chaboksms\Laravel\Requests\SendSmsRequest;
class CustomSendSmsRequest extends SendSmsRequest {
public function rules() {
return array_merge(parent::rules(), [
'custom_field' => 'sometimes|string',
]);
}
}
AppServiceProvider:
Chaboksms::extend('custom', function () {
return new CustomSendSmsRequest();
});
Middleware:
Chaboksms::extend(function ($request) {
$request->merge(['custom_header' => 'value']);
return $request;
});
Events:
SmsSent):
Chaboksms::on('SmsSent', function ($sms) {
// Trigger analytics or notifications
});
Fallback Providers:
try {
Chaboksms::send($request);
} catch (\Exception $e) {
FallbackSms::send($request); // Custom fallback logic
}
How can I help you explore Laravel packages today?