SMS.to Blog

How to Send SMS with SMS.to and Laravel/Lumen PHP

Posted on

Companies need to send SMS for all kinds of reasons: Marketing campaigns, OTP2FA, informing their customers about new products or special offers or just releasing a time critical announcement to a large group of recipients.

Whatever the use case, with SMS achieving open rates ofover 98% within the first few minutes, more and more businesses rush to intergrate SMS messaging in their workflows.

This step by step guide will show you how to easily send bulk SMS from your website using Laravel / Lumen and SMS.to.

What we need

We assume that you already have the following

  • Terminal (Shell Command Line)
  • Composer installed
  • Familiarity with PHP
  • Familiarity with Laravel (5+)
  • Existing Laravel (5+) project
  • An SMS.to account and your credentials ready:
    • Create an account in SMS.to if you do not have one
    • Request for API credentials after you login. To request an API client, please click on API Clients in the navigation bar and then “Create” if you haven’t already created one before.

api_client

If you haven’t set up Laravel yet, go to https://laravel.com/docs/5.8#installing-laravel

Installation

You need to have a working account on SMS.to with sufficient balance loaded to be able to use this package. If you do not have one, please get one here.

Require package with composer.

$ composer require intergo/sms.to-laravel-lumen

You might need to add Intergo\SmsTo\ServiceProvider::class, to the providers array config/app.php if your Laravel version is less than 5.5.

Configuration

Publish the configuration file

$ php artisan vendor:publish --provider="Intergo\SmsTo\ServiceProvider"

You will get a config file named smsto.php in your config directory. Customise the defaults to your personal SMS.to configurations.

<?php

  return [
      'client_id' => env('SMSTO_CLIENT_ID'),
      'client_secret' => env('SMSTO_CLIENT_SECRET'),
      'username'=> env('SMSTO_EMAIL'),
      'password' => env('SMSTO_PASSWORD'),
      'scope' => '*',
      'sender_id' => env('SMSTO_SENDER_ID'),
      'callback_url' => env('SMSTO_CALLBACK_URL'),
  ];

 

Change or add configuration in you .env file

SMSTO_CLIENT_ID=xyxy1234
SMSTO_CLIENT_SECRET=xyz1234567
SMSTO_EMAIL=email@sms.to
SMSTO_PASSWORD=y2kP@szword
SMSTO_SENDER_ID=SMSTO
SMSTO_CALLBACK_URL=https://mysite.org/smscallback

Create Controller and View

Run the following command to generate Laravel Controller

$ php artisan make:controller SmsController

It will generate the file app\Http\Controllers\SmsController.php

Open SmsController.php in your text editor and add SmsTo class

<?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use SmsTo;

The next step is to create the view file. This file will contain the HTML fields that we need to send the bulk SMS.

In your terminal run the command:

$ touch resources/views/smsto/sendsms.blade.php

Open the file in resources/views/smsto/sendsms.blade.php then copy and paste the following code:

<html>
      <body>
         <form method='POST'>
            @if($errors->any())
            <ul>
            @foreach($errors->all() as $error)
            <li><strong>{{ $error }}</strong></li>
            @endforeach
            <ul>
            @endif
        @if(session('success'))
            <strong>{{ session('success') }}</strong>
        @endif
            <label for="to">Comma Separated Numbers</label>
            <input type='text' name='to' />
            <br>
            <label>Message/Body</label>
            <textarea name='messages'></textarea>
            <br><br>
            <button type='submit'>Send</button>
            @csrf
      </form>
    </body>
</html>

Open your web.php file located on routes folder and paste the following code:

Route::get('/sms/send', 'SmsController@sendSms');
Route::post('/sms/send', 'SmsController@postSendSms');

Open up your SmsController.php and the code should look like below:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SmsTo;
class SmsController extends Controller
{
    public function sendsms()
    {
        return view('smsto.sendsms');
    }
    public function postSendSms(Request $request)
    {
        $request->validate([
            'to' => 'required',
            'messages' => 'required',
        ]);
        $to = explode(',', request('to'));
        $response = SmsTo::setMessage(request('messages'))
                        ->setRecipients($to)
                        ->setSenderId('SMSTO')
                        ->setCallbackUrl('https://mysite.org/smscallback')
                        ->sendMultiple();
        if ($response['success'] == true) {
            return back()->with('success', 'Messages has been queued and ready to be sent.');
        } else {
            return back()->withErrors($response['errors']);
        }
    }
}

Test and Send SMS!

Open your browser and go to /sms/send or anywhere you set up your Laravel routes. Fill up numbers and messages then click the submit button.

Wrapping Up

You experienced how easy and efficient it is to set up and send bulk SMS or notification SMS using SMS.to. Sign up now and we can give you free credits for you to test the power of SMS.to.

For a more detailed documentation on the topic please refer to our page on GitHub.