可能是重复问题:
使用PHP发送大批量电子邮件
我有一个PHP脚本,可以向我的数据库中的所有用户发送单独的电子邮件,例如月度/周讯。
我正在使用以下代码:
$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
}
这段代码在一个非常小的数据库中运行得很好... 我最近用20万用户填充了我的测试数据库,显然这个脚本失败了,内存溢出并且崩溃了...
我知道这是发送如此多电子邮件的一种糟糕方式,因此我想请教您更高效的方法!
非常感谢!