Installation Run:
composer require amirjon/sms-bundle
(Note: Fix typo in sms-bunle → sms-bundle in your docs.)
Register Bundle
Add to config/bundles.php:
Amir\SmsBundle\SmsBundle::class => ['all' => true],
Configure .env
Add Eskiz credentials (replace placeholders):
ESKIZ_EMAIL=your@email.com
ESKIZ_PASSWORD=your_secure_password
ESKIZ_FROM=your_sender_number # e.g., "998123456789"
First Use Case
Inject SmsSender into a class (e.g., a controller or service) and send an SMS:
use Amir\SmsBundle\Services\SmsSender;
class MyController {
public function __construct(private SmsSender $sender) {}
public function sendWelcomeSms() {
$this->sender->sendMessage('998123456789', 'Welcome to our platform!');
}
}
SmsSender in services/controllers.
public function __construct(private SmsSender $sender) {}
$this->app->bind(SmsSender::class, function ($app) {
return new CustomSmsSender($app->make(SmsSender::class));
});
Basic SMS Sending
$this->sender->sendMessage('+1234567890', 'Your OTP is 123456');
(Note: Validate phone numbers with Str::startsWith($number, '+') or similar.)
Bulk SMS Loop through recipients (no built-in bulk method; implement manually):
foreach ($recipients as $phone) {
$this->sender->sendMessage($phone, $message);
}
Async Processing Use Laravel Queues to offload SMS sending:
SendSmsJob::dispatch($phone, $message);
(Requires custom job class; package lacks built-in queue support.)
use Illuminate\Support\Facades\Validator;
$validator = Validator::make(['phone' => $phone], [
'phone' => 'required|string|starts_with:+,998|digits_between:9,15'
]);
try-catch to log failures:
try {
$this->sender->sendMessage($phone, $message);
} catch (\Exception $e) {
\Log::error("SMS failed for $phone: " . $e->getMessage());
}
config/sms.php (create file if missing):
return [
'default_from' => '998901234567',
'timeout' => 30, // seconds
];
Typo in Package Name
composer.json lists amirjon/sms-bundle, but the README shows sms-bunle. Double-check installation.No Error Handling
sendMessage() method lacks explicit error handling. Wrap calls in try-catch blocks.Phone Number Format
+998912345678). Test with your provider’s format.No Rate Limiting
use Illuminate\Support\Facades\Http;
Http::timeout(30)->retry(3, 100);
Dependency Conflicts
.env values and test manually via Eskiz’s API docs.\Log::debug('SMS Request:', [
'url' => $this->sender->getEndpoint(),
'data' => $this->sender->getPayload($phone, $message),
]);
$this->sender->getHttpClient()->withOptions(['debug' => true]);
Custom Providers
Extend Amir\SmsBundle\Services\SmsSender to support other APIs (e.g., Clickatell):
class CustomSmsSender extends SmsSender {
public function sendMessage(string $phone, string $message): bool {
// Implement alternative logic
}
}
Middleware Add middleware to validate SMS content or log messages:
$this->app->bind(SmsSender::class, function ($app) {
$sender = new SmsSender($app);
return new Middleware\SmsLogger($sender);
});
Events Dispatch events before/after sending (requires custom implementation):
event(new SmsSent($phone, $message));
ESKIZ_FROM in .env sets the default sender ID. Override per-message if needed:
$this->sender->sendMessage($phone, $message, ['from' => '998901234567']);
config/sms.php:
'http_client' => [
'timeout' => 60, // seconds
'connect_timeout' => 10,
],
SmsSender in tests:
$mock = Mockery::mock(SmsSender::class);
$mock->shouldReceive('sendMessage')->once();
config('sms.timeout') for dynamic values.\DB::table('sms_logs')->insert([
'phone' => $phone,
'message' => $message,
'sent_at' => now(),
]);
How can I help you explore Laravel packages today?