I have a cron job which sends email to subscribers, using CodeIgniter 3 email class. I have used smtp mail service provided by the host. The cron job gives HTTP 500 error after running for 2-3 mins and sending 453 emails. I have used sleep after sending 400 emails. There are no limits on how many emails the server can send Please review the code below.
<?php $sn=1;$emailCount=0;
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.something.com',
'smtp_port' => 587,
'smtp_user' => 'something@some.com',
'smtp_pass' => 'somepass',
'mailtype' => 'html',
'charset' => 'utf-8',
'smtp_crypto' => 'tls'
);
$this->email->initialize($config);
log_message("info", "Email Cron job started");
foreach($member as $row){
$this->email->clear();
$this->email->set_mailtype("html");
$this->email->set_newline("
");
$this->email->from('something@some.com','Sm');
$this->email->subject($title);
$this->email->set_header('Header1', 'MIME-Version: 2.5.6');
$this->email->to(trim($row->email_address));
ob_start();
?>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr><td height="15"> </td></tr>
<!-- many lines of code just html-->
<tr><td height="15"> </td></tr>
</tbody>
</table>
<?php
$message = ob_get_contents();
ob_clean();
ob_flush();
$this->email->message($message);
if($emailCount % 400 == 0 && $emailCount != 0)
{
sleep(15);
log_message("info", "Sleep.Emails Sent: ".$emailCount);
}
if($this->email->send())
{
$emailCount++;
}
else
{
log_message("info", "Error! Total Emails Sent: ".$emailCount);
}
$sn++;
}
log_message("info", "Total Emails Sent: ".$emailCount);
log_message("info", "Total Loop Ran: ".$sn);?>
Tried the sleep() but didn't work.