symfony/vonage-notifier
Symfony Notifier bridge for Vonage. Send SMS notifications via Vonage using a simple DSN (vonage://KEY:SECRET@default?from=FROM). Configure your key, secret, and sender ID, then use Symfony’s notification system to deliver messages.
Install the package:
composer require symfony/vonage-notifier
Configure DSN in .env:
VONAGE_DSN=vonage://KEY:SECRET@default?from=FROM
Replace KEY, SECRET, and FROM with your Vonage credentials and sender ID.
First use case: Send an SMS notification via Symfony Notifier:
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
$notifier = new NotifierInterface();
$message = new SmsMessage('Hello from Laravel!', 'recipient@example.com');
$notifier->send($message);
hash_equals() for signature validation (see Gotchas).Use Symfony Notifier’s message types with Vonage transport:
// SMS
$message = new SmsMessage('Your OTP is 12345', '+1234567890');
$notifier->send($message);
// Voice call
$message = new VoiceMessage('Hello, this is a test call', '+1234567890');
$notifier->send($message);
// Email (if using Vonage's email API)
$message = new Email('welcome@example.com', 'Welcome!', 'Hello!');
$notifier->send($message);
Leverage Symfony Messenger for background jobs:
use Symfony\Component\Messenger\MessageBusInterface;
$bus->dispatch(new SendSmsMessage('Hello', '+1234567890'));
Define a handler:
#[AsMessageHandler]
public function __invoke(SendSmsMessage $message)
{
$notifier->send(new SmsMessage($message->content, $message->to));
}
Use Vonage’s template system for dynamic content:
$message = new SmsMessage(
'Your balance is {{balance}}',
'+1234567890',
['balance' => 100.50]
);
For Vonage callbacks (e.g., delivery receipts), validate signatures securely:
use Symfony\Component\Notifier\Bridge\Vonage\Webhook\VonageWebhookValidator;
$validator = new VonageWebhookValidator('your-secret');
if ($validator->isValidSignature($request->getContent(), $request->headers->get('X-Vonage-Signature'))) {
// Process webhook (e.g., update notification status in DB)
}
Service Provider Setup:
public function register()
{
$this->app->bind(NotifierInterface::class, function ($app) {
return new Notifier([
new VonageTransport($app['config']['vonage.dsn']),
]);
});
}
Notification Channels:
Extend VonageChannel for custom logic:
use Symfony\Component\Notifier\Bridge\Vonage\VonageChannel;
class CustomVonageChannel extends VonageChannel
{
protected function getOptions(): array
{
return ['template' => 'custom_template'];
}
}
Queued Notifications: Use Laravel Queues with Symfony Messenger:
$bus->dispatch(new SendSmsMessage('Hello'))->setOnQueue('vonage');
$message = new MultipartMessage();
$message->addPart(new SmsMessage('SMS part', '+1234567890'));
$message->addPart(new Email('email@example.com', 'Email part', 'Hello!'));
Webhook Signature Validation:
hash_equals() as shown in the webhook example.// UNSAFE: Timing attack risk
if (hash_hmac('sha256', $content, $secret) === $signature) { ... }
// SAFE: Use hash_equals
if (hash_equals(hash_hmac('sha256', $content, $secret), $signature)) { ... }
DSN Configuration:
from parameter in DSN may cause failed deliveries.from) matches Vonage’s registered numbers/emails.VONAGE_DSN=vonage://KEY:SECRET@default?from=+1234567890 # Must match Vonage's registered number
Rate Limits:
Character Limits:
Webhook URLs:
Enable Debugging:
$transport = new VonageTransport($dsn, [
'debug' => true, // Logs requests/responses
]);
Check Vonage Dashboard:
Log Failures:
$notifier->send($message)->then(
null,
function (Throwable $e, $message) {
\Log::error("Failed to send {$message->getTransport()}: " . $e->getMessage());
}
);
Test with Sandbox: Use Vonage’s sandbox environment for testing:
VONAGE_DSN=vonage://KEY:SECRET@sandbox?from=VonageSMS
Custom Transport:
Extend VonageTransport to add features like:
Message Transformers:
Override VonageMessage to modify payloads before sending:
class CustomVonageMessage extends VonageMessage
{
public function getBody(): string
{
return parent::getBody() . "\n-- Custom footer";
}
}
Webhook Middleware: Add middleware to Vonage webhook handlers for:
Laravel Notifications: Create a custom channel:
use Illuminate\Notifications\VonageChannel;
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransport;
class CustomVonageChannel extends VonageChannel
{
public function __construct(VonageTransport $transport)
{
parent::__construct($transport);
}
}
DSN Format:
vonage://KEY:SECRET@transport?from=FROM.default (SMS/voice), email (if enabled).Environment Variables:
.env or Symfony’s %env(VONAGE_DSN)%.Timeouts:
$transport = new VonageTransport($dsn, [
'timeout' => 60, // 60 seconds
]);
Region-Specific Endpoints:
us, eu). Specify if needed:
VONAGE_DSN=vonage://KEY:SECRET@us?from=+12345678
How can I help you explore Laravel packages today?