Sapnesh Naik
Software Developer, Technical Writer
sapnesh@kerneldev.com
Blog Post

Send SMS Messages in Laravel using AWS SNS

January 3, 2018

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.

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://kerneldev.test/sendSMS/+91123456789

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