2014-01-30 90 views
-3

我需要向8000個訂閱者發送新聞稿,但我的主機只支持每小時發送100封郵件。我需要一個執行此項工作的PHP腳本,每小時發送超過100封電子郵件的8000封電子郵件,如果可能的話使用cronjobs,而不必在腳本運行時保持瀏覽器打開狀態。如何用PHP腳本和cronjobs大量發送電子郵件

感謝

+1

您準備爲程序員支付多少錢?你爲什麼不把錢花在電子郵件限制較高的服務器上,或者使用像MailChimp這樣的服務(或者沒有品牌的替代品) –

+0

因爲我沒有錢。郵件數量從未超過8000. –

+0

您應該查看[PHPList](http://www.phplist.com/)。這是一個開源的電子郵件平臺,可能會讓你的生活更輕鬆。還有很多事情需要考慮,比如ESP-ESPAM通常幫助/處理的CAN-SPAM,產能和壓制列表。 – John

回答

0

你應該有一個數據庫表,這些列:

=============== 
Suscribers Table 
=============== 
|id (int)|email (varchar)|sent (tinyint) 
|1|[email protected]|0 
|2|[email protected]|0 
|3|[email protected]|0 

那麼像這樣的PHP腳本:

// DB Connection 
require_once 'db.php'; 

// Check if we have users with a 0 sent value 
$query = mysql_query("SELECT COUNT(id) FROM suscribers WHERE sent = 0"); 
$results = mysql_num_rows($query); 

// If there's one or more suscribers with a 0 sent value 
if ($results >= 1) { 
    // Initialize and require Swift or any other email library for PHP 
    require_once 'swift/lib/swift_required.php'; 
    $transport = Swift_SmtpTransport::newInstance('mail.domain.com', 587) 
     ->setUsername('[email protected]') 
     ->setPassword('password'); 
    $mailer = Swift_Mailer::newInstance($transport); 

    // Body of the email 
    $body = '<html><head></head><body>Hello suscriber!</body></html>' 

    // Message parameters 
    $message = Swift_Message::newInstance(); 
    $message->setSubject('Newsletter'); 
    $message->setFrom(array('[email protected]' => 'Domain Newsletter')); 
    $message->setSender('[email protected]'); 
    $message->setBody($body, 'text/html'); 

    // Use a query to get only 100 suscribers from the table who have a 0 sent value 
    $query = mysql_query("SELECT id, email FROM suscribers WHERE sent = 0 LIMIT 100"); 
    while ($data = mysql_fetch_array($query)) { 
     $idEmail = $data['id']; 
     $message->setTo($data['email']); 

     // Update the email sender ID "sent" value to "1" 
     mysql_query("UPDATE suscribers SET sent = 1 WHERE id = $idEmail"); 
     $mailer->send($message); 
    } 
} 

最後使用cron作業像這樣的一個點到您的PHP cron文件:

/usr/bin/php -q /home/domain/public_html/newsletter_cron.php