Send SMS Messages in Laravel using AWS SNS

Send SMS Messages in Laravel using AWS SNS

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 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.
  • To register the AWS service provider. Add the line below to the providers key in your config/app.php.
  • You should also add the AWS facade alias to the aliases key in your config/app.php.
  • Publish the package’s configuration file using Artisan. This will generate a configuration file in the path config/aws.php.
  • Update your credentials and your preferred zone in the generated config/aws.php.

     

Send SMS in Laravel using AWS SNS:

  • We will quickly create a controller which will be responsible for sending SMS messages.
  • 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.
  • Import AWS facade and add sendSMS() function to SMSController.

    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

 

Did you find this tutorial useful? If yes, don’t forget to share your thoughts in the comment section below and you may also like my other useful posts on Laravel.

Leave a Reply

4 Comments on "Send SMS Messages in Laravel using AWS SNS"

Notify of
avatar
Sort by:   newest | oldest | most voted
Filip
Guest

Works like a charm ! Thanks !

abu
Guest

Should I pay for per SMS I send and How Should I buy SMS bundle?