symfony/message-bird-notifier
Symfony Notifier bridge for MessageBird SMS. Configure via MESSAGEBIRD_DSN (token, from sender) and send SmsMessage notifications. Supports MessageBirdOptions to set SMS API options like scheduling, validity, report URL, and URL shortening.
Install Dependencies:
composer require symfony/notifier message-bird/notifier
Note: Laravel’s native symfony/notifier may conflict; use symfony/notifier directly or create a custom bridge.
Configure .env:
MESSAGEBIRD_DSN=messagebird://YOUR_TOKEN@default?from=YourSender
Replace YOUR_TOKEN with your MessageBird API key and YourSender with a valid sender ID (e.g., SMS-12345).
Register Transport in Laravel:
Create a service provider (e.g., app/Providers/MessageBirdServiceProvider.php):
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Transport\MessageBirdTransport;
public function register()
{
$notifier = new Notifier([
new MessageBirdTransport(env('MESSAGEBIRD_DSN')),
]);
$this->app->singleton('notifier', fn() => $notifier);
}
First Use Case: Send an SMS
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\NotifierInterface;
public function sendWelcomeSms()
{
$notifier = app(NotifierInterface::class);
$message = new SmsMessage('+1234567890', 'Welcome to our app!');
$notifier->send($message);
}
Laravel Integration (Optional):
Extend Laravel’s Notification facade by creating a custom channel:
// app/Channels/MessageBirdChannel.php
use Illuminate\Notifications\Notification;
use Symfony\Component\Notifier\NotifierInterface;
class MessageBirdChannel
{
public function __construct(private NotifierInterface $notifier) {}
public function send($notifiable, Notification $notification)
{
$message = $notification->toSms($notifiable);
$this->notifier->send($message);
}
}
Register the channel in config/notifications.php:
'channels' => [
'messagebird' => [
'driver' => 'messagebird',
],
],
Use directly in controllers or services:
$notifier = app(NotifierInterface::class);
$notifier->send(new SmsMessage('+1234567890', 'Hello!'));
Dispatch to Laravel Queues:
use Illuminate\Support\Facades\Bus;
Bus::dispatch(new SendSmsJob('+1234567890', 'Hello!'));
Job Implementation:
use Symfony\Component\Notifier\NotifierInterface;
class SendSmsJob implements ShouldQueue
{
public function __construct(
private string $phone,
private string $message,
) {}
public function handle(NotifierInterface $notifier)
{
$notifier->send(new SmsMessage($this->phone, $this->message));
}
}
Trigger via Laravel Events:
// In an event listener
public function handle(UserRegistered $event)
{
$notifier = app(NotifierInterface::class);
$notifier->send(new SmsMessage(
$event->user->phone,
"Your account is ready! Verify at: {$event->verificationUrl}"
));
}
Use Symfony’s Message classes with templates:
$message = new SmsMessage(
'+1234567890',
'Your order #{{ orderId }} is shipping!'
);
$message->options((new MessageBirdOptions())->reference('order_123'));
Extend MessageBirdTransport for additional logic:
use Symfony\Component\Notifier\Transport\MessageBirdTransport as BaseTransport;
class CustomMessageBirdTransport extends BaseTransport
{
public function __construct(string $dsn, array $options = [])
{
parent::__construct($dsn, $options + ['custom_option' => true]);
}
protected function getOptions(SmsMessage $message): array
{
$options = parent::getOptions($message);
$options['custom_option'] = true;
return $options;
}
}
Configure retries in config/services.php:
'symfony.messenger.transports.messagebird' => [
'dsn' => env('MESSAGEBIRD_DSN'),
'options' => [
'retry_strategy' => [
'max_retries' => 3,
'delay' => 1000, // 1 second
],
],
],
Decorate the transport to log failures:
use Psr\Log\LoggerInterface;
class LoggingMessageBirdTransport implements TransportInterface
{
public function __construct(
private TransportInterface $transport,
private LoggerInterface $logger,
) {}
public function send(MessageInterface $message): void
{
try {
$this->transport->send($message);
} catch (\Throwable $e) {
$this->logger->error('MessageBird failed', [
'message' => $message,
'error' => $e->getMessage(),
]);
throw $e;
}
}
}
Combine with other Symfony transports (e.g., email):
$notifier = new Notifier([
new MessageBirdTransport(env('MESSAGEBIRD_DSN')),
new EmailTransport(env('MAILER_DSN')),
]);
DSN Format Sensitivity:
MESSAGEBIRD_DSN format (e.g., missing from parameter) will throw cryptic errors.MessageBirdTransport::getDsn() before use.Phone Number Validation:
+ or country code).libphonenumber to validate:
use libphonenumber\PhoneNumberUtil;
$phone = PhoneNumberUtil::getInstance()->parse($rawPhone);
Rate Limiting:
RetryStrategy.Async Deadlocks:
php artisan queue:work --sleep=3 --tries=3.Environment-Specific DSNs:
.env or a secrets manager.$dsn = env('MESSAGEBIRD_DSN');
if (!MessageBirdTransport::getDsn($dsn)) {
throw new \RuntimeException('Invalid MessageBird DSN');
}
Enable Symfony Notifier Debug Mode:
$notifier = new Notifier([$transport], [
'debug' => true,
]);
Logs will include raw API requests/responses.
Mock MessageBird in Tests:
Use symfony/notifier-bundle's test utilities:
use Symfony\Component\Notifier\Test\Transport\NullTransport;
$notifier = new Notifier([new NullTransport()]);
Check MessageBird API Status:
type: 'test' option to avoid billing:
$options = (new MessageBirdOptions())->type('test');
Handle API Errors Gracefully:
MessageBird returns HTTP errors (e.g., 400 Bad Request). Catch exceptions:
try {
$notifier->send($message);
} catch (\Symfony\Component\Notifier\Exception\TransportException $e) {
// Log or retry
}
How can I help you explore Laravel packages today?