Send SMS With Amazon SNS (Simple Notification Service) Using PHP


Share On        


In this article, I will show you how to send SMS to any country user using Amazon SNS (Simple Notifications Service) using PHP.


Code available in GitHub @ Send SMS With Amazon SNS


We will cover


  1. AWS Create New User & Give SNS Policy Access
  2. Folder Structure
  3. Downloading AWS PHP SDK
  4. Implementation
  5. Upgrading Daily Limit

Prerequisites


Basic knowledge of PHP. If you need hands-on experience then better to set up your AWS account.


Step 1 - AWS Create New User & Give SNS Policy Access


First log in to your AWS account and in the top navigation under services feature, search for IAM as shown below


AWS | Search IAM


Once you are in the IAM page. Navigate to User section on the left sidebar and click on New User as shown below


AWS IAM | Create New User


Now in Add User page, we need to add user detail and set up the as following


AWS New User Form Details


NOTE: Please check on Programmatic Access else it won't work when you start using in your code.

Once you click on Next button its time to setup Permissions as follows


Attach Existing Policies -> Search (SNS) -> Select (Amazon SNS Full Access)


Attach Permission | Amazon SNS Full Access


Next, you can add tags so that it will help you to remember for which purpose you had created this user, you can skip adding tags.


If you skip tags then it will take you to final step ie Review to check if everything is proper as follows


AWS New User Review Page


If everything is as per your expectations then click on Create User button you will be able to see the credentials and even option to download it. Make sure to save or download then credentials.


AWS New User Credentials


Now you have successfully created new user and have attached proper policies to the user.


Step 2 - Folder Structure Of Project


The following is our project folder structure. Anyways if your using the framework then just create a new controller and then add the code in the method.


Project Folder Structure


We will use the composer to download the AWS PHP SDK.


index.php - In this file, I will implement the SMS sending.


Step 3 - Downloading AWS PHP SDK


To work with Amazon SNS with PHP you need to download the AWS PHP SDK. I am using composer to download the package


composer require aws/aws-sdk-php


Simple right?


Step 4 - Implementation


Let's get our hands dirty by implementing. I am writing code inside the index.php file.


NOTE: Make sure to change the credentials in the following code

Explanation of the code I have added in the comments. Make sure to go through it.


index.php

<?php

/** Error Debugging */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL | E_STRICT);

/** Make sure to add autoload.php */
require './vendor/autoload.php';

/** Aliasing the classes */

use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;


/** AWS SNS Access Key ID */
$access_key_id    = 'XXX';

/** AWS SNS Secret Access Key */
$secret = 'XXX';

/** Create SNS Client By Passing Credentials */
$SnSclient = new SnsClient([
    'region' => 'ap-south-1',
    'version' => 'latest',
    'credentials' => [
        'key'    => $access_key_id,
        'secret' => $secret,
    ],
]);

/** Message data & Phone number that we want to send */
$message = 'Testing AWS SNS Messaging';

/** NOTE: Make sure to put the country code properly else SMS wont get delivered */
$phone = '+917019102XXX';

try {
    /** Few setting that you should not forget */
    $result = $SnSclient->publish([
        'MessageAttributes' => array(
            /** Pass the SENDERID here */
            'AWS.SNS.SMS.SenderID' => array(
                'DataType' => 'String',
                'StringValue' => 'StackCoder'
            ),
            /** What kind of SMS you would like to deliver */
            'AWS.SNS.SMS.SMSType' => array(
                'DataType' => 'String',
                'StringValue' => 'Transactional'
            )
        ),
        /** Message and phone number you would like to deliver */
        'Message' => $message,
        'PhoneNumber' => $phone,
    ]);
    /** Dump the output for debugging */
    echo '<pre>';
    print_r($result);
    echo '<br>--------------------------------<br>';
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage() . "<br>";
}


The above code is the full implementation. Feel free to change as per your requirements.


5) Upgrading Daily Limit


NOTE: If you fail to do the following process then for 1 or 2 days your SMS might work and then it will start failing. So don't get panic.


By default, you will be issued only testing SMS from Amazon. To increase the quota limit of the SMS make sure to follow the following steps.


To request a spending quota increase

  1. Go to the AWS Support Center.
  2. Choose To Create case.
  3. Choose Service limit increase, and then under Case classification, perform the following steps:
  4. For Limit type, choose SNS Text Messaging.
  5. For Link to site or app which will be sending SMS - optional, enter the URL of your website or application.
  6. For Type of messages - optional, choose One Time PasswordPromotional, or Transactional, depending on what you plan to send.
  7. For Targeted Countries - optional, choose General Limits.
  8. Under Requests, for Request 1, do the following:
  9. For Resource Type, choose General Limits.
  10. For New limit, enter the needed spend limit that you calculated earlier.
  11. Under Case description, for Use case description, enter the description that you wrote earlier.
  12. Expand Contact options, and then choose your preferred contact language.
  13. Choose Submit.
  14. When you finish, choose Submit.

The AWS Support team provides an initial response to your request within 24 hours.


Conclusion


Hope this was helpful. Kindly share with your friends :)


Code available in GitHub @ Send SMS With Amazon SNS




AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing