Page 1 of 1

best way to email batches? [SOLVED]

Posted: Sat Dec 29, 2007 9:36 pm
by s.dot
I have this code:

Code: Select all

#EMAILING ALL MEMBERS FROM admin_email-all.php
		case 'doEmailAll':
		
			if (empty($_POST['emailSubject']) || empty($_POST['emailBody']))
			{
				die('Please provide an email body AND a subject');
			}
			
			$subject = stripslashes($_POST['emailSubject']);
			$body = stripslashes($_POST['emailBody']);
			
			//load in the components
			require_once $_SERVER['DOCUMENT_ROOT'] . '/lib/Swift.php';
			require_once $_SERVER['DOCUMENT_ROOT'] . '/lib/Swift/Connection/SMTP.php';
			require_once $_SERVER['DOCUMENT_ROOT'] . '/lib/Swift/Plugin/Decorator.php';

			//load the SMTP connection and authenticate
			$smtp = new Swift_Connection_SMTP('smtp.1and1.com', 25);
			$smtp->setUsername('me@domain.tld');
			$smtp->setpassword('password');

			//start up swift with our SMTP connection
			$swift = new Swift($smtp);
			
			//email message with subject only
			$message = new Swift_Message($subject);
			
			//add our components
			$message->attach(new Swift_Message_Part(strip_tags($body)));
			$message->attach(new Swift_Message_Part($body, 'text/html')); 
			
			//recipient list
			$recipients = new Swift_RecipientList();
			
			//replacements array
			$replacements = array();
			
			//grab our email list, loop through adding to recipients and populating the replacements array
			$r = mysql_query("SELECT `email`, `username` FROM `users` WHERE `emailGeneral` = 1") or die(mysql_error());
			while ($a = mysql_fetch_assoc($r))
			{
				$recipients->addTo($a['email']);
				$replacements[$a['email']] = array('{user}' => $a['username']);
			}
			
			//Load the plugin with these replacements
			$swift->attachPlugin(new Swift_Plugin_Decorator($replacements), 'decorator');
			
			//send
			$numSent = $swift->batchSend($message, $recipients, 'me@domain.tld');
			
			echo $numSent . ' emails sent out of ' . mysql_num_rows($r) . ' possible!';
		
		break;
However my smtp server only allows me to send 99 emails at once. Should I array_chunk() the recipients list to 99 element chunks and loop and batchSend() inside of that loop?

If I did, how would Swift handle the full array of $replacements even though it won't be using all of them? Is there a better way to do it?

I know the easiest way is to test, but this thing is live!

Posted: Sat Dec 29, 2007 10:07 pm
by s.dot
D'OH!

Swift anti-flood plugin, for the win.

Posted: Sat Dec 29, 2007 10:17 pm
by John Cartwright
scottayy wrote:D'OH!

Swift anti-flood plugin, for the win.
scottayy - 0, Swift - 1