Forum Moderators: coopster

Message Too Old, No Replies

processing email queue with cron

processing email queue with cron

         

smithydude

3:04 pm on Apr 9, 2009 (gmt 0)

10+ Year Member



I email members (yes.... only those who opt in) about new posts on the site. Entries are made into an email queue table, and can then be sent (99 at a time) using a process_queue.php script.

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.

smithydude

3:07 pm on Apr 9, 2009 (gmt 0)

10+ Year Member



Here is the process_queue() method:

//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()

smithydude

1:00 pm on Apr 10, 2009 (gmt 0)

10+ Year Member



10$ cash prise for anyone who solves it :-)

coopster

3:35 pm on Apr 10, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I notice you are echoing output. This may help you solve your issue:
echo( $mail->getMessage() );

Where is your crontab sending script output? dev null? send it to a file instead perhaps.

smithydude

12:31 pm on Apr 17, 2009 (gmt 0)

10+ Year Member



YES! You solved it!