Google trends Public Holidays Coupon Code Code Compiler

How To Send A SMS Using Amazon SNS service

Feb 6, 2024

How To Send A SMS Using Amazon SNS service

Amazon SNS: Scalable messaging service for sending notifications across multiple channels like SMS, email, and mobile push.


In today's digital era, communication is key, and SMS remains a powerful tool for reaching your audience effectively. Amazon Simple Notification Service (SNS) provides a robust platform for sending SMS messages seamlessly. In this guide, we'll walk you through the step-by-step process of setting up Amazon SNS for SMS messaging and provide you with the necessary code examples to get you started.

Amazon Simple Notification Service (Amazon SNS) is a fully managed messaging service provided by Amazon Web Services (AWS). It enables you to build distributed systems, microservices, and serverless applications by facilitating the communication between different components or services. SNS allows you to send messages or notifications to a large number of subscribers or endpoints, including mobile devices, email, SMS, HTTP endpoints, and AWS Lambda functions.

Key features of Amazon SNS include:

  1. Pub/Sub Messaging Model: SNS follows a publish/subscribe messaging paradigm, where publishers (producers) send messages to topics, and subscribers (consumers) receive messages from these topics. This decouples message senders from receivers, allowing for scalable and flexible communication patterns.

  2. Multiple Protocols and Endpoints: SNS supports various protocols for message delivery, including HTTP/S, email, SMS, and mobile push notifications (e.g., Apple Push Notification Service, Google Cloud Messaging). This flexibility enables you to reach your audience through their preferred channels.

  3. Message Filtering: SNS provides message filtering capabilities, allowing subscribers to receive only the messages that match specific criteria or attributes. This feature enhances efficiency and reduces unnecessary message processing.

  4. Message Attributes: You can attach custom attributes to messages, providing additional context or metadata that can be used for message filtering or processing logic.

  5. Delivery Retry and Dead-Letter Queue: SNS automatically retries message delivery in case of transient failures, ensuring high message delivery reliability. It also supports the configuration of dead-letter queues to capture undeliverable messages for further analysis or processing.

  6. Security and Access Control: SNS integrates with AWS Identity and Access Management (IAM) for fine-grained access control, allowing you to manage permissions for topics, subscriptions, and message publishing.

  7. Monitoring and Logging: AWS CloudWatch integration enables you to monitor SNS metrics, such as message deliveries, message size, and message throughput. You can set up alarms and dashboards to gain insights into your messaging system's performance.

Overall, Amazon SNS simplifies the implementation of event-driven architectures and real-time communication in distributed applications. It provides a reliable, scalable, and cost-effective solution for sending notifications, alerts, and updates to your users or application components across various channels.

Setting Up Amazon SNS

Step 1: Sign in to the AWS Management Console: Log in to your AWS account to access the AWS Management Console.

If you don’t have an Amazon AWS account, go to this link and create your account, you will get 12 month free trial all most of the amazon AWS services. Visit for more info.

Step 2: Navigate to the Amazon SNS Dashboard: Once logged in, search for "Simple Notification Service" in the AWS services search bar and select it.

Step 3: Create IAM User: Navigate to the IAM (Identity and Access Management) dashboard and create a new user with programmatic access.

Step 4: Assign Permissions: Attach the necessary permissions to the IAM user. At a minimum, the user should have permissions to publish messages to the SNS topic created earlier.  Select Attach existing policies directly” and in the search box write sns,  allow  “AmazonSNSFullAccess” 

Step 5:  Obtain Access Key and Secret Key: Once the user is created, note down the Access Key ID and Secret Access Key. These credentials will be used to authenticate requests to the AWS API.

Step 6: Integrating SNS with Your Application using PHP

require './aws/vendor/autoload.php';
$params = array(
    'credentials' => array(
    'region' => 'us-east-1', // < your aws from SNS Topic region
    'version' => 'latest'
$sns = new \Aws\Sns\SnsClient($params);

$phone_validator = '/^\(?(\d{3})\)?[-\. ]?(\d{3})[-\. ]?(\d{4})$/';

    if(!isset($_POST['message']) && $_POST['message'] == ''){
        $_SESSION['flash_message'] = 'Please  enter message.';
    $numbersArray = array();
    if(isset($_POST['numbers']) && $_POST['numbers'] != ''){
        $numbers = explode(',', $_POST['numbers']);     
        // print_r($numbers);   
        foreach($numbers as $number_item){
            if($number_item != ""){
                if(!preg_match($phone_validator, $number_item)){
                    $_SESSION['flash_message'] = $number_item.' Number is not valid, Please enter valid number.';
                    array_push($numbersArray, $number_item);
        $_SESSION['flash_message'] = 'Please enter mobile numbers.';
    // echo json_encode($numbersArray); exit;
    foreach($numbersArray as $nmbr){
        $args = array(
            "MessageAttributes" => [
                        'AWS.SNS.SMS.SMSType' => [
                            'DataType' => 'String',
                            'StringValue' => 'Transactional'
            "Message" => $_POST['message'],
            "PhoneNumber" => '+1'.$nmbr   // Provide phone number with country code
        $result = $sns->publish($args);
    $_SESSION['flash_success_message'] = 'Message sent successfully!';



Sending SMS messages using Amazon SNS is a straightforward process that can be easily integrated into your applications. By following the steps outlined in this guide and utilizing the provided code examples, you'll be able to leverage the power of Amazon SNS to communicate with your audience effectively. Whether you're sending alerts, notifications, or promotional messages, Amazon SNS provides a reliable and scalable solution for all your SMS messaging needs.

Copyright 2024. All rights are reserved