pkc / sms-gateway
A multi-provider SMS gateway integration package for Laravel.
Requires
- php: ^8.2
- illuminate/console: ^11.0|^12.0
- illuminate/contracts: ^11.0|^12.0
- illuminate/database: ^11.0|^12.0
- illuminate/http: ^11.0|^12.0
- illuminate/notifications: ^11.0|^12.0
- illuminate/queue: ^11.0|^12.0
- illuminate/routing: ^11.0|^12.0
- illuminate/support: ^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^9.0|^10.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A multi-provider SMS gateway package for Laravel 11 and 12 (PHP 8.2+). Providers are called through Laravel's HTTP client, so no vendor SDK is needed.
| Driver | Name | Bulk call | Balance | Delivery reports |
|---|---|---|---|---|
| SSL Wireless (SMS Plus v3) | sslwireless |
no | yes | no |
| BulkSMSBD | bulksmsbd |
yes | yes | no |
| GreenWeb BD | greenweb |
yes | yes | no |
| Alpha SMS | alphasms |
yes | yes | no |
| MiM SMS | mimsms |
no | yes | no |
| Twilio | twilio |
no | yes | yes (signed) |
| Vonage | vonage |
no | yes | yes (shared secret) |
| Infobip | infobip |
no | yes | yes (shared secret) |
| Log file | log |
no | no | no |
| In-memory (tests) | array |
no | no | no |
Drivers without a bulk call send one request per recipient.
Installation
composer require pkc/sms-gateway php artisan vendor:publish --tag=sms-gateway-config
The sms_logs migration is loaded automatically. To customize it, publish it and stop the automatic loading:
php artisan vendor:publish --tag=sms-gateway-migrations
// AppServiceProvider::register() \Pkc\SmsGateway\Providers\SmsGatewayServiceProvider::ignoreMigrations();
Then run php artisan migrate. Set SMS_LOGGING_ENABLED=false to skip logging altogether.
Local development with a path repository
{
"repositories": [{ "type": "path", "url": "../pkc-sms-gateway", "options": { "symlink": true } }],
"require": { "pkc/sms-gateway": "@dev" }
}
Configuration
Pick the driver and add its credentials in .env:
SMS_DRIVER=bulksmsbd SMS_SENDER_ID=MyBrand SMS_DEFAULT_COUNTRY_CODE=880 SMS_BULKSMSBD_API_KEY= SMS_BULKSMSBD_SENDER_ID=
All keys are listed in config/sms-gateway.php (SMS_SSLWIRELESS_*, SMS_BULKSMSBD_*, SMS_GREENWEB_*, SMS_ALPHASMS_*, SMS_MIMSMS_*, SMS_TWILIO_*, SMS_VONAGE_*, SMS_INFOBIP_*). Every HTTP driver accepts a base_url override, for example to use a sandbox.
The sender ID is read from the message first, then from the driver configuration, then from SMS_SENDER_ID.
Sending
use Pkc\SmsGateway\Facades\Sms; use Pkc\SmsGateway\Support\SmsMessage; Sms::send('01712345678', 'Your code is 4821'); Sms::send(new SmsMessage('01712345678', 'Hello', sender: 'MyBrand')); Sms::via('twilio')->send('+14155552671', 'Hello from Twilio');
send() returns an SmsResponse (driver, messageId, raw). It throws a subclass of SmsException when the provider does not accept the message, for example ProviderRequestException (with driver, statusCode and payload). Provider errors are never returned as a failed response.
Phone numbers
Numbers are converted to international digits before they reach a provider. 01712345678, 1712345678, 8801712345678 and +8801712345678 all become 8801712345678. A number for another country must start with + or 00. Invalid numbers throw InvalidPhoneNumberException and are never sent.
Queue and delay
Sms::queue('01712345678', 'Sent by a queue worker'); Sms::later(now()->addMinutes(5), '01712345678', 'Sent in five minutes');
Both return a PendingDispatch. Set SMS_QUEUE_CONNECTION, SMS_QUEUE and SMS_QUEUE_TRIES; the backoff list is in the config file. The log row is created as queued and moves to sent or failed.
Bulk
$result = Sms::sendBulk(['01712345678', '01812345678'], 'Hello everyone'); $result->successes(); // recipient => SmsResponse $result->failures(); // recipient => Throwable
A driver with a bulk endpoint receives one request for the whole batch. Invalid numbers and rejected messages are collected in the result instead of stopping the batch. Each recipient gets its own log row. The array keys are phone numbers; PHP turns numeric string keys into integers, so cast them with (string).
Fallback
SMS_DRIVER=sslwireless SMS_FALLBACK_DRIVERS=bulksmsbd,twilio
Messages sent without an explicit driver are tried on the default driver, then on each fallback driver, until one accepts them. When all fail, AllDriversFailedException lists every error in failures. The log row stores the driver that finally sent the message. Sms::via('twilio') ignores the chain.
Notifications
class OrderShipped extends Notification { public function via(object $notifiable): array { return ['sms']; } public function toSms(object $notifiable): string|SmsMessage { return "Order #{$this->order->id} has shipped."; } } // On the notifiable public function routeNotificationForSms(): string { return $this->phone; }
On-demand notifications work too: Notification::route('sms', '01712345678')->notify(...).
Balance
$balance = Sms::balance(); // default driver $balance = Sms::balance('twilio'); // $balance->amount, $balance->currency
php artisan sms:balance twilio
Drivers without a balance endpoint throw UnsupportedOperationException.
Delivery reports (webhooks)
Providers post delivery reports to POST|GET {SMS_WEBHOOK_PREFIX}/webhook/{driver} (default /sms-gateway/webhook/twilio). The matching sms_logs row moves to delivered or undelivered; a row that already reached one of those statuses is not changed again.
- Twilio: set
SMS_TWILIO_CALLBACK_URLto the webhook URL. TheX-Twilio-Signatureheader is verified with your auth token, so the URL must match the public URL exactly. - Vonage and Infobip: set
SMS_WEBHOOK_SECRETand add?secret=your-secretto the callback URL (or send theX-Sms-Webhook-Secretheader). SetSMS_VONAGE_CALLBACK_URL/SMS_INFOBIP_CALLBACK_URLto include it. - Requests are rejected with 403 while no secret is configured.
The routes are registered without the web middleware group, so they are not subject to CSRF protection. Add middleware (for example a throttle) in sms-gateway.webhook.middleware, or set SMS_WEBHOOK_ENABLED=false to turn the routes off.
Custom drivers
use Pkc\SmsGateway\Contracts\SmsDriver; Sms::extend('my-provider', fn () => new MyProviderDriver(config('services.my-provider')));
A driver implements SmsDriver (name() and send()) and throws a subclass of SmsException when the provider rejects a message. Add SupportsBulk, SupportsBalance or SupportsDeliveryReports for the optional features, or extend AbstractHttpDriver for the HTTP helpers.
Testing your application
config(['sms-gateway.default' => 'array']); Sms::send('01712345678', 'Hello'); $messages = Sms::driver('array')->messages(); // list<SmsMessage>
Testing the package
composer install vendor/bin/phpunit
The tests use an in-memory SQLite database. When pdo_sqlite is not installed, point them at another database:
SMS_TEST_DB_CONNECTION=mysql SMS_TEST_DB_DATABASE=pkc_sms_gateway_test \ SMS_TEST_DB_USERNAME=root SMS_TEST_DB_PASSWORD=secret vendor/bin/phpunit
Provider API notes
The drivers follow each provider's public HTTP API as of writing. Vendors change endpoints and response formats, so verify new accounts with a real test message. Use the base_url option when your account uses another endpoint.
License
MIT