Installation:
composer require robincsamuel/laravel-msg91
Add to config/app.php:
'providers' => [
// ...
RobinCSamuel\LaravelMsg91\LaravelMsg91ServiceProvider::class,
],
'aliases' => [
// ...
'LaravelMsg91' => RobinCSamuel\LaravelMsg91\Facades\LaravelMsg91::class,
]
Environment Configuration:
Add these to .env:
MSG91_KEY=your_auth_key_from_msg91
MSG91_SENDER_ID=YourSenderID
MSG91_ROUTE=4 # (e.g., 4 for transactional, 5 for promotional)
MSG91_COUNTRY=91 # Default country code (e.g., 91 for India)
First Use Case: Send a text SMS:
use LaravelMsg91\Facades\LaravelMsg91;
LaravelMsg91::send('+919876543210', 'Hello from Laravel!');
Sending SMS:
LaravelMsg91::send($mobileNumber, $message);
$otp = LaravelMsg91::sendOTP($mobileNumber, $templateId);
Batch Processing: Use Laravel’s queues to avoid timeouts for bulk SMS:
LaravelMsg91::dispatch($mobileNumber, $message)->delay(now()->addMinutes(5));
Dynamic Sender ID: Override sender ID per request:
LaravelMsg91::setSenderId('CUSTOMID')->send($mobileNumber, $message);
Error Handling: Wrap calls in try-catch:
try {
LaravelMsg91::send($mobileNumber, $message);
} catch (\Exception $e) {
Log::error('MSG91 Error: ' . $e->getMessage());
}
Integration with Events:
Trigger SMS on model events (e.g., created):
class User extends Model {
protected static function booted() {
static::created(function ($user) {
LaravelMsg91::send($user->phone, 'Welcome!');
});
}
}
Deprecated Package:
Environment Variables:
MSG91_KEY is mandatory; missing it throws silent failures.MSG91_ROUTE defaults to 4 (transactional). Use 5 for promotional SMS (rate limits apply).Mobile Number Format:
+919876543210), or SMS may fail.MSG91_COUNTRY=91 for India; adjust for other regions.Rate Limits:
LaravelMsg91::dispatch($mobileNumber, $message)->delay(now()->addSecond());
OTP Template IDs:
.env or database.Enable Logging:
Add to .env:
MSG91_DEBUG=true
Logs API responses to storage/logs/laravel.log.
API Response Parsing: MSG91 returns JSON like:
{"type":"error", "message":"Invalid Auth Key"}
Use LaravelMsg91::getLastResponse() to inspect raw output.
Common Errors:
Invalid Auth Key: Verify MSG91_KEY in .env.Invalid Route: Ensure MSG91_ROUTE is 4 or 5.Invalid Mobile Number: Validate format (e.g., +919876543210).Custom Base URI: Override MSG91 API endpoint (useful for resellers):
MSG91_BASE_URI=https://api.msg91.com/api/sendotp.php
Mocking for Tests: Extend the facade for testing:
// In tests/CreatesApplication.php
$this->app->singleton(\RobinCSamuel\LaravelMsg91\Msg91::class, function () {
return Mockery::mock(\RobinCSamuel\LaravelMsg91\Msg91::class);
});
Adding New Features:
Msg91 class:
// app/Extensions/Msg91Extension.php
namespace App\Extensions;
use RobinCSamuel\LaravelMsg91\Msg91;
class Msg91Extension extends Msg91 {
public function sendScheduled($mobile, $message, \Carbon\Carbon $schedule) {
// Implement logic to queue SMS
}
}
AppServiceProvider:
$this->app->bind(Msg91::class, function () {
return new Msg91Extension(config('msg91'));
});
How can I help you explore Laravel packages today?