2016-11-09 73 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,而它的等待。