Gaurav Singh
Gaurav Singh

Reputation: 79

AWS SNS SMS for Indian Numbers

I'm using AWS SDK 3.0 to send SMS via AWS SNS for indian numbers. I've ec2 instance created on AWS. Please refer the code below :

$params = array(
        'credentials' => array(
            'key' => 'XXXXXX',
            'secret' => 'XXXXXXX',
        ),
        'region' => 'us-west-2', // < your aws from SNS Topic region
        'version' => 'latest',
         'http' => [ 'verify' => false ]
    );
    $sns = new \Aws\Sns\SnsClient($params);

    $args = array(
        'MessageAttribute' => [
            'AWS.SNS.SMS.SenderID' => [
                'DataType'    => 'String',
                'StringValue' => 'Sender',
            ],
            'AWS.SNS.SMS.SMSType'  => [
                'DataType'    => 'String',
                'StringValue' => 'Transactional',
            ]
        ],
        "Message" => "Test Message",
        "PhoneNumber" => "+91XXXXXX",
        'MessageStructure' => 'string',
    );

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

This code is sending SMS. However, the SMS are getting logged as Promotional SMS instead of Transactional. Thus getting delivered between 9am to 8pm only.

Any help is much appreciated.

Upvotes: 0

Views: 730

Answers (1)

Madhukar Mohanraju
Madhukar Mohanraju

Reputation: 2863

Your programming logic is fine. There is a typo in you script.It is MessageAttributes not MessageAttribute. Due to this typo, it is ignoring the Message attributes which you are passing and taking the default message type set in your Text messaging preferences section in SNS aws console which is Promotional.

Correct Code:

$args = array(
    'MessageAttributes' => [
        'AWS.SNS.SMS.SenderID' => [
            'DataType'    => 'String',
            'StringValue' => 'Sender',
        ],
        'AWS.SNS.SMS.SMSType'  => [
            'DataType'    => 'String',
            'StringValue' => 'Transactional',
        ]
    ],
    "Message" => "Test Message",
    "PhoneNumber" => "+91XXXXXX",
    'MessageStructure' => 'string',
);

Upvotes: 4

Related Questions