Last active
September 15, 2017 12:08
-
-
Save geerteltink/93abd0647936e4fd03965461970bc3c6 to your computer and use it in GitHub Desktop.
AWS SES API parallel test. A fast way to send multiple emails with SES.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// http://docs.aws.amazon.com/ses/latest/APIReference/API_SendEmail.html | |
// http://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.CommandPool.html | |
require __DIR__ . '/vendor/autoload.php'; | |
use Aws\CommandInterface; | |
use Aws\CommandPool; | |
use Aws\Exception\AwsException; | |
use Aws\ResultInterface; | |
use Aws\Ses\SesClient; | |
use GuzzleHttp\Promise\PromiseInterface; | |
// Create SES Client | |
$client = new SesClient([ | |
'version' => 'latest', | |
'region' => '', | |
'credentials' => [ | |
'key' => '', | |
'secret' => '', | |
], | |
]); | |
$recipients = [ | |
'success@simulator.amazonses.com', | |
'bounce@simulator.amazonses.com', | |
'ooto@simulator.amazonses.com', | |
]; | |
// Shuffle recipients for testing purposes | |
shuffle($recipients); | |
// Queue emails as SendEmail commands | |
$i = 100; | |
$commands = []; | |
foreach ($recipients as $recipient) { | |
$commands[] = $client->getCommand('SendEmail', [ | |
// Pass the message id so it can be updated after it is processed (it's ignored by SES) | |
'x-message-id' => $i, | |
'Source' => 'AWS SES parallel test <info@example.com>', | |
'Destination' => [ | |
'ToAddresses' => [$recipient], | |
], | |
'Message' => [ | |
'Subject' => [ | |
'Data' => 'SES API test', | |
'Charset' => 'UTF-8', | |
], | |
'Body' => [ | |
'Html' => [ | |
'Data' => 'This is a <b>test</b>.', | |
'Charset' => 'UTF-8', | |
], | |
], | |
], | |
]); | |
$i++; | |
} | |
try { | |
$timeStart = microtime(true); | |
$pool = new CommandPool($client, $commands, [ | |
'concurrency' => 10, | |
'before' => function (CommandInterface $cmd, $iteratorId) { | |
$a = $cmd->toArray(); | |
echo sprintf('About to send %d: %s' . PHP_EOL, $iteratorId, $a['Destination']['ToAddresses'][0]); | |
}, | |
'fulfilled' => function (ResultInterface $result, $iteratorId) use ($commands) { | |
echo sprintf( | |
'Completed %d: %s' . PHP_EOL, | |
$commands[$iteratorId]['x-message-id'], | |
$commands[$iteratorId]['Destination']['ToAddresses'][0] | |
); | |
}, | |
'rejected' => function (AwsException $reason, $iteratorId) use ($commands) { | |
echo sprintf( | |
'Failed %d: %s' . PHP_EOL, | |
$commands[$iteratorId]['x-message-id'], | |
$commands[$iteratorId]['Destination']['ToAddresses'][0] | |
); | |
}, | |
]); | |
// Initiate the pool transfers | |
$promise = $pool->promise(); | |
// Force the pool to complete synchronously | |
$promise->wait(); | |
$timeEnd = microtime(true); | |
echo sprintf('Operation completed in %s seconds' . PHP_EOL, $timeEnd - $timeStart); | |
} catch (Exception $e) { | |
echo sprintf('Error: %s' . PHP_EOL, $e->getMessage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great work. Thank you for your example!
But I think you have a small typo: Shouldn't $sesClient on lines 37 and 63 be $client?