Duplicates sending

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
rezur
Forum Newbie
Posts: 3
Joined: Mon Oct 08, 2007 10:47 pm

Duplicates sending

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I see you're developing with E_NOTICE error level off. Maybe there's something to find from turning it on?
rezur
Forum Newbie
Posts: 3
Joined: Mon Oct 08, 2007 10:47 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
rezur
Forum Newbie
Posts: 3
Joined: Mon Oct 08, 2007 10:47 pm

Post 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 ?
Post Reply