laravel/vonage-notification-channel
Official Laravel notification channel for sending SMS via Vonage (formerly Nexmo). Integrates with Laravel’s Notifications system to deliver text messages to notifiable users using your Vonage credentials and configuration.
Installation:
composer require laravel/vonage-notification-channel
Publish the config file (if needed):
php artisan vendor:publish --provider="Laravel\Vonage\VonageServiceProvider" --tag="config"
Configuration:
Add Vonage credentials to .env:
VONAGE_KEY=your_api_key
VONAGE_SECRET=your_api_secret
VONAGE_SMS_FROM=YourSenderName
First Use Case:
Create a notification class extending VonageMessage:
use Laravel\Vonage\VonageMessage;
class OrderConfirmation extends VonageMessage
{
public function __construct(public string $orderId)
{
$this->to = 'recipient-phone-number';
$this->message = "Your order #{$this->orderId} is confirmed!";
}
}
Send via a user model:
$user->notify(new OrderConfirmation('ORD123'));
config/services.php (Vonage section)VonageMessage for SMS notifications.Sending SMS Notifications:
VonageMessage and define to (recipient) and message properties.notify() on a user model or directly via Notification::route():
Notification::route('vonage', $phoneNumber)->notify(new OrderConfirmation('ORD123'));
Dynamic Recipients:
route() to dynamically set recipients:
$user->route('vonage', $user->phone)->notify(new Notification());
Batch Sending:
$user->notify(new OrderConfirmation('ORD123'))->onQueue('sms');
Customizing Messages:
toVonage() in your notification class for advanced formatting:
public function toVonage($notifiable)
{
return [
'text' => "Custom: {$this->message}",
'from' => 'CustomSender',
];
}
libphonenumber).$user->route('mail', $user->email)->notify(new MultiChannelNotification());
.env):
VONAGE_SANDBOX=true
Phone Number Format:
+12125551234). Use Str::of($phone)->start('+') to ensure consistency.\Log::debug('Sending to:', ['phone' => $this->to]);
Rate Limits:
php artisan queue:work --sleep=3 --tries=3
Sandbox vs. Production:
+14155238886) won’t receive messages in production. Always test with real credentials in staging.Configuration Overrides:
VONAGE_SMS_FROM in notifications. Use the config or .env for flexibility.Enable Logging:
VONAGE_LOG_LEVEL=debug
Check storage/logs/laravel.log for API responses/errors.
Vonage API Errors:
401: Invalid API key/secret (check .env).400: Invalid phone number (validate format).try/catch in notifications to handle failures gracefully:
try {
$user->notify(new OrderConfirmation('ORD123'));
} catch (\Exception $e) {
\Log::error("Vonage failed: " . $e->getMessage());
}
Custom Vonage Client:
Override the default client in config/services.php:
'vonage' => [
'client' => \App\Services\CustomVonageClient::class,
],
Webhook Handling: Use Vonage’s inbound SMS to trigger Laravel events. Example:
// routes/web.php
Route::post('/vonage-webhook', [VonageWebhookController::class, 'handle']);
Template Management: Store SMS templates in the database and fetch them dynamically:
public function toVonage($notifiable)
{
$template = Template::find($this->templateId);
return ['text' => $template->body];
}
Multi-Language Support: Localize messages using Laravel’s localization:
$message = __("notifications.order.confirmed", ['id' => $this->orderId]);
How can I help you explore Laravel packages today?