如何使用 phpMailer 发送 x 封邮件?

How to send x number of email using phpMailer?

我有一个工作站点(在 CI 中),可以有 x 个 jobseekers.What 我必须做的是根据用户工作类别发送相关工作,然后 location.So对于不同的求职者来说是不同的信息。 我正在使用 phpMailer 发送电子邮件,现在我已经完成了

$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 封。 我想知道的是这种发送电子邮件的好方法吗? 如果我在每封电子邮件之间保持 10 秒的间隔,它将使我的服务器 busy.All sql 操作在此代码之上完成并且 $job_receiving_users 是上面提取的用户电子邮件、消息和用户名的数组。

您的代码基于 the mailing list example provided with PHPMailer

你在循环中所做的事情叫做 "busy waiting";不要这样做。 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。