Page 1 of 1

Duplicates sending

Posted: Mon Oct 08, 2007 10:51 pm
by rezur
I'm coding a permanent cron loop to send all the emails continuously but I got a problem. Sometimes it sends 2,3 or 4 emails to the same person, randomly.

I'm using PHP 5 and version 3.3.1

Here's the main loop code :

Code: Select all

while($stop==false) {
    $sql = "select uniqueid,useremail,mailid from newsletter where tosend=1 and sent=0 order by mailid limit $nbmails";
    $res = $db->doSelect($sql);
    $swift =& new Swift(new Swift_Connection_SMTP("localhost"));
    while($row = $db->getNextRow()) {
        $mail->get($row[mailid]);
        $mess = stripslashes(str_ireplace("{usernum}",$row[uniqueid],$mail->message));
        try {
            $message =& new Swift_Message($mail->subject, $mess,"text/html");
            if($swift->send($message,$row[useremail],"***@***.com")) {
                $sql = "update newsletter set sent=1,tosend=0 where uniqueid=".$row[uniqueid];
            } else {
                $sql = "update newsletter set sent=1,tosend=0,bounce=1 where uniqueid=".$row[uniqueid];
            }
            $db->doUpdate($sql);
        } catch (Swift_ConnectionException $e) {
            $logs->add($e->getMessage(),$row[mailid]);
        } catch (Swift_Message_MimeException $e) {
            $logs->add($e->getMessage(),$row[mailid]);
        }
    }
    $swift->disconnect();
    sleep(20);
  }
As I update every email flag everytime I don't understand how I could send many times the same email (knowing that there's no duplicates in newsletter table).

Thanks for your help.

Posted: Mon Oct 08, 2007 10:58 pm
by feyd
I see you're developing with E_NOTICE error level off. Maybe there's something to find from turning it on?

Posted: Mon Oct 08, 2007 11:07 pm
by rezur
E_Notice level for error reporting is active, you just don't see the declaration part of my code. And nothing on the console reporting any problem.

Posted: Tue Oct 09, 2007 7:21 am
by Chris Corbyn
rezur wrote:E_Notice level for error reporting is active, you just don't see the declaration part of my code. And nothing on the console reporting any problem.
E_NOTICE cannot be on, unless you've got some use of lowercase constants in that script. Either that or display_errors is off. You've got some unquoted strings in your array indices which *will* generate notices.

Back to your original problem, how are you running this script? Is it possible it's running more than once at the same time? Also, have you double checked the rows returned from the query?

Posted: Tue Oct 09, 2007 11:57 pm
by rezur
Only messages from Enotice is the quoted row names (which is fixed atm).
The script is a linux process that could run only once.
And the row are ok.
Could it be due to some kind of smtp timeout that blocks the database update, letting the mail somewhere btw my server and the mail server, and after 20 sec the while loop is executed once more ?