Sending SMS messages in Amazon SNS with the Amazon SDK for PHP Version 3 - Amazon SDK for PHP
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Sending SMS messages in Amazon SNS with the Amazon SDK for PHP Version 3

You can use Amazon Simple Notification Service (Amazon SNS) to send text messages, or SMS messages, to SMS-enabled devices. You can send a message directly to a phone number, or you can send a message to multiple phone numbers at once by subscribing those phone numbers to a topic and sending your message to the topic.

Use Amazon SNS to specify preferences for SMS messaging, such as how your deliveries are optimized (for cost or for reliable delivery), your monthly spending limit, how message deliveries are logged, and whether to subscribe to daily SMS usage reports. These preferences are retrieved and set as SMS attributes for Amazon SNS.

When you send an SMS message, specify the phone number using the E.164 format. E.164 is a standard for the phone number structure used for international telecommunications. Phone numbers that follow this format can have a maximum of 15 digits, and are prefixed with the plus character (+) and the country code. For example, a US phone number in E.164 format would appear as +1001XXX5550100.

The following examples show how to:

  • Retrieve the default settings for sending SMS messages from your account using GetSMSAttributes.

  • Update the default settings for sending SMS messages from your account using SetSMSAttributes.

  • Discover if a given phone number owner has opted out of receiving SMS messages from your account using CheckIfPhoneNumberISOptedOut.

  • List phone numbers where the owner has opted out of receiving SMS messages from your account using ListPhoneNumberOptedOut.

  • Send a text message (SMS message) directly to a phone number using Publish.

For more information about using Amazon SNS, see Using Amazon SNS for User Notifications with a Mobile Phone Number as a Subscriber (Send SMS).

All the example code for the Amazon SDK for PHP is available here on GitHub.

Credentials

Before running the example code, configure your Amazon credentials, as described in Credentials. Then import the Amazon SDK for PHP, as described in Basic usage.

Get SMS attributes

To retrieve the default settings for SMS messages, use the GetSMSAttributes operation.

This example gets the DefaultSMSType attribute. This attribute controls whether SMS messages are sent as Promotional, which optimizes message delivery to incur the lowest cost, or as Transactional, which optimizes message delivery to achieve the highest reliability.

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); try { $result = $SnSclient->getSMSAttributes([ 'attributes' => ['DefaultSMSType'], ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Set SMS attributes

To update the default settings for SMS messages, use the SetSMSAttributes operation.

This example sets the DefaultSMSType attribute to Transactional, which optimizes message delivery to achieve the highest reliability.

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); try { $result = $SnSclient->SetSMSAttributes([ 'attributes' => [ 'DefaultSMSType' => 'Transactional', ], ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Check if a phone number has opted out

To determine if a given phone number owner has opted out of receiving SMS messages from your account, use the CheckIfPhoneNumberIsOptedOut operation.

In this example, the phone number is in E.164 format, a standard for international telecommunications.

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $phone = '+1XXX5550100'; try { $result = $SnSclient->checkIfPhoneNumberIsOptedOut([ 'phoneNumber' => $phone, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

List opted-out phone numbers

To retrieve a list of phone numbers where the owner has opted out of receiving SMS messages from your account, use the ListPhoneNumbersOptedOut operation.

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); try { $result = $SnSclient->listPhoneNumbersOptedOut(); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Publish to a text message (SMS message)

To deliver a text message (SMS message) directly to a phone number, use the Publish operation.

In this example, the phone number is in E.164 format, a standard for international telecommunications.

SMS messages can contain up to 140 bytes. The size limit for a single SMS publish action is 1,600 bytes.

For more details on sending SMS messages, see Sending an SMS Message.

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $message = 'This message is sent from a Amazon SNS code sample.'; $phone = '+1XXX5550100'; try { $result = $SnSclient->publish([ 'Message' => $message, 'PhoneNumber' => $phone, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }