Scroll to top

SENDING SMS USING TWILIO

Ever wondered how to send SMS message through Laravel App, here I am for your rescue. Today we will use Twilio a Cloud-Based Communication System some of the features include call routing, SMS, MMS, SIP, call recordings. Everything in Twilio can be done using its nice and powerful API. We will use Twilio to create our SMS sending the app.

SETTING UP TWILIO

Go to Twilio and click on Sign Up for Free. Fill out your details and click Get Started. Now Twilio will ask you mobile no which will be used to verify your account if SMS verification does not work then click on the call to verify your account. Once verified you will be redirected to Twilio tour after that go to dashboard you will see two fields ACCOUNT SID and AUTH TOKEN this are used to validate your app with Twilio Server. To reveal the AUTH TOKEN click on Lock Icon.  Click on Configure your Trial number on the dashboard to see your configured trial number. If you are using the trial account you cannot send SMS message to unverified numbers. To see all verified numbers click here, If there are no numbers in the list then add a new number. We will use our Twilio trial number to send SMS message to one of our verified numbers.  We will use official Twilio package for Laravel

Open composer.json and add below lines to require array

"twilio/sdk": "dev-master"

Now run

composer update

VIEWS

We will use bootstrap to style our message field and Send button . We will have only one view which will have a text field to enter message and a button to send the message .

views/index.blade.php

<html>
    <head>
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
        <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
  
        <!-- Optional theme -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
  
        <!-- Latest compiled and minified JavaScript -->
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
  
    </head>
    <body>
  
        <div class="container" style="margin-top:100px;">
            <div class="row">
                {{Form::open(array("url"=>"/send"))}}
                <div class="col-xs-6">
                    <input type="text" class="form-control" name="message"/>
                </div>
                <div class="col-xs-6">
                    <button class="btn btn-success" type="submit">Send</button>
                </div>
                {{Form::close()}}
            </div>
        </div>
  
    </body>
</html>

ROUTES

As you can see in index.blade.php we are submitting the form to / send. So we need two routes, first to display index page and second to send a message.

Route::get('/', 'SMSController@index');
Route::post('/send', 'SMSController@send');

CONTROLLERS

Lets now create our SMSController. Our SMSController code is below

<?php
class SMSController extends BaseController {
  
    public function __construct() {
  
    }
  
    public function index() {
        return View::make('index');
    }
  
    public function send() {
        if (Input::has("message")) {
            $sid = "YOUR ACCOUNT SID";
            // Your Account SID from www.twilio.com/user/account
            $token = "YOUR ACCOUNT AUTH TOKEN";
            // Your Auth Token from www.twilio.com/user/account
  
            $client = new Services_Twilio($sid, $token);
            $message = $client -> account -> messages -> sendMessage('YOUR TRIAL/PAID TWILIO NUMBER', // From a valid Twilio number
            'YOUR VERIFIED NUMBER', // Text this number
            Input::get("message"));
  
            echo "Message Send Successfully Message SID :: " . $message -> sid;
        } else {
            echo "Error : No Message Entered <a href='/'>click here</a> to go back";
        }
    }
  
}

If everything is correct then you will see below message, for you, SID will be different.

To check logs click on Logs in the menu and click on any submenu like messages etc . Since we have sent a message output is shown below.

Feel free to comment if you have any doubt.

Thanks

KodeInfo

 

Author avatar

admin

http://www.kodeinfo.com
Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Post a Comment

Your email address will not be published. Required fields are marked *