Send sms to Indian numbers using aws sns - php

I am trying to send a sms to a particular number using aws sns using the following code but it requires a target/topic arn which is not applicable for me as I want to send sms to the numbers that I set in the params.
$client = SnsClient::factory([
'credentials' => [
'key' => "my key",
'secret' => "my secret"
],
'region' => "us-east-1",
'version' => "latest"
]);
$sent = $client->publish(
[
'Message' => 'This is the message',
'PhoneNumber' => '+91887******7'
]
);
I am not able to find the exact params for the publish object.

I had to update the aws php sdk (1). Then the following code works fine -
$sns = SnsClient::factory(array(
'credentials' => array(
'key' => 's3_key',
'secret' => 's3_secret'
),
'region' => Constants::$s3Region,
'version' => 'latest'
));
$msgattributes = [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'Klassroom',
],
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Transactional',
]
];
$payload = array(
'Message' => $message,
'PhoneNumber' => $number,
'MessageAttributes' => $msgattributes
);
$result=$sns->publish($payload);

From the Amazon SNS Publish() documentation for PHP:
TargetArn: If you don't specify a value for the TargetArn parameter, you must specify a value for the PhoneNumber or TopicArn parameters.
TopicArn: If you don't specify a value for the TopicArn parameter, you must specify a value for the PhoneNumber or TargetArn parameters.
PhoneNumber: If you don't specify a value for the PhoneNumber parameter, you must specify a value for the TargetArn or TopicArn parameters.
Therefore, provide a PhoneNumber instead of a Target/Topic.

Related

AMAZON SNS / SMS , sending sms , delivery status

I am using amazon PHP SDK to try to send sms I am using following code:
<?php
require 'vendor/autoload.php';
$sdk = new Aws\Sns\SnsClient([
'region' => 'eu-west-1',
'version' => 'latest',
'credentials' => ['key' => 'xxx', 'secret' => 'xxx']
]);
$result = $sdk->publish([
'Message' => 'This is a test message.',
'PhoneNumber' => '+123456789',
'MessageAttributes' => ['AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'testing sms'
]
]]);
print_r( $result );
My question is how do I get delivery Status back to an endpoint url HTTPS or HTTP?
like the sms got delivered or failed? any idea?
The only way to do this today is to leverage the CloudWatch API (e.g. https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_GetLogEvents.html) to read the logs delivered to your log groups

How to send an email through AWS simple email service(ses) using signature version 4 in PHP

Currently I'm sending email through AWS SDK SES signature 3 and got an email from Amazon to upgrade it to SES signature version 4. But where to add signature in AWS SDK? below is the current code that is being used to send emails.
<?php
//SES
$SESCredentials = array(
'key' => awsSESAccessKey,
'secret' => awsSESSecretKey
);
$SESClient = new SesClient([
// 'profile' => 'default',
'version' => AWS_SDK_VERSION,
'region' => AWS_REGION,
'http' => [
'verify' => AWS_CERT_PATH
],
'credentials' => $SESCredentials
]);
$result = $SESClient->sendEmail([
'Destination' => [
'ToAddresses' => $recipient_emails
],
'ReplyToAddresses' => [$sender_email],
'Source' => $sender_email,
'Message' => [
'Body' => [
'Html' => [
'Charset' => $char_set,
'Data' => $html_body
]
],
'Subject' => [
'Charset' => $char_set,
'Data' => $subject,
]
]
]);
?>
You can use this package as it supports signature 4
https://github.com/daniel-zahariev/php-aws-ses
$signature_version = SimpleEmailService::REQUEST_SIGNATURE_V4;
$ses = new SimpleEmailService('AccessKey', 'SecretKey', $region_endpoint,
$trigger_error, $signature_version);
$m->addTo('Recipient Name <recipient#example.com>');
$m->setFrom('Sender <user#example.com>');
$m->setSubject('Hello, world!');
$m->setMessageFromString('This is the message body.');
$ses = new SimpleEmailService('AccessKey', 'SecretKey');
print_r($ses->sendEmail($m));

How to send a simple email in AWS Pinpoint using the PHP SDK?

The official AWS PHP SDK Pinpoint documentation is so dense that even sending a simple email seems like a daunting task :)
https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-pinpoint-2016-12-01.html#sendmessages
$result = $client->sendMessages([
'ApplicationId' => '<string>', // REQUIRED
'MessageRequest' => [ // REQUIRED
'Addresses' => [
'<__string>' => [
'BodyOverride' => '<string>',
'ChannelType' => 'GCM|APNS|APNS_SANDBOX|APNS_VOIP|APNS_VOIP_SANDBOX|ADM|SMS|VOICE|EMAIL|BAIDU|CUSTOM',
'Context' => ['<string>', ...],
'RawContent' => '<string>',
'Substitutions' => [
'<__string>' => ['<string>', ...],
// ...
],
'TitleOverride' => '<string>',
],
// ...
],
'Context' => ['<string>', ...],
'Endpoints' => [
'<__string>' => [
'BodyOverride' => '<string>',
'Context' => ['<string>', ...],
'RawContent' => '<string>',
'Substitutions' => [
'<__string>' => ['<string>', ...],
// ...
],
'TitleOverride' => '<string>',
],
// ...
],
'MessageConfiguration' => [ // REQUIRED
'EmailMessage' => [
'Body' => '<string>',
'FeedbackForwardingAddress' => '<string>',
'FromAddress' => '<string>',
'RawEmail' => [
'Data' => <string || resource || Psr\Http\Message\StreamInterface>,
],
'ReplyToAddresses' => ['<string>', ...],
'SimpleEmail' => [
'HtmlPart' => [
'Charset' => '<string>',
'Data' => '<string>',
],
'Subject' => [
'Charset' => '<string>',
'Data' => '<string>',
],
'TextPart' => [
'Charset' => '<string>',
'Data' => '<string>',
],
],
'Substitutions' => [
'<__string>' => ['<string>', ...],
// ...
],
],
],
'TemplateConfiguration' => [
'EmailTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
'PushTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
'SMSTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
'VoiceTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
],
'TraceId' => '<string>',
],
]);
Does someone have a working code snippet just for sending a simple email using the PHP SDK v3?
The following php script demonstrates how you can use AWS PHP SDK v3 to send emails using Amazon Pinpoint service using the sendMessage API.
Feel free to fork it from gists and customize to fit your need.
<?php
require 'vendor/autoload.php';
use Aws\Pinpoint\PinpointClient;
use Aws\Exception\AwsException;
/**
* #author syumaK(Amos Syuma)
* Date: March 24, 2020
* Description: A php script that uses AWS PHP SDK for Pinpoint service to send an email message.
*
*/
/**
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
// Instantiate a client with the credentials from the credential profiles (.aws/credentials) file.
$settings = (array(
'profile' => 'syumaK',
'region' => 'us-east-1',
'version' => 'latest',
));
$pinpointClient = new Aws\Pinpoint\PinpointClient($settings);
# The "From" address. This address has to be verified in Amazon Pinpoint in the region you're using to send email.
$SENDER = "redacted";
# The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses also have to be verified.
$TOADDRESS = "redacted";
try {
$result = $pinpointClient->sendMessages([
'ApplicationId' => '4fd13xxxxxxxxx', // REQUIRED
'MessageRequest' => [ // REQUIRED
'Addresses' => [
$TOADDRESS => [
'ChannelType' => 'EMAIL',
],
],
'MessageConfiguration' => [ // REQUIRED
'EmailMessage' => [
'FromAddress' => $SENDER,
],
],
'TemplateConfiguration' => [ // REQUIRED
'EmailTemplate' => [
'Name' => 'One',
'Version' => '1',
],
],
],
]);
print $result;
} catch (AwsException $e){
// output error message if fails
error_log($e->getMessage());
}
?>
Tested the above code snippet using the following environment spec:
Ubuntu: 18.04
Apache2: 2.4.18
aws-sdk-php": "^3.108"
Hope this helps!
Based on syumaK answer, I finally got it working with this snippet:
<?php
require 'vendor/autoload.php';
use Aws\Pinpoint\PinpointClient;
use Aws\Exception\AwsException;
/**
* #author syumaK(Amos Syuma)
* Date: March 24, 2020
* Description: A php script that uses AWS PHP SDK for Pinpoint service to send an email message.
*
*/
/**
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
// Instantiate a client with the credentials from the credential profiles (.aws/credentials) file.
$settings = (array(
'profile' => 'syumaK',
'region' => 'us-east-1',
'version' => 'latest',
));
$pinpointClient = new Aws\Pinpoint\PinpointClient($settings);
# The "From" address. This address has to be verified in Amazon Pinpoint in the region you're using to send email.
$SENDER = "redacted";
# The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses also have to be verified.
$TOADDRESS = "redacted";
try {
$result = $pinpointClient->sendMessages([
'ApplicationId' => 'REPLACE WITH APP ID (NOT NAME)',
'MessageRequest' => [ // REQUIRED
'Addresses' => [
$TOADDRESS => [
'ChannelType' => 'EMAIL',
],
],
'MessageConfiguration' => [ // REQUIRED
'EmailMessage' => [
'FromAddress' => $SENDER,
'SimpleEmail' => [
'HtmlPart' => [
'Charset' => 'utf-8',
'Data' => 'my sample html',
],
'Subject' => [
'Charset' => 'utf-8',
'Data' => 'my sample subject',
],
'TextPart' => [
'Charset' => 'utf-8',
'Data' => 'my sample text',
]
]
],
],
]
]);
print $result;
} catch (AwsException $e){
// output error message if fails
error_log($e->getMessage());
}
?>

aws-sdk-php v3 sns not working for me

I really want to publish a lambda subscriber to an SNS topic. It works in amazon console, but with php-sdk it does not. When I execute the php code and I look in CloudWatch, there's nothing (no response). Why my SNS did not work?
it is my code, but it does not working:
....
$credentials = new Credentials($access, $secret);
$this->aws_client = new SnsClient(
array(
'region' => 'us-east-1',
'version' => 'latest',//'version' => '2010-03-31',
'credentials' => $credentials
)
);
...
$message = "Hello H.";
$message_atr = array('String' => array('DataType' => 'String', 'StringValue' => $message));
$payload = array(
'TargetArn' => 'arn:aws:sns:us-east-1:****:mysns',
'Message' => $message,
'MessageAttributes' => $message_atr,
);
$this->aws_client->publish($payload);

SenderID using Amazon Web Services PHP SDK

I'm trying to send an SMS through my PHP site using the AWS SDK. I use the code from Sending SMS with Amazon AWS services PHP.
require $_SERVER['DOCUMENT_ROOT'] . '/inc/aws/aws-autoloader.php';
error_reporting(E_ALL);
ini_set("display_errors", 1);
$params = array(
'credentials' => array(
'key' => 'abc',
'secret' => 'abc',
),
'region' => 'eu-west-1', // < your aws from SNS Topic region
'version' => 'latest'
);
$sns = new \Aws\Sns\SnsClient($params);
$args = array(
"SenderID" => "TestSender",
"SMSType" => "Transactional",
"Message" => "Sending SMS with PHP",
"PhoneNumber" => "+87654321"
);
$result = $sns->publish($args);
echo "<pre>";
var_dump($result);
echo "</pre>";
This is not working. I have tested with a lot of different SenderIDs and all messages are received from NOTICE.
However, when I send a message from the AWS console, the message is received with the correct SenderID. So I assume my code is wrong.
I found the solution. Set the args this way. It works!
$args = array(
'MessageAttributes' => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'YourSenderName'
]
],
"SMSType" => "Transactional",
"PhoneNumber" => "+87654321",
"Message" => "Hello World!"
);

Categories