2012-09-02 138 views
4

可能重複:
Sending mass email using PHPPHP發送批量電子郵件

我有發送個人電子郵件到我的數據庫,所有用戶一個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 +的用戶,顯然這個腳本失敗,失控的內存和死亡...

我知道這是一個不好的方式發送這麼多的電子郵件,這就是爲什麼我想問你更有效的方式來做到這一點!

非常感謝!

+1

要做的第一件事:溝渠郵件()就像它有毒的垃圾一樣,然後切換到一個真正的郵件包,例如, [Swiftmailer](http://swiftmailer.org)或[PHPMailer](http://phpmailer.worxware.com) –

回答

0

見以下鏈接: -

Sending mass email using PHP

首先,使用PHP附帶的mail()函數不是最佳解決方案。它很容易被標記爲垃圾郵件,您需要設置標題以確保您正確發送HTML電子郵件。至於代碼片段是否可以正常工作,但是我懷疑你會正確得到它的HTML代碼,而不是specifying extra headers

我建議你看看SwiftMailer,它支持HTML,支持不同的MIME類型和SMTP身份驗證(不太可能將您的郵件標記爲垃圾郵件)。

+0

看到這個URL它對你來說非常有用http://stackoverflow.com/questions/1118154/sending-mass-email-using- php –

+0

另請參閱http://stackoverflow.com/questions/5179168/php-sending-huge-quantity-of-emails-in-batch –

+0

感謝您的回答,我會閱讀文章。 SwiftMailer也被卡住了......最大執行時間30秒超出/home/content/lib/classes/Swift/Mime/Headers/MailboxHeader.php第307行 – qalbiol

2

您遇到的超時是由於Apache和PHP的執行限制。

您需要與set_time_limit(0);

PHP /path/to/app/script.php像這樣直接在控制檯中運行它作爲一個CLI應用程序。

如果你沒有SSH訪問,然後用shell_exec像這樣運行:

shell_exec("php /path/to/app/script.php > /dev/null 2>/dev/null &"); 

這將確保調用,直到它完成它不流連腳本。

1

您可以設置一個Cron調度程序來運行每分鐘或某個時間間隔。腳本的每次執行都會從數據庫中選取幾條記錄,並從數據庫中刪除它們或將其設​​置爲不活動。發送郵件到一個小塊,讓腳本死掉。另一個cron調用將會選擇其他幾條記錄並死亡。你也可以利用exec()。

1

分批執行這些操作,因此您一次只能發送一些數據,在數據庫中有一個字段用於檢查是否已發送該月的通訊,並在通訊發送給該用戶,那麼你可以繼續運行腳本,直到它被髮送給每個人。

0

我寧願implode在一個變量,並且發送一個電子郵件用戶去

$to_array = array(); 

foreach ($query as $row) { 

    $to_array[] = $row['email']; 

} 

$to = implode(', ', $to_array); 

//do your email stuff here 

mail($to, $subject, $newMessage, $headers); // Send email to all users at once 

希望這有助於:-)

+1

如果您遇到執行時間問題,請添加此位代碼。 'ini_set(' max_execution_time',3000);'在這種情況下,3000是幾秒鐘,即5分鐘 – mithilatw