[英]Sending email to Amazon SES in PHP freezes when triggered by request
[英]PHP Sending Large Amounts of Email to Amazon SES
我有一個在線軟件,可以向Amazon SES發送電子郵件。 目前我有一個cron作業,通過SMTP發送電子郵件與phpmailer發送消息。 目前我必須將發送限制最大限制為每分鍾300左右,以確保我的服務器沒有超時。 我們看到增長,最終我想發送到10,000或更多。
有沒有更好的方式發送到亞馬遜SES,或者這是其他人做的,但只有更多的服務器運行工作負載?
提前致謝!
您可以嘗試使用AWS SDK for PHP。 您可以通過SES API發送電子郵件,SDK允許您並行發送多封電子郵件。 這是一個代碼示例(未經測試且僅部分完成)以幫助您入門。
<?php
require 'vendor/autoload.php';
use Aws\Ses\SesClient;
use Guzzle\Service\Exception\CommandTransferException;
$ses = SesClient::factory(/* ...credentials... */);
$emails = array();
// @TODO SOME SORT OF LOGIC THAT POPULATES THE ABOVE ARRAY
$emailBatch = new SplQueue();
$emailBatch->setIteratorMode(SplQueue::IT_MODE_DELETE);
while ($emails) {
// Generate SendEmail commands to batch
foreach ($emails as $email) {
$emailCommand = $ses->getCommand('SendEmail', array(
// GENERATE COMMAND PARAMS FROM THE $email DATA
));
$emailBatch->enqueue($emailCommand);
}
try {
// Send the batch
$successfulCommands = $ses->execute(iterator_to_array($emailBatch));
} catch (CommandTransferException $e) {
$successfulCommands = $e->getSuccessfulCommands();
// Requeue failed commands
foreach ($e->getFailedCommands() as $failedCommand) {
$emailBatch->enqueue($failedCommand);
}
}
foreach ($successfulCommands as $command) {
echo 'Sent message: ' . $command->getResult()->get('MessageId') . "\n";
}
}
// Also Licensed under version 2.0 of the Apache License.
您還可以考慮使用Guzzle BatchBuilder
和朋友來使其更加強大。
您需要對此代碼進行微調,但您可能會獲得更高的電子郵件吞吐量。
如果有人正在尋找這個答案,它已過時,你可以在這里找到新的文檔: https : //docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/commands.html
use Aws\S3\S3Client;
use Aws\CommandPool;
// Create the client.
$client = new S3Client([
'region' => 'us-standard',
'version' => '2006-03-01'
]);
$bucket = 'example';
$commands = [
$client->getCommand('HeadObject', ['Bucket' => $bucket, 'Key' => 'a']),
$client->getCommand('HeadObject', ['Bucket' => $bucket, 'Key' => 'b']),
$client->getCommand('HeadObject', ['Bucket' => $bucket, 'Key' => 'c'])
];
$pool = new CommandPool($client, $commands);
// Initiate the pool transfers
$promise = $pool->promise();
// Force the pool to complete synchronously
$promise->wait();
對於SES命令也可以做同樣的事情
謝謝您的回答。 這是一個很好的起點。 @Jeremy Lindblom
我現在的問題是我無法使錯誤處理工作。 catch() - Block工作正常並在其中
$successfulCommands
返回所有成功響應狀態代碼,但僅在發生錯誤時返回。 例如沙箱模式中的“未驗證地址”。 像catch()應該工作。 :)
try-Block中的$ successfulCommands僅返回:
SplQueue Object
(
[flags:SplDoublyLinkedList:private] => 1
[dllist:SplDoublyLinkedList:private] => Array
(
)
)
我無法弄清楚如何通過狀態代碼等從亞馬遜獲得真實的響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.