Have you ever wondered how to send text messages (SMS) from a website or application using Twilio in Laravel 10? It's actually not that hard! In this guide, we'll show you step-by-step how to do just that.
So, if you want to learn how to make your website or app send SMS messages, keep reading! We'll break it down for you, making it easy to understand and follow along. Sending SMS with Twilio in Laravel 10 is a powerful way to reach your users via text messages.
In this tutorial, we'll demystify the process and help you get started.
So, let's dive into how to send SMS using Twilio in laravel 8, Laravel 9, and Laravel 10, Laravel 10 SMS integration.
If you haven't already, start by creating a new Laravel project using Composer:
composer create-project laravel/laravel your-project-name
cd your-project-name
- Go to the Twilio website (https://www.twilio.com/) and sign up for an account if you don't already have one.
- After signing in, obtain your Account SID and Auth Token from the Twilio Dashboard. You'll need these credentials to interact with the Twilio API.
In your Laravel project, open a terminal window. Install the Twilio PHP library using Composer by running this command,
composer require twilio/sdk
Open your Laravel project's .env
file.
Add the following lines and replace YOUR_TWILIO_ACCOUNT_SID
and YOUR_TWILIO_AUTH_TOKEN
with your Twilio credentials.
.env
TWILIO_ACCOUNT_SID=YOUR_TWILIO_ACCOUNT_SID
TWILIO_AUTH_TOKEN=YOUR_TWILIO_AUTH_TOKEN
TWILIO_FROM=YOUR_TWILIO_PHONE_NUMBER
Create a new controller using Artisan.
php artisan make:controller SMSController
Open the newly created SMSController.php
in your project's app/Http/Controllers
directory.
Add a method to send an SMS using Twilio. You can use the following example as a starting point.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Exception;
use Twilio\Rest\Client;
class SMSController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function sendSMS()
{
$receiverNumber = "RECEIVER_NUMBER";
$message = "Hello, this is Vidvatek SMS Tutorial!";
try {
$accountSid = getenv("TWILIO_ACCOUNT_SID");
$authToken = getenv("TWILIO_AUTH_TOKEN");
$twilioNumber = getenv("YOUR_TWILIO_PHONE_NUMBER");
$client = new Client($accountSid, $authToken);
$client->messages->create($receiverNumber, [
'from' => $twilioNumber,
'body' => $message
]);
echo('SMS Sent Successfully.');
} catch (Exception $e) {
dd("Error: ". $e->getMessage());
}
}
}
Open the routes/web.php
file.
Add a route that maps to your SMSController
and the sendSMS
method.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SMSController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/send-sms', [SMSController::class, 'sendSMS']);
Run the laravel application using the following command.
php artisan serve
You might also like:
- Read Also: How to Create Multiple Authentication in Laravel 10
- Read Also: How to use Unique Validation on Update in Laravel 10
- Read Also: How to Sorting Column in Table in Laravel 10
- Read Also: Laravel 10 one to many Relationship Example