tags:

views:

16

answers:

1

I need to be able to send emails to contacts stored in a database(its for newsletter not spam :P). I'm able to do this using mail() and a loop, but I've read that this isn't a very good idea as there could be a few hundred contacts.

What is the best way to go about this? Any advice or pointers in the right direction will be greatly appreciated!

Thanks.

+1  A: 

mail() will be very slow with several hundred contacts. I recommend swiftmailer at http://www.swiftmailer.org. Here's an example of sending many mails from thier site:

require_once 'lib/swift_required.php';

//Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('[email protected]' => 'John Doe'))
  ->setTo(array('[email protected]', '[email protected]' => 'A name'))
  ->setBody('Here is the message itself')
  ;

//Send the message
$numSent = $mailer->batchSend($message);

And you can use your SMTP connection/account to send, or sendmail.

Erik