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:
- User Details
- Create a new “User name”, e.g. “SMS”
- Set the “Access type” to “Programmatic access”
- Permissions
- Select “Attach existing policies directly”
- Search for “SNS”
- Check the box for “AmazonSNSFullAccess” to allow SNS access
- Complete
- 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.
What will be pricing for SMS ?
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/
Will this work on local machine?
Yes, since you can connect to AWS via a local machine.
Command executed successfully, I also got msgID but i didn’t received the message
Check that the
PhoneNumber
is correct, including your full country code (e.g.+44
for UK) and area code.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….
sms not received
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.
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.
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: