sendrail / laravel
Laravel integration for Sendrail, the Czech SMS API: facade, notification channel and webhook signature middleware
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.8
- guzzlehttp/psr7: ^2.5
- illuminate/contracts: ^12.0 || ^13.0
- illuminate/http: ^12.0 || ^13.0
- illuminate/notifications: ^12.0 || ^13.0
- illuminate/routing: ^12.0 || ^13.0
- illuminate/support: ^12.0 || ^13.0
- sendrail/sendrail-php: ^0.1
Requires (Dev)
- orchestra/testbench: ^10.0 || ^11.0
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.5 || ^11.5 || ^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-27 11:46:21 UTC
README
Laravel integration for Sendrail, the Czech SMS API, built on sendrail/sendrail-php:
Sendrailfacade and the SDK client in the container- notification channel
sendrail - middleware that verifies the signature and the age of incoming webhooks
Laravel 12 and 13, PHP 8.2 or newer (Laravel 13 needs PHP 8.3). Česky: níže.
Installation
composer require sendrail/laravel
The service provider and the facade are discovered automatically. Add the key to .env:
SENDRAIL_API_KEY=sr_test_…
SENDRAIL_WEBHOOK_SECRET=…
A key starting with sr_test_ sends nothing and charges nothing; use it in development and tests.
To change the timeout, the number of retries, the webhook tolerance or the API address, publish the config:
php artisan vendor:publish --tag=sendrail-config
Sending
use Sendrail\Laravel\Facades\Sendrail; $message = Sendrail::send('+420777123456', 'Your order is ready.', idempotencyKey: "order-{$order->id}-ready"); Sendrail::messages()->sendTemplate('+420777123456', 'order-ready', ['name' => 'Jana']); Sendrail::messages()->get($message['id']); foreach (Sendrail::messages()->iterate(status: 'failed') as $failed) { // every failed message of the key, newest first, with its text } $verification = Sendrail::verify()->send('+420777123456', app: config('app.name')); Sendrail::verify()->isValid($verification['id'], $request->input('code')); Sendrail::numbers()->check('777 123 456'); Sendrail::account()->get();
Sendrail::messages(), verify(), numbers(), optOuts(), templates(), account(), prices(), inbound() and webhook() return the resources of the SDK; everything they can do, and the exceptions they throw, is in the SDK README. You can also type-hint Sendrail\Client anywhere the container injects.
Notifications
use Illuminate\Notifications\Notification; use Sendrail\Laravel\SendrailMessage; class OrderReady extends Notification { public function via(object $notifiable): array { return ['sendrail']; } public function toSendrail(object $notifiable): SendrailMessage { return SendrailMessage::create("Order {$this->order->number} is ready for pickup."); } }
The notifiable tells where to send:
class User extends Authenticatable { use Notifiable; public function routeNotificationForSendrail(Notification $notification): ?string { return $this->phone; // international format, +420777123456 } }
toSendrail() may return a plain string too. SendrailMessage also has:
SendrailMessage::create()->template('order-ready', ['name' => $notifiable->name]) // a stored template ->to('+420777123456') // overrides the notifiable's number ->idempotencyKey('order-1042') // your own key ->bulk() // mass mailing ->dryRun(); // check and price only
Without a number nothing is sent. On-demand: Notification::route('sendrail', '+420777123456')->notify(new OrderReady($order)).
Unless you set your own, the idempotency key is the id of the notification. It is unique per recipient and stays the same when a queued notification is retried, so a retried job does not send the SMS twice. Errors of the API are thrown as SDK exceptions (Sendrail\Exception\ApiException), which fails the queue job as usual.
Webhooks
Set the webhook address on the key in the Sendrail dashboard and put its signing secret into SENDRAIL_WEBHOOK_SECRET. The sendrail.webhook middleware lets through only requests with a valid X-Sendrail-Signature that are not older than 300 seconds (401 otherwise):
use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use Sendrail\Laravel\Http\Middleware\VerifyWebhookSignature; use Sendrail\Webhook\Event; Route::post('/webhooks/sendrail', function (Request $request) { $event = VerifyWebhookSignature::event($request); // the id is the same for every repeat of one event: handle each only once if (! Cache::add("sendrail:{$event->id}", true, now()->addDays(2))) { return response()->noContent(); } if ($event->is(Event::MESSAGE_FINAL)) { // $event->data['id'], $event->data['status']: sent, failed, cancelled, rejected } return response()->noContent(); })->middleware('sendrail.webhook');
Put the route into routes/api.php, or exclude it from CSRF protection. Events: message.final, message.received, call.missed, optout.created.
Repeats. Answer within 5 seconds (dispatch a job for slow work), otherwise Sendrail posts the event again. The same event can therefore arrive more than once; deduplicate by $event->id (evt_…) as above, or with a unique database column.
Replays. Every webhook carries a signed timestamp of the delivery attempt. The middleware refuses a request whose timestamp differs from the server clock by more than sendrail.webhook_tolerance seconds, so a recorded request cannot be sent again later:
.env |
Default | |
|---|---|---|
SENDRAIL_WEBHOOK_TOLERANCE |
300 |
allowed age in seconds; 0 switches the check off |
SENDRAIL_WEBHOOK_ALLOW_MISSING_TIMESTAMP |
false |
also accept correctly signed bodies without a timestamp (old format); leave it off |
A wrong signature and a stale or missing timestamp answer 401, a signed body that is not an event 400. $event->timestamp is the Unix time of the delivery.
Testing your application
Notification::fake() works as with any channel. To exercise real calls without the network, bind your own PSR-18 client:
$this->app->instance(\Sendrail\Laravel\SendrailServiceProvider::HTTP_CLIENT, $mockPsr18Client);
Česky
Integrace českého SMS API Sendrail do Laravelu 12 a 13.
composer require sendrail/laravel
Do .env přidejte SENDRAIL_API_KEY (testovací klíč sr_test_… nic neodešle ani neúčtuje) a pro webhooky SENDRAIL_WEBHOOK_SECRET (podpisový klíč webhooku z administrace).
use Sendrail\Laravel\Facades\Sendrail; Sendrail::send('+420777123456', 'Objednávka je připravena.', idempotencyKey: "objednavka-{$order->id}");
- Notifikace: kanál
sendrail, metodatoSendrail()vracíSendrailMessagenebo text, číslo dodárouteNotificationForSendrail(). - Webhook: middleware
sendrail.webhookověří podpisX-Sendrail-Signaturea stáří požadavku (výchozí tolerance 300 s,SENDRAIL_WEBHOOK_TOLERANCE,0= vypnuto), událost vrátíVerifyWebhookSignature::event($request). Stejná událost může přijít víckrát, deduplikujte podle$event->id. - Chyby API jsou výjimky
Sendrail\Exception\ApiExceptionshttpStatusaerrorCode.
Dokumentace API: https://sendrail.cz/docs. Podpora: info@sendrail.cz.
Development
composer install
composer check # composer validate, PHPStan, PHPUnit
License
MIT, see LICENSE.