How to set the credentials to send an email with AmazonSES using the PHP SDK from aws
-
23-03-2021 - |
質問
I am trying to use the latest SDK for PHP (v. 1.5.0). I am trying to send an email with AmazonSES. I have successully sent emails with the python scripts, so I know that my crendentials and other settings are okay.
I have copied the sample code however, it does not work. When calling AmazonSES, I get an error saying:
Catchable fatal error: Argument 1 passed to AmazonSES::__construct() must
be an array, string given, called in sendemail.php on line 31 and
defined in sdk-1.5.0/services/ses.class.php on line 67
This is the code:
$AWS_KEY = "AKIEDIEDEIMIAXEOA";
$AWS_SECRET_KEY = "Te+EDEwjndjndededededededj";
require_once("../library/lib_aws/sdk-1.5.0/sdk.class.php");
$amazonSes = new AmazonSES($AWS_KEY, $AWS_SECRET_KEY);
$response = $amazonSes->send_email(
"from@email.com",
array("ToAddresses" => "to@email.com"),
array(
"Subject.Data" => "test",
"Body.Text.Data" => "body test",
)
);
if (!$response->isOK())
{
echo "error";
}
I cannot find how to set up the credentials correctly to send an email.
解決
Yeah, the config file format and the service constructors changed slightly in version 1.5. They mentioned this as a backwards-incompatible change in the release notes.
他のヒント
The constructor for AmazonSES takes an array with options. Check the source:
https://github.com/amazonwebservices/aws-sdk-for-php/blob/master/services/ses.class.php#L55
You'll want to write it like:
$amazonSes = new AmazonSES(array(
"key" => $AWS_KEY,
"secret" => $AWS_SECRET_KEY
));
Please click through to the source (or consult the docs) to make sure there aren't other options you need to set.
ToAddress's value must be an array, not a string. This should work:
require_once('amazonsdk/sdk.class.php');
$ses = new AmazonSES();
$response = $ses->send_email(
"senderaddress@example.com",
array("ToAddresses"=>array('receiveraddress@example.com')),
array("Subject.Data"=>"Testing SES subject","Body.Text.Data"=>"Testing SES body.")
);
print_r($response);