symfony/smsapi-notifier
Symfony Notifier bridge for SMSAPI (smsapi.pl / smsapi.com). Send SMS using an OAuth token via DSN config, with options for sender name, fast delivery priority, and test mode.
Install the Package:
composer require symfony/smsapi-notifier
If using Laravel with Symfony Notifier (e.g., via spatie/laravel-notifier), ensure symfony/notifier is also installed:
composer require symfony/notifier spatie/laravel-notifier
Configure the DSN:
Add the SMSAPI DSN to your .env:
SMSAPI_DSN=smsapi://YOUR_API_TOKEN@default?from=YOUR_SENDER&fast=0&test=0
For smsapi.com, use:
SMSAPI_DSN=smsapi://YOUR_API_TOKEN@api.smsapi.com?from=YOUR_SENDER
First Use Case: Send an SMS via Symfony Notifier (if integrated):
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\NotifierInterface;
$notifier = new NotifierInterface(); // Assume bound to Laravel's container
$message = new SmsMessage('Hello via SMSAPI!', 'recipient@example.com');
$notifier->send($message);
For Laravel without Symfony Notifier, wrap the DSN in a custom transport:
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Transport\SmsapiTransport;
$dsn = new Dsn('smsapi://TOKEN@default?from=SENDER');
$transport = new SmsapiTransport($dsn);
$transport->send(new SmsMessage('Test', '123456789'));
Sending SMS Notifications:
SmsMessage with Symfony’s NotifierInterface:
$notifier->send(
new SmsMessage('Your OTP: 1234', 'user@example.com')
);
// app/Facades/SmsNotifier.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class SmsNotifier extends Facade {
protected static function getFacadeAccessor() { return 'sms.notifier'; }
}
Bind the notifier in AppServiceProvider:
$this->app->singleton('sms.notifier', function ($app) {
return new NotifierInterface([new SmsapiTransport(new Dsn(env('SMSAPI_DSN')))]);
});
Usage:
SmsNotifier::send(new SmsMessage('Hello', 'user@example.com'));
Environment-Specific Config:
Use Laravel’s .env for DSN variations:
SMSAPI_DSN_PROD=smsapi://PROD_TOKEN@api.smsapi.com?from=PROD_SENDER&fast=1
SMSAPI_DSN_STAGING=smsapi://STAGING_TOKEN@default?test=1
Dynamically resolve in AppServiceProvider:
$dsn = new Dsn(env('SMSAPI_DSN_' . config('app.env')));
Async Delivery with Queues: Dispatch SMS jobs via Laravel Queues:
use App\Jobs\SendSmsJob;
SendSmsJob::dispatch('Hello', 'user@example.com');
Job implementation:
// app/Jobs/SendSmsJob.php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\NotifierInterface;
class SendSmsJob {
use Queueable;
public function handle(NotifierInterface $notifier) {
$notifier->send(new SmsMessage($this->message, $this->recipient));
}
}
Event-Driven Extensions:
Listen to SmsMessageSent events for analytics or retries:
// app/Listeners/SmsSentListener.php
namespace App\Listeners;
use Symfony\Component\Notifier\EventListener\SmsMessageSent;
use Symfony\Component\Notifier\Message\SentMessage;
class SmsSentListener {
public function onSmsSent(SmsMessageSent $event) {
$sentMessage = $event->getSentMessage();
if ($sentMessage instanceof SentMessage) {
logger()->info('SMS sent', [
'message_id' => $sentMessage->getMessageId(),
'recipient' => $sentMessage->getRecipient(),
]);
}
}
}
Register the listener in EventServiceProvider:
protected $listeners = [
SmsMessageSent::class => [
SmsSentListener::class,
],
];
Laravel-Symfony Bridge:
Use spatie/laravel-symfony-messenger to integrate Symfony Messenger with Laravel:
composer require spatie/laravel-symfony-messenger
Configure in config/services.php:
'messenger' => [
'transports' => [
'smsapi' => [
'dsn' => env('SMSAPI_DSN'),
],
],
],
Testing: Mock the transport in PHPUnit:
use Symfony\Component\Notifier\Transport\MockTransport;
$transport = new MockTransport();
$notifier = new NotifierInterface([$transport]);
$notifier->send(new SmsMessage('Test', '123'));
$this->assertCount(1, $transport->getSentMessages());
Fallback Mechanisms: Combine with other transports (e.g., email) for multi-channel notifications:
$notifier->send(
new SmsMessage('SMS fallback: Email sent', 'user@example.com'),
new EmailMessage('Primary email', 'user@example.com', 'user@example.com')
);
Rate Limiting:
Use Laravel’s throttle middleware or spatie/laravel-circuit-breaker:
use Spatie\CircuitBreaker\CircuitBreaker;
CircuitBreaker::new('smsapi', 5, 60)->run(function () {
$notifier->send(new SmsMessage('Rate-limited test', '123'));
});
DSN Misconfiguration:
from= or test= parameters in the DSN.test=1).Symfony Notifier Dependency Conflicts:
symfony/notifier and symfony/smsapi-notifier.composer.json:
"symfony/notifier": "^6.0",
"symfony/smsapi-notifier": "^6.0"
Missing messageId in SentMessage:
messageId in SentMessage.symfony/smsapi-notifier:^7.3.0 or manually set it:
$sentMessage->setMessageId($response['messageId']);
Laravel’s env() Caching:
.env changes not reflected due to Laravel’s cached config.php artisan config:clear
SMSAPI API Rate Limits:
429 Too Many Requests responses.use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Transport\TransportInterface;
class RetryableSmsapiTransport implements TransportInterface {
public function send(SmsMessage $message) {
$retries = 3;
while ($retries--) {
try {
$this->transport->send($message);
break;
} catch (TransportException $e) {
if ($retries === 0) throw $e;
sleep(2 ** $retries);
}
}
}
}
Enable Symfony Notifier Debug:
Set the NOTIFIER_DEBUG env var:
NOTIFIER_DEBUG=1
Logs will include transport details.
Inspect Raw API Responses: Extend the transport to log requests/responses:
class
How can I help you explore Laravel packages today?