Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • 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

11

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

11 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
    • Niraj Shah says:
      August 31, 2021 at 5:09 PM

      Some message may be blocked by Amazon if they are deemed to be spam or malicious. If your message has been blocked and not any of the above, you should check with support on what happened.

      Reply
  6. cary42 says:
    August 31, 2021 at 4:49 PM

    How do you send a message that’s more than 160 characters? This seems to cut off the message if it’s more than that.

    Reply
    • Niraj Shah says:
      August 31, 2021 at 5:08 PM

      I’ve tested this in the past. If the message exceeds the 160 character limit, the message is split automatically and sent as multiple messages. On the receiving end, this still looks like one large message. But this does also mean that you will be charged for each message part.

      The documentation says:

      If you publish a message that exceeds the size limit, Amazon SNS sends it as multiple messages, each fitting within the size limit. Messages are not cut off in the middle of a word but on whole-word boundaries. The total size limit for a single SMS publish action is 1600 bytes.

      Reply

Leave a ReplyCancel 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

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy