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....
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)
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.
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
SMS Sandbox Mode
When you start off using SNS for the first time for sending SMS, it starts in the so called 'Sandbox Mode'.
In this mode, you must verify the phone numbers that you intend to use.
If you are using SMS in Sandbox Mode and try to send an SMS via SNS to an unverified number, you will get the error
This prevents unsolicited text messaging.
If you try to create an SMS subscription to an SNS topic without a phone number that's been verified you will indeed receive the error:
Enter a valid mobile number
The phone number is valid. You just need to verify it first, as described above, before you can create this subscription in SNS
Of course, the same principle applies, when you're trying to achieve this via code.
Related
I am attempting to send an email through AWS SES using the php sdk. Please note that I am able to send a message through the aws CLI - no problem, and I am able to send an email from the SES console using the domain and email areas under Identity Management area. Also, both the domain and email have been verified and appear in the Identity Management area.
I am using the example code from the PHP SDK. I did edit the region as my emails and domains have been verified, and I operate out of the us-west-2 region.
<?php
/**
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This file is licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License. A copy of
* the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
require 'vendor/autoload.php';
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
$SesClient = new Aws\Ses\SesClient([
'profile' => 'default',
'version' => '2010-12-01',
'region' => 'us-west-2'
]);
$html_body = '<h1>AWS Amazon Simple Email Service Test Email</h1>' .
'<p>This email was sent with <a href="https://aws.amazon.com/ses/">' .
'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-php/">' .
'AWS SDK for PHP</a>.</p>';
$subject = 'Amazon SES test (AWS SDK for PHP)';
$plaintext_body = 'This email was send with Amazon SES using the AWS SDK for PHP.';
$sender_email = 'email_address';
$recipient_emails = ['email_address'];
$char_set = 'UTF-8';
$configuration_set = 'ConfigSet';
try {
$result = $SesClient->sendEmail([
'Destination' => [
'ToAddresses' => $recipient_emails,
],
'ReplyToAddresses' => [$sender_email],
'Source' => $sender_email,
'Message' => [
'Body' => [
'Html' => [
'Charset' => $char_set,
'Data' => $html_body,
],
'Text' => [
'Charset' => $char_set,
'Data' => $plaintext_body,
],
],
'Subject' => [
'Charset' => $char_set,
'Data' => $subject,
],
],
// If you aren't using a configuration set, comment or delete the
// following line
'ConfigurationSetName' => $configuration_set,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo "\n";
}
I am providing the credentials for the SDK through the ~/.aws/credential file, that contains the access ID key and secret key. This is the same file that is accessed by the AWS CLI, so the credentials appear to be correct and accessible to the CLI.
[default]
aws_access_key_id = AKIA2PK........Z72J3
aws_secret_access_key = co9r41j/i+QTrsA7244xG........KPH1LyBn34b
I added the "........" to the above to protect my access and secret access keys.
I get the following error that indicates that my email has not been verified. But it has! Also, I am not in the sandbox mode.
"Error executing "SendEmail" on "https://email.us-west-2.amazonaws.com"; AWS HTTP error: Client error: POST https://email.us-west-2.amazonaws.com resulted in a 400 Bad Request response: Sender MessageReje (truncated...) MessageRejected (client): Email address is not verified. The following identities failed the check in region US-WEST-2: msellers#ranchmed.com - Sender MessageRejected Email address is not verified. The following identities failed the check in region US-WEST-2: msellers#ranchmed.com dbbb0c1d-5493-4205-9257-a2fbb81eb5d9 The email was not sent. Error message: Email address is not verified. The following identities failed the check in region US-WEST-2: msellers#ranchmed.com"
Any ideas on how to debug this?
Appreciate any help that you can provide.
Mark
Found the problem. I hope this helps other aws sdk users. The sdk credential credentials provider found an .aws/credentials file for the webserver user and not from my home directory. In my case, looked in the file /var/cache/nginx/.aws/credentials and found some credentials defined for previous testing purposes.
Lesson is, be aware of how the aws sdk finds the credentials file. If the web server user is set up with a credentials file, then it will use that credential file in preference to one located in your $HOME/.aws/credentials file.
I am working on a project where under certain events it will send an SMS message.
What I was planning was when the SMS was delivered, I would keep a record, both for debugging purposes if required, and to keep a history of what's happening with my service. One of the things I was planning on logging was the cost of the message being delivered. It looked like from the Twilio documentation that this is possible, except when I send the message, the response I get back is status queued and price is null.
I then looked at using the Twilio webhook call back so that I receive an update on the status of the SMS delivery, which also works fine exception I don't get the price I only receive the following:
(
[SmsSid] => redacted
[SmsStatus] => delivered
[MessageStatus] => delivered
[To] => redacted
[MessageSid] => redacted
[AccountSid] => redacted
[From] => redacted
[ApiVersion] => 2010-04-01
)
For reference below is how I am sending the SMS
$twilio = new \Twilio\Rest\Client($sid, $token);
$message = $twilio->messages->create("redacted", array(
"body" => $smsContent,
"from" => "redacted",
"statusCallback" => $callback
));
How can I get the price of the SMS delivery from Twilio?
I've figured a way but it seems a little inefficient as I have to send another request to Twilio to get the full status of the SMS but this is how I've done.
I receive a Twilio request via webhook and I then fetch the message the message using the SmsId that is posted like the following:
$smssid = $_POST["SmsSid"];
$twilio = new \Twilio\Rest\Client($sid, $token);
$message = $twilio->messages($smssid)->fetch();
$twilioResponse = new TwilioResponse($message);
FYI TwilioResponse is my own class that has the $message passed in a constructor and I then use it to build and parse my own version for my needs.
I have a flash based ios app that I am currently testing on phone/ipod.
I have added push notification to the app and I am receiving device tokens from apple.
I have added those tokens to AWS SNS APN SandBox
The platform Application ARN looks like
arn:aws:sns:us-east-1:7860818154325:app/APNS_SANDBOX/gamename
I have added tokens to the PAA and they look like:
token: f63d05538cd7d9b86c76188dbfeb42edf26a1e23b47334a02a205e0fb54a812a
endpoint ARN: arn:aws:sns:us-east-1:7860818154325:endpoint/APNS_SANDBOX/gamename/7412740a-79e4-3b71-8e7f-56e127bc4cg1
Sending out the notification via PHP:
require 'vendor/autoload.php';
$sns = Aws\Sns\SnsClient::factory(array(
'key' => 'mykey',
'secret' => 'mysecret',
'region' => 'us-east-1'
));
$snsmessage = $sns->publish(array(
'TargetArn' => "$endpoint",
'Message' => 'A game is waiting for you to play'
));
(where $endpoint = "arn:aws:sns:us-east-1:7860818154325:endpoint/APNS_SANDBOX/gamename/7412740a-79e4-3b71-8e7f-56e127bc4cg1")
$snsmessage returns a message ID, so it looks like it is sending the message.
However no message is ever received on the phone.
I have tried manually sending a message from the SNS portal and the same issue. IT sends with no error message but it never arrives.
I am building the app as "device testing" and I am using provisionprofile that has push notifcation enabled.
I am not sure where to start looking to find the issue.
Any help would be appreciated.
I am using the AWS php SDK to send SNS messages. I am using APNS. I have followed the SDK examples, no problems there. Most of my messages are getting sent, but some appear to be failing. According to the SNS doc, if I get a message ID back - which I am every single time - then the message has been saved to the queue. How do I get more detailed information about these failed messages? The messages are obviously failing in the attempt to send between AWS and APNS.
$result = $client->publish(array(
'TopicArn' => 'string',
'TargetArn' => 'string',
// Message is required
'Message' => 'string',
'Subject' => 'string',
'MessageStructure' => 'string',
));
I am working on a project that notifies an android application using push notifications using Google Cloud Messaging. I have implemented the application server in PHP. When i run the android application, the device gets the registration id from the GCM server and it sends the registration id to the PHP server(application server). But i get the following httpresponse from the server :
{
"multicast_id": 7015234441922271670,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [{
"message_id": "0:1344007383866721%2adac3a0ad8b3148"
}]
}
So, the message is getting delivered since the success flag is 1, but when i display the message in the android app, it displays nothing(null). I am not able to figure out what happens with the message.
I have the exact same question ... this is what i'm trying this weekend:
http://developer.android.com/guide/google/gcm/gcm.html
excerpt:
Note: If your organization has a firewall that restricts the traffic to or from the Internet, you'll need to configure it to allow connectivity with GCM. The ports to open are: 5228, 5229, and 5230. GCM typically only uses 5228, but it sometimes uses 5229 and 5230. GCM doesn't provide specific IPs. It changes IPs frequently. We recommend against using ACLs but if you must use them, take a broad approach such as the method suggested in this support link.
Add this in your php code
$data = array(
'registration_ids' => array($reg),
'data' => array(
'type' => 'New',
'title' => 'App Name',
'flag' => '1',
'msg' => 'New Message')
Hope this will help.