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 Dependencies:
composer require symfony/notifier symfony/smsapi-notifier
For Laravel, ensure compatibility with Symfony components (e.g., via spatie/laravel-symfony-messenger if using queues).
Configure DSN:
Add to .env:
SMSAPI_DSN=smsapi://YOUR_TOKEN@default?from=YOUR_SENDER&fast=0&test=0
For smsapi.com, use:
SMSAPI_DSN=smsapi://YOUR_TOKEN@api.smsapi.com?from=YOUR_SENDER
First Use Case: Send a test SMS via a Laravel command or controller:
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
public function sendTestSms(NotifierInterface $notifier) {
$message = new SmsMessage('Hello from Laravel!', '1234567890');
$notifier->send($message);
}
Bind NotifierInterface in Laravel’s service container (e.g., via AppServiceProvider).
Symfony Notifier Integration:
config/services.php or a Symfony-compatible config file:
$notifier = new Notifier([
new SmsapiTransport(env('SMSAPI_DSN')),
]);
event(new SmsSentEvent($message));
Or directly:
$notifier->send(new SmsMessage('OTP: 1234', $user->phone));
Laravel-Specific Patterns:
AppServiceProvider:
$this->app->singleton(NotifierInterface::class, function ($app) {
return new Notifier([new SmsapiTransport(env('SMSAPI_DSN'))]);
});
spatie/laravel-symfony-messenger to async SMS:
$message = new SmsMessage('Your order is confirmed!', $user->phone);
$this->bus->dispatch($message);
Dynamic Configuration:
$dsn = config('services.smsapi.test_mode') ?
'smsapi://TOKEN@default?test=1' :
env('SMSAPI_DSN');
Template Management:
Use SMSAPI’s templates via custom SmsMessage extensions:
class TemplatedSmsMessage extends SmsMessage {
public function __construct(string $templateName, array $params, string $to) {
parent::__construct($this->renderTemplate($templateName, $params), $to);
}
}
Event Listeners: Track SMS delivery status:
public function handle(SentMessage $event) {
if ($event->getMessage() instanceof SmsMessage) {
Log::info('SMS sent to ' . $event->getMessage()->getRecipients()[0]);
}
}
Fallback Mechanisms: Combine with other transports (e.g., email) for resilience:
$notifier = new Notifier([
new SmsapiTransport(env('SMSAPI_DSN')),
new EmailTransport(env('MAILER_DSN')),
]);
DSN Configuration:
from= in DSN causes "eco" sender (may be blocked by carriers).
Fix: Always specify from=YOUR_SENDER.test=1 mode doesn’t send real SMS but may still count against quotas.
Fix: Disable in production (test=0).Rate Limiting:
try {
$response = $client->post(...);
} catch (RateLimitException $e) {
sleep(2 ** $retryCount);
}
Message Length:
Recipient Validation:
+123) may fail silently. Tip:
Validate with a regex before sending:
preg_match('/^\+[0-9]{10,15}$/', $phone);
Enable Logging:
Configure Symfony’s logger in config/logging.php to capture SMSAPI responses:
'channels' => [
'smsapi' => [
'driver' => 'single',
'path' => storage_path('logs/smsapi.log'),
'level' => 'debug',
],
],
Test Mode:
Use test=1 in DSN to validate payloads without sending:
SMSAPI_DSN=smsapi://TOKEN@default?test=1
API Errors:
SMSAPI returns HTTP codes (e.g., 400 for invalid tokens). Tip:
Extend SmsapiTransport to map errors:
public function send(SmsMessage $message): void {
try {
parent::send($message);
} catch (ClientException $e) {
throw new \RuntimeException('SMSAPI Error: ' . $e->getResponse()->getBody());
}
}
Custom Transports:
Extend SmsapiTransport for additional SMSAPI features (e.g., webhooks):
class CustomSmsapiTransport extends SmsapiTransport {
public function __construct(string $dsn, array $options = []) {
parent::__construct($dsn, $options + ['webhook_url' => 'https://your-app.com/sms-webhook']);
}
}
Laravel Notifications:
Create a custom SmsChannel for Laravel’s notification system:
use Illuminate\Notifications\Notification;
class SmsNotification extends Notification {
public function via($notifiable) {
return ['sms'];
}
public function toSms($notifiable) {
return (string) $this->message;
}
}
Monitoring: Integrate with Laravel Telescope or Prometheus:
// Track SMS metrics
Telescope::log('sms.sent', ['to' => $phone, 'status' => 'success']);
Dsn::getBooleanOption() for flags like fast:
$fast = $dsn->getBooleanOption('fast'); // Returns bool, not string
SMSAPI_DSN=smsapi://TOKEN@staging.smsapi.pl?from=TEST
How can I help you explore Laravel packages today?