Laravel sends SMS to mobile

Today, I will give you an example of “How to Send SMS in Laravel (Send SMS to Mobile)”, So you can easily apply it with your laravel 5, laravel 6, laravel 7, laravel 8, and Laravel 9 application.

First, what we’re doing here, This is the example :

send SMS to users in Laravel

fast2sms API in Larvel

send free sms to user mobile laravel

Need to Send SMS to Mobile in Laravel

We need to send SMS to mobile whenever users login to our Laravel application we verify their phone numbers and also we use the SMS service to send notifications to users, for example: In the e-commerce platform for order status, order, and payment confirmation we use the SMS API to send a notification to users.

Step 1. Insert Some Dummy Records on the user’s table using Seeder

We have a default table user in our database just add a new column phone, Now create a seeder file.

Related article: Add a new column in the table using migration in Laravel.

Create a database Seeder

php artisan make:seeder UserTableSeeder

database\seeders\TaskTableSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;

class UserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $users =[
            
            ['id'=>1,'name'=>'8bityard','email'=>'8bityard@gmail.com','phone'=>'8958056508','password'=>'$2y$10$R'],
            ['id'=>2,'name'=>'User 2','email'=>'newuser@gmail.com','phone'=>'1212121212','password'=>'$2y$10$R']
           
        ];
        User::insert($users);
    }
}

Register Seeder

We Register UserTableSeeder Class in DatabaeSeeder File

database\seeders\DatabaseSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // \App\Models\User::factory(10)->create();
         $this->call(UserTableSeeder::class);
    }
}

Hit the Seed Command

php artisan db:seed
user table seeder

Step 2. Create Controller and Model

php artisan make:controller SendSmsController
php artisan make:model Sms

Step 3. Create an account on the fast2sms website

fast2sms website url

We create an account on the fast2sms website, when you register on the fast2sms website you will get 50 rupee credits We test the message successfully.

Step 3. Send a Text Message Statically to test API

send text message using fast2ms api in Laravel

After successfully registration, just click on the Dev Api option to test a message service it’s all free just fill up all the details and click on Overall Url to send a message.

message send successfully

Step 4. Now we implement SMS API in Our Laravel application

setup environment in fast2sms API

Just fill-up the Dev API form and copy the authorization key and a sender ID. these are the credentials that we will use on our model to send SMS.

copy environment

Step 5. Define Web Route and Model

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\SendSmsController;

/*
|--------------------------------------------------------------------------
| 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!
|
*/

// Send Sms to users Example
Route::get('/user-list', [SendSmsController::class, 'userlist'])->name('user.list');
Route::any('/send-sms-user/{id}', [SendSmsController::class, 'sendsmsTouser'])->name('sms.send');

Step 6. Setup API credentials and create a function in Sms Model

Create a function in the Sms model and paste your Sms API credentials.

app/Models/Sms.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Sms extends Model
{
    use HasFactory;

    public static function sendSms($message,$mobile){
        
    $request ="";
    $param['authorization']="NzL1vQGdmxZbqDrUWP0ca8phTV7xxxxx;
    $param['sender_id']="Cghpet";
    $param['message']=$message;
    $param['numbers']=$mobile;
    $param['language']="english";
    $param['route']="v3";

    foreach($param as $key => $val){
        $request.=$key."=".urlencode($val);
        $request.="&";
    }
    $request =substr($request,0,strlen($request)-1);

    $url ="https://www.fast2sms.com/dev/bulkV2?".$request;
    $ch =curl_init($url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    $curl_scaped_page=curl_exec($ch);
    curl_close($ch);

    }
}

Step 7. Get all the records from the user table and send an SMS from the Controller

app/Http/SendSmsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Sms;

class SendSmsController extends Controller
{
    public function userlist(){
        $users = User::get();
        return view('sms.list',compact('users'));
    }

    public function sendsmsTouser(Request $request,$id)
    {
        $user = User::find($id);
        
        #send sms to user phone number
        $message = " ".$user->name.", Thank you for Registering in our Website.";
        $mobile = $user->phone;
        Sms::sendSms($message,$mobile);
        dd('success');
    }
}

resources/views/sms/list.blade.php

<div class="container">
         <h3><a>Send Free Sms to User in Laravel using Fast2sms API.</a></h3>
         <table class="table">
            <thead>
               <tr>
                  <th>S.no</th>
                  <th>Name</th>
                  <th>Email</th>
                  <th>phone</th>
                  <th>Action</th>
               </tr>
            </thead>
            <tbody>
               @foreach($users as $key => $user)
               <tr>
                  <th scope="row">{{ $key+1 }}</th>
                  <td>{{ $user->name }}</td>
                  <td>{{ $user->email }}</td>
                  <td>{{ $user->phone }}</td>
                  <td><a href="{{route('sms.send',$user->id)}}">Send Sms</a></td>
               </tr>
            </tbody>
            @endforeach
         </table>
      </div>

Step 8. Run your application and check the delivery report in fast2sms API and in your Mobile phone

Run Application :

127.0.0.1:8000/user-list
send bulk sms to all users mobile laravel


delivery report after send sms

sms successfully received to user mobile laravel

In this article, we successfully learned “How to Send SMS to Mobile in Laravel”, I hope this article will help you with your Laravel application Project.

Read also: How to make Delete API in Laravel.

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com

Scroll to Top