Possible Duplicate:
Sending mass email using PHP
I have a PHP script that sends an individual email to all users in my DB, such as a monthly / weekly newsletter.
The code I am using goes as follows:
$subject = $_POST['subject'];
$message = $_POST['message'];
// Get all the mailing list subscribers.
$query = $db->prepare("SELECT * FROM maildb");
$query->execute();
// Loop through all susbcribers, and
send and individual email.
foreach ($query as $row) {
// Setting maximum time limit to infinite.
set_time_limit(0);
$newMessage = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>';
// Search for the [unsubscribe] tag and replace it with a URL for the user to unsubscribe
$newMessage .= str_replace("[unsubscribe]", "<a href='".BASE_URL."unsubscribe/".$row['hash']."/".$row['email']."'>unsubscribe</a>", $message);
$newMessage .= '</body></html>';
$to = $row['email'];
// Establish content headers
$headers = "From:
[email protected]"."\n";
$headers .= "Reply-To:
[email protected]"."\n";
$headers .= "X-Mailer: PHP v.". phpversion()."\n";
$headers .= "MIME-Version: 1.0"."\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1"."\n";
$headers .= "Content-Transfer-Encoding: 8bit;";
mail($to, $subject, $newMessage, $headers); //
Send email to each individual user
}
This code works perfectly with a REALLY small database... I recently populated my test db with 200k+ users, and obviously this script fails, gets out
of memory, and dies...
I know this is a bad way to
send so many emails, thats why I'd like to ask you for much more efficient ways to do this!
Thank you very much!