Search code examples
phpphpmailersleeplarge-data

How to send a large email?


$i = 1;
foreach ($recipients as $email => $name) {
    $mail->ClearAddresses();
    $mail->AddBCC($email, $name); 
    if (!$mail->send()) {
        $send = 0;
    } else {
        $send = 1;
    }
    $query = "INSERT INTO `newsletter_send`(`email`, `id_newsletter`, `date`, `send`) VALUES ('$email',$id_newsletter, NOW(),$send) ";
    $stmt = $link->prepare($query) or die('error');
    $stmt->execute();
    $mail->clearAllRecipients(); 

    if (($i % 100) == 0) {
        sleep(60);
    }

    $i++;
}

What is the best way to send a large emails without sleep() and without to wait the page to finish loading? In addition to the cron job you have other ideas ?

EDIT: I have 680 users who will receive the email, but after a while I get 500 Internal Server Error.. why? It maybe time_limit? enter image description here


Solution

  • Message queues.

    beanstalkd is a good solution.

    You can then use a SDK like pheanstalk to handle the queue and its jobs.

    EDIT: If you have restricted access to your server (for example, if you are using a shared hosting) message queues as a service are also an option.