Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

In this example we are going to deal with Amazon Simple Notification Service (SNS) to send SMS text messages to mobile phones.


Installation


Run composer require aws/aws-sdk-php to install AWS SDK for PHP library.


AWS SNS configuration


Go to "Identity and Access Management" dashboard and add a new user by assigning "AmazonSNSFullAccess" policy to it. The secret can be seen only once at the time of creating a new user so make sure you note it somewhere safe. Also make sure that the user cannot login to AWS like normal person would do so the "Group" should be "None" instead of something like "AdministratorAccess" so on.


Files


parameters.yml


parameters:
aws_sdk.config.default:
version: 'latest'
region: 'eu-west-1'

aws_sdk.credentials.default:
credentials:
key: 'AWS_KEY'
secret: 'AWS_SECRET'

services.yml


services:
Aws\Sdk: ~

App\Util\AwsSnsUtil:
arguments:
$config: '%aws_sdk.config.default%'
$credentials: '%aws_sdk.credentials.default%'

AwsSnsUtilInterface


declare(strict_types=1);

namespace App\Util;

interface AwsSnsUtilInterface
{
public function sendSms(string $phoneNumber): bool;
}

AwsSnsUtil


Configuration details can be found here.


declare(strict_types=1);

namespace App\Util;

use Aws\Sdk;
use Aws\Sns\Exception\SnsException;

class AwsSnsUtil implements AwsSnsUtilInterface
{
private $client;

public function __construct(Sdk $sdk, iterable $config, iterable $credentials)
{
$this->client = $sdk->createSns($config+$credentials);
}

public function sendSms(string $phoneNumber): bool
{
try {
$this->client->publish([
'PhoneNumber' => $phoneNumber,
'Message' => 'Feeds have been processed!',
'MessageAttributes' => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'INANZZZ'
],
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Promotional'
],
],
]);

$result = true;
} catch (SnsException $e) {
$result = false;
}

return $result;
}
}

Test


$this->awsSnsUtil->sendSms('0044_rest_of_the_number');

Result