creativecrafts / laravel-twillio-sms
A simple package to send sms messages using twillio
Requires
- php: ^8.2
- illuminate/contracts: ^11.0|^10.0
- spatie/laravel-package-tools: ^1.16
- twilio/sdk: ^7.7
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- mockery/mockery: ^1.6
- nunomaduro/collision: ^8.1|^7.0
- orchestra/testbench: ^9.0|^8.0
- pestphp/pest: ^3.2|^2.35.1
- pestphp/pest-plugin-arch: ^3.0|^2.7
- pestphp/pest-plugin-laravel: ^3.0|^2.4
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- rector/rector: ^1.2
- rector/swiss-knife: ^1.0
- symplify/easy-coding-standard: ^12.3.5
README
Laravel Twilio SMS
Laravel Twilio SMS is a simple, elegant package for sending SMS messages using the Twilio API within your Laravel applications. This package allows you to validate phone numbers and send SMS messages effortlessly.
Table of Contents
Requirements
- PHP 8.2 or higher
- Laravel 10.x or higher
Installation
You can install the package via Composer:
composer require creativecrafts/laravel-twillio-sms
Configuration
After installing the package, you need to publish the configuration file:
php artisan vendor:publish --tag="twillio-sms-config"
This will create a config/twillio-sms.php file where you can set up your Twilio credentials:
return [ 'account_sid' => env('TWILIO_ACCOUNT_SID'), 'auth_token' => env('TWILIO_AUTH_TOKEN'), 'sms_from' => env('TWILIO_SMS_FROM'), // The Lookup v2 API allows you to query information on a phone number so that you can make a trusted interaction with your user. // With this, you can format and validate phone numbers with the free Basic Lookup request // and add on data packages to get even more in-depth carrier and caller information. // currently supported fields: 'line_type_intelligence', 'sms_pumping_risk' for more information 'phone_number_lookup' => [ 'fields' => [ 'line_type_intelligence', 'sms_pumping_risk', ], 'sms_pumping_risk' => [ // low risk: 0 - 59 // mild risk: 60 - 74 // moderate risk: 75 - 89 // high risk: 90 - 100 // visit https://www.twilio.com/docs/lookup/v2-api/sms-pumping-risk for more information 'max_allowed_sms_pumping_risk_score' => 59, ], ], ];
Ensure you have the following environment variables set in your .env file:
TWILIO_ACCOUNT_SID=your_twilio_account_sid TWILIO_AUTH_TOKEN=your_twilio_auth_token TWILIO_SMS_FROM=your_twilio_phone_number
Usage
To send an SMS, you can create an instance of the LaravelTwillioSms class and chain the methods to set the recipient’s phone number, the message content, and the sender’s number.
Example
use CreativeCrafts\LaravelTwillioSms\LaravelTwillioSms; $sms = LaravelTwillioSms::init() ->setPhoneNumber('+1234567890') ->setMessage('Hello, this is a test message!') ->send(); if ($sms) { echo "Message sent successfully!"; } else { echo "Failed to send the message."; }
Method Reference
init()
public static function init(): self
Creates a new instance of the LaravelTwillioSms class.
setFrom(string $from): self
Sets the ‘from’ phone number for the SMS messages.
setPhoneNumber(string $number): self
Sets the recipient’s phone number for the SMS message.
setMessage(string $message): self
Sets the content of the SMS message.
send(): bool
Sends the SMS message. Returns true if the message is sent successfully, or throws a TwilioException if an error occurs.
sendSms(string $number, string $message): bool
- Deprecated. Use send() instead. This method sends an SMS using the provided number and message directly.
Getters
• getPhoneNumber(): string
• getMessage(): string
• getSentFrom(): string
validatePhoneNumber(): bool
Validates the recipient’s phone number using Twilio’s Lookup API.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.