5
votes

i am trying to learn aws sns service to send sms from my web application. I am working on localhost.

$params = array(
    'credentials' => array(
        'key' => 'iam_key',
        'secret' => 'iam_secret',
    ),
    'region' => 'ap-south-1', // < your aws from SNS Topic region
    'version' => 'latest',
    'http' => ['verify'=>false]
);
$sns = \Aws\Sns\SnsClient::factory($params);

$msgattributes = [
        'AWS.SNS.SMS.SenderID' => [
            'DataType' => 'String',
            'StringValue' => 'Klassroom',
        ],
        'AWS.SNS.SMS.SMSType' => [
            'DataType' => 'String',
            'StringValue' => 'Transactional',
        ]
    ];

$payload = array(
        'Message' => "HK test",
        "PhoneNumber" => "1234567890",
        'MessageAttributes' => $msgattributes
    );

$result = $sns->publish($payload);

i want to send sms directly on number. this code is giving this error

Fatal error: Uncaught exception 'Aws\Sns\Exception\SnsException' with message 'Error executing "Publish" on "https://sns.ap-south-1.amazonaws.com"; AWS HTTP error: Client error: POST https://sns.ap-south-1.amazonaws.com resulted in a 400 Bad Request response: Sender InvalidPara (truncated...) InvalidParameter (client): Invalid parameter: PhoneNumber Reason: +1234567890 is not valid to publish to - Sender InvalidParameter Invalid parameter: PhoneNumber Reason: +1234567890 is not valid to publish to 13e181c2-a20f-5e77-9c8e-d38c55c50266 ' exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: POST https://sns.ap-south-1.amazonaws.com resulted in a 400 Bad Request

i don't know why....

3
I would guess that the phone number first has to sign up for SNS notifications.ceejayoz
can't we send sms directly on number using aws sns service??Harshit Kudeshiya

3 Answers

0
votes

I believe a phone number for AWS has to include the country code.

For instance if my phone number is (999) 365-6721 it would be +19993656721 (for US)

0
votes

I had the exact same issue even though I called the API with a valid phone number prefixed with correct country code (for example +57xxxxxxxxxx).

Then I figured out after running through some threads on the internet that it was only because of the incorrect REGION_CODE (for example us-west-1 instead of us-east-1) used during the instantiation of SNSClient class.

    @Bean
public AmazonSNS snsClient() {
    return AmazonSNSClientBuilder.standard().withRegion("us-east-1").withCredentials(new AWSStaticCredentialsProvider(credentials())).build();
}

Refer to the link to find the right region code for your case.

0
votes

You need to use the region that supported SMS for AWS SNS.

More information: https://docs.aws.amazon.com/sns/latest/dg/sms_supported-countries.html