Database driven batch mailing list

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
SeekerToo
Forum Newbie
Posts: 1
Joined: Tue Nov 06, 2007 4:41 pm

Database driven batch mailing list

Post by SeekerToo »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm trying to make a batch mailing list program using Swift Mailer based on a MySQL database email list containing around 800 names. Included below is the code I am working on and it contains two approaches to sending the list; the commented out code trying to use the recipients -> addto method and the more straight forward loop which is what I am currently using. I am running a cron event which is pulling out email addresses in groups of 30.

More in keeping with the spirit of the Swift Mailer application, and more elegant, I would rather use the recipient list approach, but the only success I have had is with the other kludge. The only problem with this is that the program seems to get hung up once in a while on a particular email address and each mailing process must be babysat in order to finish it. This is not good! At the minimum, I need to gain some information from Swift that will allow bypassing an address that hangs the program.

Here is the important part of the code. Any suggestions are greatly appeciated. We are in desperate need of an in-depth mailing list tutorial. If you want to help me but are unsure of something in my question, please contact me!

Code: Select all

require_once "../swift/Swift.php";
		require_once "../swift/Swift/Connection/SMTP.php";
		 
		//Start Swift
		$swift =& new Swift(new Swift_Connection_SMTP("localhost"));
		//$swift->log->enable();
		 
		//Create the message
		$message =& new Swift_Message($subject);
		
		// add attachment
		$afile =& new Swift_File($image);
		$attachment =& new Swift_Message_Attachment($afile);
		$message->attach($attachment);
		
		// add embedded image
		$img =& new Swift_Message_Image(new Swift_File("../images/emailimage.jpg"));
		$src = $message->attach($img);
		
		// add message body
		$body_h =& new Swift_Message_Part("<br />
		<img src=\"" . $src . "\" /><br />
		" . $body_html, "text/html");
		 
		$message->attach($body_h);
		
		//Add some "parts" for plain text and html
		$message->attach(new Swift_Message_Part($body));
		$message->attach(new Swift_Message_Part($body_h, "text/html"));

                               
		// here is the code I am using in place of the below commented out code
		for($i=0;$i<$recipctr;$i++) {
		   $swift->send($message, $addressarray[$i], $propmailfrom);
		   sleep(1); // probably does nothing useful
                                   mysql_query("update mailinglist set resetflag = '1' where mailid = '$mailidarray[$i]'"); 
		}

		
                                /* this is the code not being used whcih employs the batch method
                                
                                 //setup recipients list x at a time
		$recipients =& new Swift_RecipientList();

                                for($i=0;$i<$recipctr;$i++) {
		   $recipients->addTo($addressarray[$i]);
		}
		
		$batch =& new Swift_BatchMailer($swift);
		$batch->setMaxTries(5);
		$batch->setMaxSuccessiveFailures(2);
		$batch->setSleepTime(10); //Sleep for 10 seconds if an error occurs
		
		$batch->send($message, $recipients, new Swift_Address($propmailfrom, "All French Services"));
		*/
		  
		//It's polite to do this when you're finished
		$swift->disconnect();



[/syntax]


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply