Batch 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
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Batch sending

Post by rsmarsha »

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:

Code: Select all

<?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 10:58 am, edited 2 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

http://swiftmailer.org/wikidocs/v3/send ... =batchsend

Specifically the part about using the Swift_BatchMailer object instead of $swift->batchSend( .. )
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

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.

Code: Select all

//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();
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Please post the entire code so we can see exactly whats going on.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

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:

Code: Select all

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

Post by Chris Corbyn »

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 :? ;)
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

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