Installation
composer require chewbacco/sms-bundle
Add the bundle to config/bundles.php (Symfony) or register the service provider in config/app.php (Laravel):
Chewbacco\SmsBundle\ChewbaccoSmsBundle::class,
Configuration Publish the config file:
php artisan vendor:publish --provider="Chewbacco\SmsBundle\ChewbaccoSmsBundle" --tag="config"
Update config/sms.php with your provider credentials (e.g., Twilio, AWS SNS, or a custom gateway).
First Use Case: Sending a Basic SMS
Inject the SmsService into a controller or service:
use Chewbacco\SmsBundle\Services\SmsService;
public function sendWelcomeSms(SmsService $smsService)
{
$smsService->send([
'to' => '+1234567890',
'body' => 'Welcome to our platform!'
]);
}
Verify Provider
Check config/sms.php for the active provider and its configuration. Example for Twilio:
'providers' => [
'twilio' => [
'account_sid' => env('TWILIO_SID'),
'auth_token' => env('TWILIO_TOKEN'),
'from' => '+1234567890',
],
],
Set the default provider in default_provider: twilio.
Sending SMS
$smsService->send(['to' => '+1234567890', 'body' => 'Hello!']);
$smsService->send([
'to' => '+1234567890',
'body' => 'Hello!',
'from' => '+1987654321', // Override default 'from'
'provider' => 'aws', // Specify provider
]);
$smsService->sendBatch([
['to' => '+1234567890', 'body' => 'Message 1'],
['to' => '+1987654321', 'body' => 'Message 2'],
]);
Event-Driven SMS
Listen for model events (e.g., UserRegistered) and trigger SMS:
use Chewbacco\SmsBundle\Events\SmsSent;
public function handleUserRegistered()
{
event(new SmsSent([
'to' => auth()->user()->phone,
'body' => 'Your account is ready!',
]));
}
Queueing SMS
Dispatch SMS jobs to a queue (requires queue driver in .env):
$smsService->dispatch([
'to' => '+1234567890',
'body' => 'Queued message!',
]);
libphonenumber).
use giggsey\LibPhonenumber\PhoneNumberBundle\Validator\Constraints\PhoneNumber;
config/sms.php:
'log_sent_sms' => true,
SmsService in tests:
$this->mock(SmsService::class)->shouldReceive('send')->once();
Provider Configuration
default_provider in config/sms.php throws RuntimeException.storage/logs/laravel.log for missing credentials.Phone Number Format
1234567890 without +) may fail silently or return errors.libphonenumber to validate/format numbers:
$phone = PhoneNumber::parse($rawPhone, 'US');
$formatted = $phone->formatE164();
Rate Limits
retry middleware or queue failed jobs:
$smsService->sendWithRetry(['to' => '+1234567890', 'body' => 'Message'], 3);
Environment Variables
config/sms.php instead of using .env..env for sensitive data:
'account_sid' => env('TWILIO_SID'),
'debug' => true in config/sms.php to log provider responses.php artisan queue:work
config/sms.php.Custom Providers Extend the bundle by creating a new provider class:
namespace App\Providers;
use Chewbacco\SmsBundle\Contracts\SmsProviderInterface;
class CustomProvider implements SmsProviderInterface {
public function send(array $message) {
// Custom logic (e.g., HTTP API call)
}
}
Register it in config/sms.php:
'providers' => [
'custom' => [
'class' => App\Providers\CustomProvider::class,
'config' => [...],
],
],
Events and Listeners
Extend functionality by listening to SmsSent or SmsFailed events:
// Event listener
public function onSmsSent(SmsSent $event) {
Log::info('SMS sent to: ' . $event->getMessage()['to']);
}
Middleware Add middleware to modify messages before sending:
$smsService->addMiddleware(function ($message) {
$message['body'] .= "\nPowered by ChewbaccoSmsBundle";
return $message;
});
sendBatch() for bulk SMS to reduce API calls.How can I help you explore Laravel packages today?