best way to email batches? [SOLVED]

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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

best way to email batches? [SOLVED]

Post 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!
Last edited by s.dot on Sat Dec 29, 2007 10:07 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

D'OH!

Swift anti-flood plugin, for the win.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

scottayy wrote:D'OH!

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