Create a tool for sending bulk SMS using PHP

Create a customizable tool using PHP to send massive SMS messages for marketing or notification messages

A bulk SMS tool can be useful for marketing and notification purposes. You can send massive SMS messages from Google Sheets to a list of mobile phone numbers using our Google Sheets add-on . You can also use this PHP code to create your own bulk SMS application to send customizable messages to multiple customers in a short time.

What you can get

This is what you can get with this sample:

  1. You can enter a list of mobile phone numbers in international format.
  2. Enter the message you want to send to all the phone numbers.
  3. Click on the Submit button for sending the desired message to all the phone numbers.
  4. After processing the bulk SMS sending, the page will render a list displaying if the messages were successfully sent or if they failed.

Simple bulk sms tool

Server code in PHP


API Endpoint used


Parameters you need to replace in the sendSMS function:

  • [[TOKY_API_KEY]]: You can get the Toky API on this link:
  • [[TOKY_SMS_PHONE_NUMBER]]: This is the phone number in Toky, enabled to send SMS text messages. It should be entered in international format, for example: + 18443326433
  • [[AGENT_EMAIL]]: It is the email of the agent to whom you want to be assigned SMS sent in Toky.
 //start the session variable

 //Function that evaluates form submit
 function getPhoneList()
     //Check if there is a generated verification code and a mobile phone    
    if  ($_POST["txtPhoneNumbers"] !="" &&  $_POST["txtMessage"] !="")
      $phoneList= explode(PHP_EOL, $_POST["txtPhoneNumbers"]);
          displayToastMessage('Missing phone list or text message','error');

      return $phoneList;


 function displayToastMessage($message,$type){
    echo "<script>M.toast({html: '".$message."',classes:'red darken-4'});</script>";
    echo "<script>M.toast({html: '".$message."',classes:'green darken-3'});</script>";

//function to send SMS
function sendSMS($phoneNumber,$textMessage)
  // create a new cURL resource
  $ch = curl_init();
  $smsPhoneNumber = "[[TOKY_SMS_PHONE_NUMBER]]";
  $api_key = '[[TOKY_API_KEY]]';//'[[TOKY_API_KEY]]';
  $headers = array();
  $headers[] = "X-Toky-Key: {$api_key}";
   $data = array("from" => $smsPhoneNumber, "email" => "[[AGENT_EMAIL]]",
                   "to" => $phoneNumber,
                   "text" => $textMessage);

   $json_data = json_encode($data);

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "");
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch,CURLOPT_POSTFIELDS, $json_data);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
    curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");

    $curl_response = curl_exec($ch); // Send request
   curl_close($ch); // close cURL resource

   $decoded = json_decode($curl_response,true);
   //Return error message if the message can't be sent
   if (!$decoded["success"]){
     $result= "Error sending SMS to $phoneNumber: ".$decoded["error_message"];
     $result='SMS sent successfully';
	return $result;

<!DOCTYPE html>
<html lang="en">

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="" rel="stylesheet">
    <!-- Compiled and minified CSS -->
    <link rel="stylesheet" href="">

    <!-- Compiled and minified JavaScript -->
    <script src=""></script>

    <div class="container">
            <div class="nav-wrapper">
                <a href="#" class="brand-logo">Toky's Simple Bulk SMS</a>
        <form action="index.php" method="POST">
            <div class="row">
                <div| class="col s4">
                    <div class="row">
                        <div class="input-field col s12">
                            <textarea id="txtPhoneNumbers" class="materialize-textarea"
                                name="txtPhoneNumbers"><? echo $_POST["txtPhoneNumbers"] ?></textarea>
                            <label for="txtPhoneNumbers">Phone Numbers</label>
                <div class="col s8">
                    <div class="row">
                        <div class="input-field col s12">
                            <textarea id="txtMessage" class="materialize-textarea"
                                name="txtMessage"><? echo $_POST["txtMessage"] ?></textarea>
                            <label for="txtMessage">Message</label>
            <div class="row">
                <button class="btn waves-effect waves-light btn-large center-align" type="submit" name="action">Submit
                    <i class="material-icons right">sms</i>

            $textMessage= $_POST["txtMessage"];
        <? if($phoneCount>0) { ?>
        <div class="row">
            <ul class="collection with-header">
                <li class="collection-header">
                <? foreach ($phoneNumberList as $phoneNumber){ 
                <li class="collection-item">
                    <? if ($result== "SMS sent successfully") { ?>
                        <?echo $phoneNumber?><span class="secondary-content light-green-text accent-4">
                            <? echo $result ?><i class="material-icons ">sms</i></span>
                        <?echo $phoneNumber?><span class="secondary-content red-text accent-4">
                            <? echo $result ?><i class="material-icons ">sms_failed</i></span>
                    <? } ?>
                <? } ?>
        <? } ?>