Laravel is a powerful and popular PHP framework used by developers around the world to create robust and scalable web applications. In this blog post, we’ll be discussing how to send SMS messages using Twilio in Laravel 10.
Twilio is a cloud communications platform that provides a wide range of APIs for sending and receiving SMS messages, making phone calls, and more. With its easy-to-use APIs and robust documentation, Twilio is a great choice for developers looking to integrate SMS messaging into their Laravel applications.
Before we begin, make sure you have a Twilio account and have created an API key and secret. Once you have these credentials, you’re ready to get started!
Step 1: Install the Twilio PHP SDK
The first step is to install the Twilio PHP SDK via Composer. Open your terminal and navigate to your Laravel project directory, then run the following command:
composer require twilio/sdk
This command will download and install the Twilio PHP SDK and its dependencies.
Step 2: Configure the Twilio credentials in Laravel
Next, we need to configure our Twilio credentials in Laravel. Open the .env
file in your Laravel project and add the following lines:
TWILIO_ACCOUNT_SID=your_account_sid_here
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_FROM_NUMBER=your_twilio_phone_number_here
Replace your_account_sid_here
, your_auth_token_here
, and your_twilio_phone_number_here
with your Twilio account SID, auth token, and phone number, respectively.
Step 3: Create a Controller
Now, let’s create a new controller to handle our SMS sending functionality. In your terminal, run the following command to generate a new controller:
php artisan make:controller SmsController
Open the SmsController.php
file and add the following code to the top of the file to import the necessary classes:
use Twilio\Rest\Client;
use Illuminate\Http\Request;
Then, add the following code to your sendSms()
function:
public function sendSms(Request $request)
{
$client = new Client(env('TWILIO_ACCOUNT_SID'), env('TWILIO_AUTH_TOKEN'));
$client->messages->create(
$request->input('to'),
[
'from' => env('TWILIO_FROM_NUMBER'),
'body' => $request->input('message'),
]
);
return response()->json(['status' => 'success']);
}
This code creates a new Twilio client using our Twilio account SID and auth token, then sends an SMS message using the messages
API. The to
and message
fields are taken from the request input.
Step 4: Create a Route
Finally, we need to create a route to our sendSms()
function. Open the routes/web.php
file in your Laravel project and add the following code:
Route::post('/send-sms', 'SmsController@sendSms');
This code creates a new route that listens for POST
requests to /send-sms
and maps them to our sendSms()
function in the SmsController
.
Step 5: Test it out!
That’s it! We’re now ready to test out our SMS sending functionality. To do so, we can use a tool like Postman to send a POST
request to our /send-sms
endpoint with the following parameters:
to: <recipient phone number>
message: <SMS message body>
Once you send the request, you should receive an SMS message at the specified phone number!
Conclusion
Sending SMS messages using Twilio in Laravel 10 is a straightforward process. By leveraging the power of Twilio’s API and Laravel’s ease of use, you can easily add SMS functionality to your application. Whether you’re building a small web application or a large-scale enterprise system, Twilio and Laravel make it easy to send SMS messages.