Sending Mobile Verification OTP SMS Using PHP — Lelocode

24 April, 2021 2 minute read

Sending mobile verification OTP SMS using PHP — Lelocode

Getting Started

Summary: An increasing number of web applications, android applications, and ios application have integrated SMS to notify users for Welcome message, OTP verification, Events, Sales or Coupons, directly through their mobile devices.

Let’s Begin

Before start, we need a third party that provides SMS service. So for this tutorial, I am using the MSG91 service to send SMS.

MSG91 account

Go to ,

1.Create a new account or just click social login.

msg91 login page


2. Go to Dashboard and create your Sender ID like my sender id after creating Conyou.


sender id creation

3. Go to Side menu and click on API Section and copy Authkey.



Note: MSG91 provides 250 Free Transactional SMS After that you need to purchase SMS.

I assume that you already have SMS service from msg91 using the above setup or you can use any SMS service providers like Twilio, Nexmo, etc. Now I am creating a common function which you can call anywhere in your PHP code to send any type of text SMS.

For Best Practices, you can create a constant file and put all your configuration keys in that file. For this I create a .env file where I put all my configurations .env this also used by some Popular PHP Framework Laravel and Symfony, So here my keys:

# MSG91 AUTH KEY and configuration parameters
# MSG91 ROUTE you can use this in your project without changing value

Just copy this function and paste it into your helper class and call anywhere you want.

//send otp message
public static function sendMessage($mobile, $message)
$message = urlencode($message);
$url = sprintf("",
env('MSG91_AUTH_KEY'), $mobile, $message, env('MSG91_SENDER_ID'), env('MSG91_ROUTE'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$output = curl_exec($ch);
return $output;

How it's work here I am creating a message creation function and finally call these two function to send OTP.

//otp sms message helper
public static function sendOtpSms($name, $otp)
    $message = "Hi " . $name . "%0aThank you for showing interest in Lelocode. Your OTP is%0a" . $otp;
    return $message;
}// Generate random four digit number
$four_digit_random_number = mt_rand(1000, 9999);
// call message helper function
$message = Helpers::sendOtpSms($full_name = 'Guest', $four_digit_random_number);// get mobile number and add country code with mobile
$phone = '91' . $request->get('mobile');
// send message to mobile number
Helpers::sendMessage($phone, $message);

Congratulations, you have just integrated SMS API to your PHP application. Now it's your time to do some cool stuff using this code. Feel free to comment and ask any query if you stuck in your code or general questions.

Shobhit Singh

Shobhit Singh

Lead Developer at Signifier Technology Pvt Ltd

Leave a Reply

More from Rannkly

Our best blog on PHP


Integrate CCavenue Payment Gateway In PH...

Shobhit Singh

Shobhit Singh

10 minute read

Integrating CCAvenue with php web application

Read more

PHP Latitude Longitude To Address

Vishnu Sharma

Vishnu Sharma

1 minute read

Read more

Laravel Latest Version 5.7 With Material...

Stuti varshney

Stuti varshney

1 minute read

Read more