How to send SMS in laravel 9 using Twilio SMS API-Webjourney

How to send SMS in laravel 9 using Twilio SMS API-Webjourney

Dear friends today's tutorial we will learn how to send SMS to a phone number using the twilio SMS API with laravel 9. Before sending sms we have to create an account in twilio official website. Now we need to follow the steps to send SMS successfully.

1. Create Twilio Account.

2. Setup Project.

3. Install twilio/sdk Package.

4. Create Routes.

5. Create Controller.

6. Create blade file.

7. Test App
    

 

Step-1: Create Twilio Account.

 

Click on the bellow link to register your account.

Twilio Signup Link 

Once the registration have completed we will get all the required credentials from twilio. 

 

 

                                                                                                                                                                                    

 

Step-2. Setup Project.

 

Create a fresh project sms-send using the artisan command or use your existing project.

composer create-project laravel/laravel sms-send

 

Now open .env file and paste the bellow code with your twilio credentials. This info not work for you, use your own information provided by twilio.

.env

TWILIO_SID=AC7aa73bf7f150253eb2973cc7cf0efak8
TWILIO_TOKEN=288dc0785572eefdadcc5cb7ea3f4dcd
TWILIO_FROM=+16292764067

 

Step-3. Install twilio/sdk Package.

 

Open your terminal and go to the project for install the sdk package using the bellow command.

composer require twilio/sdk

 

 

Step-4. Create Routes.

 

Paste the bellow routes inside routes\web.php

routes\web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SmsController;


Route::get('/',[SmsController::class,'sms_page']);
Route::post('/send-sms',[SmsController::class,'send_sms'])->name('send.sms');

 

Step-5. Create Controller.

 

Run the following command to create a controller

php artisan make:controller SmsController

 

Now paste the code in SmsController.

Note: don't forget to use Exception and Client at the top of the SmsController class.

app\Http\Controllers\SmsController

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Exception;
use Twilio\Rest\Client;

class SmsController extends Controller
{
    public function sms_page()
    {
        return view('sms_page');
    }

    public function send_sms(Request $request)
    {
        $receiver_number = $request->number;
        $message = 'SMSFrom Web Journey';
        try {
            $account_sid = getenv("TWILIO_SID");
            $auth_token = getenv("TWILIO_TOKEN");
            $twilio_number = getenv("TWILIO_FROM");
  
            $client = new Client($account_sid, $auth_token);
            $client->messages->create($receiver_number,[
                'from' => $twilio_number, 
                'body' => $message
            ]);
            return redirect()->back(); 
        }catch (Exception $e) {
            //
        }
    }
}

 

 

Step-6. Create blade file.

 

Now we will create a blade file sms_page.blade.php inside resources\views and paste the following code.

resources\views\sms_page.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How to Send SMS Using Laravel 9 - webjourny.dev</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container text-center mt-5">
        <h2>How to Send SMS Using Laravel 9</h2>
        <hr style="color:#ff0000;height:5px;">
        <div class="row">
        	<div class="col-md-3"></div>
            <div class="col-md-6">
                <form action="{{ route('send.sms') }}" method="post">
                    @csrf
                    <div class="input-group mb-3">
                        <input type="text" class="form-control" name="number" placeholder="Recipient's Phone Number" aria-label="Recipient's username" aria-describedby="button-addon2">
                        <button class="btn btn-dark" type="submit">Send SMS</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>

 

 

Stpe-7. Test App

 

Start your development server using the artisan command.

php artisan serve

 

 

 

 

 

Enter your real number in the input field and hit Send SMS button. Don't forget to verified your phone number in twilio account.

 

 

 

Hope this will help you a lot. Thanks.

 

 

 

 

If you like what you are reading, please think about buying us a coffee as a token of appreciation.

Buy Me A Coffee

We appreciate your support and are committed to providing you useful and informative content.

We are thankful for your ongoing support 

Tags

  • Share This: