Question

I am attempting to send mail using AWS SES sendEmail method, and I'm having trouble with an error. I have read this question: AWS SDK Guzzle error when trying to send a email with SES

I am dealing with a very similar issue. The original poster indicates that they have a solution, but did not post the solution.

My code:

$response = $this->sesClient->sendEmail('example@example.com', 
array('ToAddresses' => array($to)), 
array('Subject.Data' => array($subject), 'Body.Text.Data' => array($message)));

Guzzle code producing the error (from aws/Guzzle/Service/Client.php):

return $this->getCommand($method, isset($args[0]) ? $args[0] : array())->getResult();

Error produced:

Catchable fatal error: Argument 2 passed to Guzzle\Service\Client::getCommand() must be of the type array, string given

Looking at the Guzzle code, I can see that the call to getCommand will send a string if args[0] is set and is a string. If args[0] is NOT set then an empty array is sent.

What am I missing here please?

Was it helpful?

Solution

SOLUTION: It turns out I was trying to use SDK1 data structures on the SDK2 code base. Thanks to Charlie Smith for helping me to understand what I was doing wrong.

For others (using AWS SDK for PHP 2) :

Create the client -

$this->sesClient = \Aws\Ses\SesClient::factory(array(
    'key'    =>AWS_ACCESS_KEY_ID,
    'secret' => AWS_SECRET_KEY,
    'region' => Region::US_EAST_1
));

Now, structure the email (don't forget that if you're using the sandbox you will need to verify any addresses you send to. This restriction doesn't apply if you have been granted production status) -

$from = "Example name <example@example.com>";
$to ="example@verified.domain.com";
$subject = "Testing AWS SES SendEmail()";

$response = $this->sesClient->getCommand('SendEmail', array(
    'Source' => $from,
    'Destination' => array(
        'ToAddresses' => array($to)
    ),
    'Message' => array(
        'Subject' => array(
            'Data' => $subject
        ),
        'Body' => array(
            'Text' => array(
                'Data' => "Hello World!\n Testing AWS email sending."
            ),
            'Html' => array(
                'Data' => "<h1>Hello World!</h1><p>Testing AWS email sending</p>"
            )
        ),
    ),
))->execute();

That should work now.

Here is the relevant section in the AWS SDK for PHP 2 documentation:

http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.Ses.SesClient.html#_sendEmail

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top