How to update to each mail on batchSend

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
YvesTan
Forum Newbie
Posts: 8
Joined: Thu Mar 06, 2008 1:54 pm

How to update to each mail on batchSend

Post by YvesTan »

Hello,

How it's possible to update user row (exemple : on sql table) after send if I use batchSend ?

thanks !
YvesTan
Forum Newbie
Posts: 8
Joined: Thu Mar 06, 2008 1:54 pm

Re: How to update to each mail on batchSend

Post by YvesTan »

It's me too ;)

I've find ! I've use VerbosePlugin to update each user... But if I don't want use batchSend, what do you think about my code :

Code: Select all

 
<?php
 
  // instancie l'objet et la connexion via sendmail
    $swift = new Swift(new Swift_Connection_Sendmail(Swift_Connection_Sendmail::AUTO_DETECT));
 
    // composition du message
    $message =& new Swift_Message($subject); // instanciation et sujet
    $message->setReturnPath($sender_return); // adresse de retour
    $message->attach(new Swift_Message_Part($msg_text.$file_name)); // alternate
    $message->attach(new Swift_Message_Part($msg_html, "text/html")); // html
 
    // si ce n'est pas un test
    if (!$testsend) {
        
        // all 30 000 users
        $res = $db->query($sql_send);
 
        // incrementation
        $i = 0;
 
        // statement pour l'update
        $idvalue = null;
        $sta = $db->prepare("UPDATE ".$table_users." SET ".$field_send."=1 WHERE ".$field_id."=?");
        $sta->bindParam(1, $idvalue);
    
        //  boucle
        while($row = $res->fetch()) {
 
            $email = $row[$field_email];
            $email = strtolower($email);
            
            // la pause (deconnection/reconnection)
            if (($i % $per_send) == 0 && $i>0) {
 
                // on deconnecte avant la pause
                $swift->disconnect();
                
                if (!$silent) {
                    echo "==============================> Pause au niveau ".$i."\n";
                }
 
                for($s=0;$s<$pause_time;$s++) {
                    if(!$silent) {
                        echo ".";
                    }
                    sleep(1);
                }
 
                if(!$silent) {
                    echo "\n";
                }
 
                // on reconnecte
                $swift->connect();
                
            }
                
            // on envoi
            $swift->send($message, new Swift_Address($email), new Swift_Address($sender, $sender_name));
 
            // on mets a jour le flag "envoye"
            $idvalue = $row[$field_id];
            $sta->execute();
 
            if (!$silent) {
                echo "-> Envoi en cours a ".$email." ".$row[$field_id]."\n";
            }
 
            $i++;
 
        }
 
        // fin de l'envoi
        echo "=======> Envoi termine !\n";
 
    }
?>
 
$per_send = 100 and $pause = 20;

Do you think it's OK (optimize ...) ?

thanks a lot !
YvesTan
Forum Newbie
Posts: 8
Joined: Thu Mar 06, 2008 1:54 pm

Re: How to update to each mail on batchSend

Post by YvesTan »

I wan't to try this way on my 30 000 users but can you confirm :

- it's possible to open connection with sendmail for more than one email or I should put SendMail connection on the loop ?
- this way send email without revealing all addresses

thanks a lot to your help !
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: How to update to each mail on batchSend

Post by Chris Corbyn »

You should use the AntiFlood plugin to handle the sleep()/disconnect() stuff. It doesn't only work with batchSend(), it works whichever way you send.

The Sendmail connection does persist yes, putting the connection stuff inside the loop would make it slower.

There's a handy plugin coming in v4 called the ReporterPlugin... it works by sending pass/fail notifications for every single address to an observer you implement yourself, or to one of the pre-written ones I've provided.
YvesTan
Forum Newbie
Posts: 8
Joined: Thu Mar 06, 2008 1:54 pm

Re: How to update to each mail on batchSend

Post by YvesTan »

Chris Corbyn wrote:You should use the AntiFlood plugin to handle the sleep()/disconnect() stuff. It doesn't only work with batchSend(), it works whichever way you send.
OK ! I thinked it's worked only with batchSend() !

Antiflood plugin work like my script... I think ;)
The Sendmail connection does persist yes, putting the connection stuff inside the loop would make it slower.

There's a handy plugin coming in v4 called the ReporterPlugin... it works by sending pass/fail notifications for every single address to an observer you implement yourself, or to one of the pre-written ones I've provided.
When I read this forum, I think lot's people waiting for this plugin ;)

thank you very much !
Post Reply