我有發送個人電子郵件到我的數據庫,所有用戶一個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: [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
}
此代碼的工作完全與一個非常小的數據庫......我最近填充我的測試分貝200K +的用戶,顯然這個腳本失敗,失控的內存和死亡...
我知道這是一個不好的方式發送這麼多的電子郵件,這就是爲什麼我想問你更有效的方式來做到這一點!
非常感謝!
要做的第一件事:溝渠郵件()就像它有毒的垃圾一樣,然後切換到一個真正的郵件包,例如, [Swiftmailer](http://swiftmailer.org)或[PHPMailer](http://phpmailer.worxware.com) –