Palzin Track
Get 15% off with code PTRACKSIGNUP15 

Laravel Diary Logo

How to send SMS Messages in Laravel using AWS SNS

aws
Table of Contents

Simple Notification Service (SNS) is a full-featured notification service provided by Amazon Web Services (AWS). SNS provides a way to quickly and reliably send messages to a large number of subscribers. With SNS you can send Mobile Push Notifications, Emails, and Worldwide SMS messages!. Let me show you how to send SMS in Laravel using AWS SNS.

Prerequisites:

  • AWS access key ID and**secret access key**: To learn how to obtain your’s see this helpful article by AWS.
  • AWS_REGION (default = us-east-1): Region property is important because not all countries are supported by AWS for SMS messaging. See the list of supported countries to know whether your region is supported or not.

Installation and Configuration:

We will be using a composer package called [aws/aws-sdk-php-laravel](https://github.com/aws/aws-sdk-php-laravel) which provides a Laravel (5 and 4) service provider for the AWS SDK for PHP .

  • Require the aws-aws-sdk-php-laravel package in your Laravel project.

    composer require aws/aws-sdk-php-laravel
    
  • To register the AWS service provider. Add the line below to the providers key in your config/app.php.

    'providers' => array(
        // ...
        Aws\Laravel\AwsServiceProvider::class,
    )
    
  • You should also add the AWS facade alias to the aliases key in your config/app.php.

    'aliases' => array(
         // ...
        'AWS' => Aws\Laravel\AwsFacade::class,
    )
    
  • Publish the package’s configuration file using Artisan. This will generate a configuration file in the path config/aws.php.

    php artisan vendor:publish  --provider="Aws\Laravel\AwsServiceProvider"
    
  • Update your credentials and your preferred zone in the generated config/aws.php.

      <pre class="lang:php decode:true">return [
          'credentials' => [
              'key'    => 'your_key',
              'secret' => 'your_secret',
          ],
          'region' => 'us-east-1',
          'version' => 'latest',
      ];
    

Send SMS in Laravel Using AWS SNS:

  • We will quickly create a controller which will be responsible for sending SMS messages.
        php artisan make:controller SMSController
  • We will also define a route in routes/web.php which will call a function in SMSController to send a message. Note that here I am passing the phone number with the route itself for demo purposes. In a practical scenario, you may want to get your phone numbers from a model or aform request.
        Route::get('/sendSMS/{phone_number}', 'SMSController@sendSMS');
  • Import AWS facade and add sendSMS() function to SMSController.
        <?php
        
        namespace App\Http\Controllers;
        
        use Illuminate\Http\Request;
        use AWS;
        
        class SMSController extends Controller
        {
            protected function sendSMS($phone_number){
                $sms = AWS::createClient('sns');
        
                $sms->publish([
                    'Message' => 'Hello, This is just a test Message',
                    'PhoneNumber' => $phone_number,
                    'MessageAttributes' => [
                        'AWS.SNS.SMS.SMSType'  => [
                            'DataType'    => 'String',
                            'StringValue' => 'Transactional',
                         ]
                   ],
                ]);
            }
        }
Here we create an SNS client (`$sms`) using `AWS::createClient()` method and then we can send a message by calling the `publish()` method on the client object (`$sms`). We must pass some necessary options to `publish()` such as a message, phone\_number, and SMSType (`Transactional` or `Promotional`). A full list of available options can be found [here](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#publish).
Note:
  • The phone number must be a valid one and should also include the country code (+91 for India).
  • The Promotional type messages will only be delivered from 9 AM to 9 PM in India. (This is due to govt. regulations)

That is it!. Test if everything is working fine by sending a message using the route we defined earlier in this tutorial. In your browser, you should type something like:

http://bhavikfuletra.test/sendSMS/+919980000000

Did you find this tutorial useful? If yes, don’t forget to share your thoughts in the comment section below.

::Share it on::

Comments (0)

What are your thoughts on "How to send SMS Messages in Laravel using AWS SNS"?

You need to create an account to comment on this post.

Related articles