Forum Moderators: coopster
This works fine when I execute the script via my browser or via the command line, but I would like the emails to be processed by a cron job executing every 5 mins. When I do this only 21 emails are sent. (the table is updated with each attempt so I can see exactly how many are sent each time). This is on 1and1 shared hosting solution.
Any ideas why this might be? Any solutions? Currently I would only be able to send 21 emails every 5 mins which is not enough for my purposes.
//method to process the queue
function ProcessQueue(){
// query email_queue for records where success = 0
$sql = "SELECT * FROM email_queue WHERE success = 0 AND max_attempts != attempts LIMIT " . $this->max_emails_per_batch;
$result = mysql_query($sql);
// check if records found
if (mysql_num_rows( $result )) {
// prepare mailer
$smtp = Mail::factory('mail');
// loop through records to send emails
while ($queued_mail = mysql_fetch_array($result)) {
// send email
$to = $queued_mail['to_email'];
$subject = $queued_mail['subject'];
$body = $queued_mail['message'];
$from = $queued_mail['from_name'] . ' < ' . $queued_mail['from_email'] . '>';
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
// else update attempts, last attempt
$sql = "UPDATE email_queue SET " .
"attempts = attempts+1, " .
"last_attempt = now() " .
"WHERE id = '" . $queued_mail['id'] . "'";
mysql_query($sql);
echo( $mail->getMessage() );
} else {
// if successful, update attempts, success, last attempt, date_sent
$sql = "UPDATE email_queue SET " .
"attempts = attempts+1, " .
"success = '1', " .
"last_attempt = now(), " .
"date_sent = now() " .
"WHERE id = '" . $queued_mail['id'] . "'";
mysql_query($sql);
echo("Message successfully sent!");
}
} // end while (loop through records and sending emails)
} // no rows so quit
} // end of method ProcessQueue()