Search code examples
phpamazon-web-servicesaws-php-sdk

AWS SES Guzzle Error when sending email


I have set up AWS Ses service using PHP SDK:

$this->client = SesClient::factory([
    'key' => $params['key'],
    'secretKey' => $params['secret_key'],
    'region' => 'eu-west-1',
    'base_url' => 'https://email-smtp.eu-west-1.amazonaws.com',
]);

$this->client->sendEmail($this->params());

public function params() {
    array(
        'Source' => '[email protected]',
        'Destination' => array(
            'ToAddresses' => array('[email protected]')
        ),
        'Message' => array(
            'Subject' => array(
                'Data' => 'SES Testing',
                'Charset' => 'UTF-8',
            ),
            // Body is required
            'Body' => array(
                'Text' => array(
                    'Data' => 'My plain text email',
                    'Charset' => 'UTF-8',
                ),
                'Html' => array(
                    'Data' => '<b>My HTML Email</b>',
                    'Charset' => 'UTF-8',
                ),
            ),
        ),
        'ReplyToAddresses' => array( '[email protected]' ),
        'ReturnPath' => '[email protected]'
    );
}

After trying to send email, I receive this error message:

exception 'Guzzle\Http\Exception\CurlException' with message 
'[curl] 23: Failed writing body (0 != 86) [url] https://email-smtp.eu-west-1.amazonaws.com/' 
in C:\xampp\htdocs\myProject\protected\lib\vendor\guzzle\guzzle\src\Guzzle\Http\Curl\CurlMulti.php:338

Anyone know how to fix that error?

cUrl has support for SMTP, but smtp://email-smtp.eu-west-1.amazonaws.com throws error [curl] 1: Protocol smtp not supported or disabled in libcurl [url] smtp://email-smtp.eu-west-1.amazonaws.com/ cUrl


Solution

    1. Use https://email.{$region}.amazonaws.com as base_url
      At this stage, i get error: 'User 'arn:aws:iam::######:user/ebpush' is not authorized to perform 'ses:SendEmail' on resource `arn:aws:ses:eu-west-1:447362101282:identity/[email protected]'
    2. Add IAM > Groups > Create group > AmazonSESFullAccess
    3. Go to IAM > Policies > AmazonSESFullAccess > Attach > ebPush

    OR

    Use PHPMailer to connect via email-smtp.{region}.amazonaws.com where Username is your SES Access Key and password is your SES Secret Key