macellan / twilio
Twilio SMS notification channel for Laravel
Installs: 2 507
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- illuminate/notifications: ^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
- twilio/sdk: ^7.0
Requires (Dev)
- orchestra/testbench: ^6.23|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.6|^10.0
README
This package makes it easy to send sms notifications using Twilio with Laravel 8.x, 9.x, 10.x
Contents
Installation
You can install this package via composer:
composer require macellan/twilio
Setting up the Twilio service
Add your Twilio configs to your config/services.php:
// config/services.php ... 'sms' => [ 'twilio' => [ 'account_sid' => env('TWILIO_ACCOUNT_SID', ''), 'auth_token' => env('TWILIO_AUTH_TOKEN', ''), 'from' => env('TWILIO_FROM', ''), 'enable' => env('TWILIO_ENABLE', false), 'debug' => env('TWILIO_DEBUG', false), // Will log sending attempts and results 'sandbox_mode' => env('TWILIO_SANDBOX_MODE', false), // Will not invoke API call ], ], ...
Usage
You can use the channel in your via() method inside the notification:
use Illuminate\Notifications\Notification; use Macellan\Twilio\TwilioSmsMessage; class TestNotification extends Notification { public function via($notifiable) { return ['twilio']; } public function toTwilio(object $notifiable): TwilioSmsMessage { return new TwilioSmsMessage('Twilio test message'); } }
In your notifiable model, make sure to include a routeNotificationForSms() method, which returns a phone number.
public function routeNotificationForSms() { return $this->phone; }
On-Demand Notifications
Sometimes you may need to send a notification to someone who is not stored as a "user" of your application. Using the Notification::route method, you may specify ad-hoc notification routing information before sending the notification:
Notification::route('sms', '+905554443322') ->notify(new TestNotification());
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.