PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Wed Aug 12, 2020 12:34 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Batch sending
PostPosted: Wed Nov 28, 2007 9:44 am 
Offline
Forum Contributor

Joined: Tue Feb 08, 2005 5:06 am
Posts: 242
Location: Leeds, England
I've followed the instructions for batch sending (with replacements), from the swiftmailer wiki.

The following code is setup to use 2 test emails for the moment:

Syntax: [ Download ] [ Hide ]
<?php

//CREATE AND SEND EMAILS //



//Start Swift

$swift =& new Swift(new Swift_Connection_SMTP("127.0.0.1"));

//Set message subject

$message =& new Swift_Message($subject);



$message->attach(new Swift_Message_Part('<html><body>'.$mail_body.'</body></html>', 'text/html'));



$recipients =& new Swift_RecipientList();

$replacements = array();

if ($_POST['xMode']=='1')

        {

                $whereClause = "(email='testa@testing.com' OR email='testb@testing.com')";

        }

        else

        {

                $whereClause = "(email='test1@testing.com' OR email='test2@testing.com')";

        }

$customers = "SELECT email,forename,surname FROM customers WHERE ".$whereClause."";

$cq = mysql_query($customers) or die("Query Customers: $customers Failed".mysql_error());

while ($cr = mysql_fetch_assoc($cq))

{

        //Specify the list of replacements as a 2-d array

        $replacements[$cr['email']] = array("{forename}" => $cr['forename'], "{surname}" => $cr['surname']);

         

        //Load the plugin with these replacements

        $swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");



        $recipients->addTo($cr['email']);

}



//Send messages

$swift->batchSend($message, $recipients, "from@testing.com");



$swift->disconnect();

?>


Ignore the test mode part, that makes use of an option on the previous form.

My question is how to you control how many mails swift sends per batch? Or does it send them all in one go?

Also does the above look right? :)


Last edited by rsmarsha on Wed Nov 28, 2007 11:58 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 11:35 am 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
http://swiftmailer.org/wikidocs/v3/send ... =batchsend

Specifically the part about using the Swift_BatchMailer object instead of $swift->batchSend( .. )


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 11:51 am 
Offline
Forum Contributor

Joined: Tue Feb 08, 2005 5:06 am
Posts: 242
Location: Leeds, England
It's been a long day, was looking on the swift site for that very page. :)

Thanks for the heads up.


*edit*

When sending using the following code, I don't seem to get any mails through.

Syntax: [ Download ] [ Hide ]
//Send messages

//$swift->batchSend($message, $recipients, $from);

$batch =& new Swift_BatchMailer($swift);

$batch->setMaxTries(2);

$batch->setMaxSuccessiveFailures(10);

$batch->setSleepTime(10); //Sleep for 10 seconds if an error occurs

$batch->send($message, $recipients, $from);

print_r($batch->getFailedRecipients());

$swift->disconnect();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 12:09 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Please post the entire code so we can see exactly whats going on.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 12:14 pm 
Offline
Forum Contributor

Joined: Tue Feb 08, 2005 5:06 am
Posts: 242
Location: Leeds, England
Sorry didn't want to duplicate what i'd posted earlier as only the end part has changed.

The section of code which involves swiftmailer is:

Syntax: [ Download ] [ Hide ]
<?php

//CREATE AND SEND EMAILS //



//Start Swift

$swift =& new Swift(new Swift_Connection_SMTP("127.0.0.1"));

//Set message subject

$message =& new Swift_Message($subject);



$message->attach(new Swift_Message_Part('<html><body>'.$mail_body.'</body></html>', 'text/html'));



$recipients =& new Swift_RecipientList();

$replacements = array();



while ($cr = mysql_fetch_assoc($cq))

{

        //Specify the list of replacements as a 2-d array

        $replacements[$cr['email']] = array("{forename}" => $cr['forename'], "{surname}" => $cr['surname']);

         

        $recipients->addTo($cr['email']);

}



//Load the plugin with these replacements

$swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");



//Send messages

//$swift->batchSend($message, $recipients, $from);

$batch =& new Swift_BatchMailer($swift);

$batch->send($message, $recipients, $from);

$batch->setMaxTries(2);

$batch->setMaxSuccessiveFailures(10);

$batch->setSleepTime(10); //Sleep for 10 seconds if an error occurs

print_r($batch->getFailedRecipients());

$swift->disconnect();

?>


As i said, it works fine if i remove the $batch sections and uncomment $swift->batchSend.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 9:46 pm 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
It should work fine, although I'm not sure why you're setting the max retries etc for the batch *after* you've sent the email :? ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 5:41 am 
Offline
Forum Contributor

Joined: Tue Feb 08, 2005 5:06 am
Posts: 242
Location: Leeds, England
Yeah i've changed that since. :oops:

It's an odd one. Works fine using batchSend but not batchMailer.

All i'm changing is the bottom part as you can see from the above code.


It's a great class though, use it for 3 mailing scripts at the moment. I think i'm on an older version 3.1.3.

Is it worth updating to the latest version (as in will it effect current scripts), or wait until v4?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group