
Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
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
Basic knowledge of PHP. If you need hands-on experience then better to set up your AWS account.
First log in to your AWS account and in the top navigation under services
feature, search for IAM
as shown below
Once you are in the IAM
page. Navigate to User section on the left sidebar and click on New User as shown below
Now in Add User page, we need to add user detail and set up the as following
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)
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
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.
Now you have successfully created new user and have attached proper policies to the user.
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.
We will use the composer to download the AWS PHP SDK.
index.php
- In this file, I will implement the SMS sending.
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?
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.
<?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.
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
The AWS Support team provides an initial response to your request within 24 hours.
Hope this was helpful. Kindly share with your friends :)
Code available in GitHub @ Send SMS With Amazon SNS
Securely Connect Server MYSQL DB From Sequel Pro / MYSQL Workbench
Send Email In PHP With PHPMailer
Comment And Like System Using Disqus
Google reCAPTCHA Integration In PHP Laravel Forms
Sass or SCSS @mixin vs @extends vs Placeholder (%)
Debugging Laravel Queue Email Issues
Factory States For Clean And Fluent Laravel Testing
Free SSL Certificate For CPanel
Simple Way To Create Resourceful API Controller In Laravel
Supervisor For Laravel Queue Scheduling
Install Linux, Apache, MYSQL, PHP (LAMP Stack) on Ubuntu
Accessors And Mutators In PHP Laravel