2016-11-09 75 views
3

我有一個求職網站(CI),可以有一定數量的求職者。我必須做的是根據用戶的工作類別和地點發送求職信息。所以對於不同的求職者有不同的信息。 我使用的PHPMailer發送電子郵件,現在我已經做了如何使用phpMailer發送x個電子郵件?

$subject = 'Revalent Jobs For Your Profile'; 
foreach ($job_receiving_users as $job_receiving_user){ 
    $this->send_email->send_email(FROM_NOREPLY_EMAIL,ORG_NAME,$job_receiving_user['email'],$job_receiving_user['username'],$subject,$job_receiving_user['message']); 
     $time = time() + 10; 
     while(time() < $time){ 
     // do nothing, just wait for 10 seconds to elapse 
     } 
    } 

(有PHPMailer的電子郵件發送庫SEND_EMAIL內法)

有一個從服務器每小時200電子郵件的限制,也可以將其擴展到500. 我想知道的是發送電子郵件的好方法嗎? 如果我保持每個電子郵件之間10secod的差距,它會讓我的服務器忙。所有的sql操作都在此代碼之上完成,並且$job_receiving_users是上面提取的用戶電子郵件,消息和用戶名的數組。

回答

2

基地您的the mailing list example provided with PHPMailer

你在你的循環做什麼代碼被稱爲「忙等待」;不要這樣做。 PHP有幾個sleep functions;改用它們。例如:

$sendrate = 200; //Messages per hour 
$delay = 1/($sendrate/3600) * 1000000; //Microseconds per message 
foreach ($job_receiving_users as $job_receiving_user) { 
    //$this->send_email->send_email(FROM_NOREPLY_EMAIL,ORG_NAME,$job_receiving_user['email'],$job_receiving_user['username'],$subject,$job_receiving_user['message']); 
    usleep($delay); 
} 

這將導致它來發送消息每18秒(200 /小時),使用睡眠功能將意味着它幾乎不消耗CPU,而它的等待。