PHP code example of sguy / easysms

1. Go to this page and download the library: Download sguy/easysms library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sguy / easysms example snippets




namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Sguy\EasySms\Sms\EasySms;

class SmsController extends Controller
{
    public function sendSms(Request $request, EasySms $sms)
    {
        //get information from request or any source...
        $mess = $request->message;
        $phone = $request->phone;

        //sender id is optional, value from easysms.php config file will be used if not set.
        $sms->setSenderId("Yara Ghana");

        //set message content
        $sms->setMessage($mess);
        
        //send message
        $status = $sms->sendMessageTo($phone);

        //status will contain either "success", "invalid credentials", "insufficient balance" or "failed",
        if($status==="success"){
            return redirect()->back()->with('success','Message has been sent successfully');
        }
        //you can do more checks and act accordingly
    }
}


  

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Sguy\EasySms\Sms\EasySms;

class SmsController extends Controller
{
    public function scheduleSms(Request $request, EasySms $sms)
    {
        //get information from request or any source...
        $mess = $request->message;
        $phone = $request->phone;

        //sender id is optional, value from easysms.php config file will be used if not set.
        $sms->setSenderId("Yara Ghana");

        //Date can be obtained from request or any datasource
        $date = now()->addMinutes(10);
        $sms->schedule($date);

        //set message content
        $sms->setMessage($mess);

        //send message
        //message will be sent in 10 mins time
        $status = $sms->sendMessageTo($phone);

        //status will contain either "success", "invalid credentials", "insufficient balance" or "failed",
        if($status==="success"){
            return redirect()->back()->with('success','Message has been sent successfully');
        }
        //you can do more checks and act accordingly
    }
}

  

  

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Sguy\EasySms\Sms\EasySms;

class SmsController extends Controller
{
    public function sendToMultiple(Request $request, EasySms $sms)
    {
        //get information from request or any source...
        $mess = $request->message;
        $phones = $request->phones;

        //sender id is optional, value from easysms.php config file will be used if not set.
        $sms->setSenderId("Yara Ghana");

        //set destinations/receipients which accepts an array of phone numbers to send message to
        $sms->setDestinations($phones);

        //you can schedule too
        // $date = now()->addMinutes(10);
        // $sms->schedule($date);

        //set message content
        $sms->setMessage($mess);

        //send message
        //Now use sendMessage instead of sendMessage
        $status = $sms->sendMessage();

        //status will contain either "success", "invalid credentials", "insufficient balance" or "failed",
        if($status==="success"){
            return redirect()->back()->with('success','Message has been sent successfully');
        }
        //you can do more checks and act accordingly
    }
}

  

  

namespace App\Http\Controllers;
use Sguy\EasySms\Sms\EasySms;

class SmsController extends Controller
{
   //You can just add a constructor and inject EasySms as opposed to injecting in each function

   public function getBalance(EasySms $sms)
   {
    $balance = $sms->getSmsAccountBalance();
    return $balance;
   }
   public function getChargePerSms(EasySms $sms)
   {
    //Message must be set before the charge can be determined
    $sms->setMessage("Bunch of Text Here");
    $charge = $sms->getChargePerSms();
    return $charge;
   }

}

  

  

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use \Sguy\EasySms\Channel\EasySms; //Import EasySms from Channel, not Sms

class TestNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [EasySms::class]; //Replace the default ['mail'] with [EasySms::class].
    }


    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
     
    //Replace toArray or toDatabase function with this
    public function toEasySms($notifiable)
    {
        return [

          'message'=>"Hello, Notification from EasySms",

          //specify a field on your notifiable entity where you save the phone, Easysms uses phone field if set to null
          'field'=>null,

          //whom you want to send the sms as, Easysms uses sender id in config if not set, maximum of 11 chars,it can also be
          //a phone number
          'sender_id'=>'Easy Sms',

          //'if set, sms will be sent on the specified date but not immediately'
          'datetime'=>null,
        ];
    }
}



namespace App;
use Illuminate\Notifications\Notifiable;

class YourModel
{
    use Notifiable;

    
}

 

  

namespace App\Http\Controllers;

use App\Notifications\TestNotification;

class SmsController extends Controller
{
    public function notify()
    {
    $user = auth()->user();
    //Note you can schedule notifications too, just accept a data, eg. date in your TestNotification Constructor and supply it here
    //$date = now()->addMinutes(20);
    //$user->notify(new TestNotification($date));
    
    //notify user
    $user->notify(new TestNotification);;
    return redirect()->back();
    }
}