Page 1 of 1

Batch sending

Posted: Wed Nov 28, 2007 8:44 am
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? :)

Posted: Wed Nov 28, 2007 10:35 am
by John Cartwright
http://swiftmailer.org/wikidocs/v3/send ... =batchsend

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

Posted: Wed Nov 28, 2007 10:51 am
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();

Posted: Wed Nov 28, 2007 11:09 am
by John Cartwright
Please post the entire code so we can see exactly whats going on.

Posted: Wed Nov 28, 2007 11:14 am
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.

Posted: Wed Nov 28, 2007 8:46 pm
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 :? ;)

Posted: Thu Nov 29, 2007 4:41 am
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?