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: info@domain.com"."\n";
$headers .= "Reply-To: bounce@domain.com"."\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!
See this Url:–
Sending mass email using PHP
First off, using the mail() function that comes with PHP is not an optimal solution. It is easily marked as spammed, and you need to set up header to ensure that you are sending HTML emails correctly. As for whether the code snippet will work, it would, but I doubt you will get HTML code inside it correctly without specifying extra headers
I’ll suggest you take a look at SwiftMailer, which has HTML support, support for different mime types and SMTP authentication (which is less likely to mark your mail as spam).