最近,我决定使用Mandrill发送电子邮件。我需要一次发送大约30 000封电子邮件,我认为使用Mandrill批量发送可以解决这个问题,但是当我在Mandrill网站上查看API日志时,每个发送的电子邮件都会显示为单独的API调用(约200ms通话时间)。每封已发送电子邮件的结果状态等于“已发送”。
当收到10个以上的收件人时,我可以收集信息,它应该异步工作,状态应该排队等等。
我现在发送大约600封电子邮件,CRON脚本大约需要7分钟才能执行。这太长了。
以下是代码:
public function getSendEmails()
{
\Log::info('get-send-emails (start)');
$tasks = $this->task->where('published_at', '>', date('Y-m-d', time()))->whereNotNull('published_at')->get();
foreach ($tasks as $task)
{
$users = $this->user->where('task_id', $task->id)
->where('allow_emails', 1)
->where('activated', 1)
->get();
$users_array = array();
foreach ($users as $user)
{
$users_array[] = array(
'email' => $user->email,
'name' => $user->first_name . ' ' . $user->last_name,
'type' => 'to'
);
}
if (!empty($users_array))
{
$html = View::make('emails.notifications.task')->with('task', $task)->render();
$batches = array_chunk($users_array, ceil($users->count()/1000.0));
foreach($batches as $batch)
{
try
{
$mandrill = new \Mandrill('some API key');
$message = array(
'html' => $html,
'subject' => 'Some Subject!',
'from_email' => 'some@email.com',
'from_name' => 'Some Name',
'to' => $batch,
'preserve_recipients' => false
);
$async = true;
$result = $mandrill->messages->send($message, $async);
}
catch(\Mandrill_Error $e)
{
echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
throw $e;
}
}
}
}
\Log::info('get-send-emails (end)');
}
你能不能告诉我,我做错了什么,或者这应该花多长时间?
答案 0 :(得分:3)
看起来你正在将阵列分成很少的收件人。假设您有10个用户(因此$users->count()
为10),您将使用此行将批量大小设置为1,因为ceil(10/1000.0)
将返回1:
$batches = array_chunk($users_array, ceil($users->count()/1000.0));
即使有30K收件人,您也要将每个批次设置为30个收件人(因此仍然是相对较小的批次)。您可能希望将其更改为类似的内容,以使每个批次成为1000个收件人:
$batches = array_chunk($users_array, 1000);