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.
- PHP 8.2 or higher
- Laravel 10.x or higher
You can install the package via Composer:
composer require creativecrafts/laravel-twillio-sms
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
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.";
}
public static function init(): self
Creates a new instance of the LaravelTwillioSms class.
Sets the ‘from’ phone number for the SMS messages.
Sets the recipient’s phone number for the SMS message.
Sets the content of the SMS message.
Sends the SMS message. Returns true if the message is sent successfully, or throws a TwilioException if an error occurs.
- Deprecated. Use send() instead. This method sends an SMS using the provided number and message directly.
• getPhoneNumber(): string
• getMessage(): string
• getSentFrom(): string
Validates the recipient’s phone number using Twilio’s Lookup API.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.