Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • Home
  • About Me
  • Contact Me
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • AWS SNS: Sending a SMS Message using the AWS PHP SDK

AWS SNS: Sending a SMS Message using the AWS PHP SDK

8

Amazon makes it really easy to send SMS messages worldwide using their SNS or Simple Notification Service. This tutorial shows you how you can send a SMS in less than 10 lines of code. This particular example uses composer to install the AWS SDK.

Installation

Start by installing the AWS PHP SDK using composer:

composer require aws/aws-sdk-php

AWS Access Key and Secret

If you haven’t setup a Key and Secret to access the AWS APIs, you can do so using the IAM Management Console. Here are some basic instructions you can follow:

  1. User Details
    1. Create a new “User name”, e.g. “SMS”
    2. Set the “Access type” to “Programmatic access”
  2. Permissions
    1. Select “Attach existing policies directly”
    2. Search for “SNS”
    3. Check the box for “AmazonSNSFullAccess” to allow SNS access
  3. Complete
    1. Make a note of the “Access key ID” and “Secret access key”. You won’t be able to access the “Secret access” key again.

The Code

The below code can be used to send a SMS. Extra line breaks have been added to make the code readable – otherwise, the code can be condensed to as little as 5 lines.

On line 7, you’ll need to set the “key” (which is your “Access key ID”) and the “secret” (which is your “Secret access key”). If you wish to use a different AWS region, you can update line 5 to your region ID.

Lines 10-17 can be updated to specify your SMS message (using “Message”) and the phone number (using “PhoneNumber”, remember to include the international country code) to send the message to.

If you haven’t set a Sender ID from the SNS Console, you can specify one in the code (update “WebNiraj” in the code to an alphanumeric string (no spaces or symbols allowed).

The Result

Calling the API successfully will return a message ID in the form 4f218c80-3c95-5b41-b4c7-de59b89b01c3. As for the actual delivery of the message, this can be tracked from the SNS Console.

Amazon Web Services, PHP, SMS, Tutorial

8 comments on “AWS SNS: Sending a SMS Message using the AWS PHP SDK”

  1. Manan Shah says:
    May 5, 2018 at 8:16 AM

    What will be pricing for SMS ?

    Reply
    • Niraj Shah says:
      May 10, 2018 at 3:55 PM

      The pricing depends on which country and network the SMS is going to, and what type of message it is. For example, SMS to USA (any network) will cost ~$0.0064 (i.e. 0.6 cents).

      In comparison, messages to the UK to a O2 phone will cost between ~$0.03 and ~$0.07 (i.e. between 3 and 7 cents per message).

      See the pricing page:
      https://aws.amazon.com/sns/sms-pricing/

      Reply
  2. Rain says:
    August 15, 2019 at 4:14 PM

    Will this work on local machine?

    Reply
    • Niraj Shah says:
      August 15, 2019 at 4:48 PM

      Yes, since you can connect to AWS via a local machine.

      Reply
  3. akshay says:
    January 20, 2020 at 4:30 AM

    Command executed successfully, I also got msgID but i didn’t received the message

    Reply
    • Niraj Shah says:
      January 21, 2020 at 9:08 AM

      Check that the PhoneNumber is correct, including your full country code (e.g. +44 for UK) and area code.

      Reply
  4. Ochen Ao says:
    July 4, 2020 at 4:42 PM

    composer require aws/aws-sdk-php

    This should be executed on the php folder, so from the command-line cd to the php directory. Else you gonna have to start editing all the autoloader.php files and while importing the aws sdk in yyour php code….

    Reply
  5. Balakrishna says:
    November 20, 2020 at 11:15 AM

    sms not received

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Tags

Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache API Application Backup Bug Command Line Cordova Example Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Geolocation Google Nexus 5 Google Nexus One Hacking HTML5 Image Manipulation iOS iPhone JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2021 Niraj Shah
  • Privacy Policy
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkPrivacy Policy