I will guide you through a step-by-step tutorial on how to send SMS using Twilio in Laravel 10. Additionally, I will demonstrate how to implement Twilio SMS notifications in Laravel 10, explaining each step thoroughly. If you have any inquiries regarding sending SMS to mobile using Twilio in Laravel 10, I will provide a simple example with a solution to address your concerns.
Founded in 2008 and based in San Francisco, California, Twilio is a leading cloud communications platform. It provides developers with the ability to enhance their applications by integrating various communication channels, such as voice, SMS, and video. Twilio's Application Programming Interfaces (APIs) serve as a bridge, allowing developers to easily incorporate these communication channels into their applications. This empowers developers to effortlessly create applications that can send and receive SMS messages, make and receive phone calls, and offer a wide range of other communication functionalities.
Following the steps below to completed the task
Suppose you have installed Laravel framework. If you are not yet, please read this to know how to install Laravel project.
Firstly we have to create and add a phone number and you will obtain the account SID, Token, and Number. You can create an account by visiting the following website: www.twilio.com. Next, add a Twilio phone number. Finally, retrieve the account SID, Token, and Number, and add them to the .env file as shown below:
TWILIO_SID=XXXXXXXXXXXXXXXXX
TWILIO_TOKEN=XXXXXXXXXXXXX
TWILIO_FROM=+XXXXXXXXXXX
In this step, we will install twilio/sdk composer package to use Twilio API. Let's run below command:
composer require twilio/sdk
We are going to create TwilioController and write some logic that we can send sms. Let's run below command to generate controller file
php artisan help make:controller TwilioController
Open file app -> Http -> Controllers -> TwilioController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Twilio\Rest\Client;
use Exception;
class TwilioController extends Controller
{
public function index()
{
$receiverNumber = "RECEIVER_NUMBER";
$message = "This is your OTP password xxxx do not share with other.";
try
{
$acc_sid = getenv("TWILIO_SID");
$auth_token = getenv("TWILIO_TOKEN");
$twilio_number = getenv("TWILIO_FROM");
$client = new Client($acc_sid, $auth_token);
$client->messages->create($receiverNumber, [
'from' => $twilio_number,
'body' => $message]);
dd('SMS Sent');
} catch (Exception $e)
{
dd("Error: ". $e->getMessage());
}
}
}
We have create controller file then we have to match URL with class controller in route file. so let's add new route to web.php file as bellow:
Open file routes -> web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TwilioController;
/*
|--------------------------------------------------------------------------
| 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', [TwilioController::class, 'index']);
Finally, we can run artisan command bellowing we are going to start the Laravel server with following command.
php artisan serve
Go to URL http://localhost:8000/ will making request and send to the receiver phone number.
Hope it would help have a nice day
You might also like
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him