0

I have more than 10K email id is my database. I am trying to send them email with php mailer. Problem is my hosting provider allow 60 emails per minute.

So, i am thinking i need to change my email code to limit 60 email per minute.

Here is the code i have written so far.

$result = $mysqli->prepare("SELECT uid,email FROM users where address IS NULL and city IS NULL");
            $result->execute();
            $result->store_result();
            $result->bind_result($uid,$email);
            while ($result->fetch()){
                   $email = $email;
                   --Code to send email--

                }

I thought to put sleep command in for loop but i read somewhere that it is bad practice.

I checked this question PHP sleep delay

and found we can use AJAX for the same.

You could use Ajax. And use a timeout to call your PHP script every few seconds. This will avoid the slow script loading. And also you can keep doing it constantly (the current for loop will only run for 33 seconds and then stop).

Is there any advise how can i implement AJAX or any other way to achieve this.

Community
  • 1
  • 1
Roxx
  • 3,738
  • 20
  • 92
  • 155
  • instead to use sleep command i suggest to use cron job which will be run every minute and you script will have send only 60 emails in script that you can take one flag in database for to know mail sent or not (you can take one database field is_mail_sent. 0 OR 1, 0=not sent, 1= sent). and while email sent from the script you can update that flag. – Ashok Chandrapal Aug 06 '16 at 05:02
  • Email sending is on demand. I don't have to send them regularly its once in a week. – Roxx Aug 06 '16 at 05:04
  • then you can set cron job once in a week also. – Ashok Chandrapal Aug 06 '16 at 05:04
  • But if i set cron job once a week then it will send all the emails at once. I need to limit that 60 emails per minute. I checked the cron job i can set once in a week but how can i limit that to 60 emails. – Roxx Aug 06 '16 at 05:09
  • You can follow below steps which i mentioned in answer – Ashok Chandrapal Aug 06 '16 at 05:22

1 Answers1

1

You can follow below steps

Step 1: You can make one table called "setting" : that will have one column called "last_email_sent_id"

Step 2: you need to set limit in your sql query with 60 fix and primary_id > "last_email_sent_id" column from setting table.

Step 3: This "last_email_sent_id" will be update every time while you cron job will be run. like below

Step 4: if you send first 60 email then it will be update "last_email_sent_id" to "60". then again cron will run and as per "step 2" "last_email_sent_id" column will be updated with value 120 (last value of primary id which table you using to store email id) after cron complete. if the primary id of table will be last record then "last_email_sent_id" update to "0" value.

Step 5: cron will be set every 1 minute on last day of week.

Ashok Chandrapal
  • 1,020
  • 7
  • 27