salin kunwar
salin kunwar

Reputation: 1188

How to send x number of email using phpMailer?

I have a job site (in CI) and there can be x number of jobseekers.What i have to do is send revalent jobs according to users job category and location.So there is different message for different jobseeker. i am using phpMailer to send email for now i have done

$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
       }
     }

(There is phpMailer email sending method inside library send_email)

There is limit of 200 email per hour from server or can extend it to 500. What i want to know is this good way to send email? if i keep 10secod gap between every email will it keep my server busy.All sql actions were done above this code and $job_receiving_users is array of user email,message and username extracted above.

Upvotes: 3

Views: 497

Answers (1)

Synchro
Synchro

Reputation: 37710

Base your code on the mailing list example provided with PHPMailer

What you're doing in your loop is called "busy waiting"; don't do it. PHP has several sleep functions; use them instead. For example:

$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);
}

This will cause it to send a message every 18 seconds (200/hour), and the use of the sleep function will mean it consumes almost no CPU while it's waiting.

Upvotes: 2

Related Questions