-3

I have a users table in my database. There have 50,000 above email addresses. I want to send email to all at a time. I am using while loop. But my browser hang during send mail. How to send email to all users?

Code:

$qryUserEmail = mysql_query("SELECT username,email FROM users WHERE is_active=1");
if(mysql_num_rows($qryUserEmail)){ //I got 50624 email addresses.
  while($getUserEmail = mysql_fetch_array($qryUserEmail)){
      //Sending Email code Here...
  }
}

I don't know about background-process code. If anybody know please post your answer here. How to send email without taking any time for my related code?

Developer
  • 2,676
  • 8
  • 43
  • 65

2 Answers2

1

Php should be able to start background processes, but I don't know much about that.

I would suggest that you introduce some kind of a queue and background worker that can send the email without direct interaction from the user.

So when the user clicks 'send', a job is added to the queue, and once in a while, queued jobs are handled (like cron job or similar)

Another alternative is to pay for a service like send grid or mailchimp.

Vegar
  • 12,828
  • 16
  • 85
  • 151
  • How to set mail in queue? – Developer May 25 '15 at 18:29
  • @chatfun have a new table, the queue, where you add a record about the news you wanna send. The key the background worker read that table and do the actual sending, just like you do now. – Vegar May 25 '15 at 18:31
  • I don't want to use any third party api. – Developer May 26 '15 at 05:32
  • Vegar I have updated my code. Please check. I don't need any 3rd party. I want to write code in my own script. – Developer May 26 '15 at 05:54
  • Using 3rd party code is one of the best way of coding. Not copy-paste but using libraries maintained and tested by many others. – Vegar May 26 '15 at 05:59
  • https://www.developphp.com/video/PHP/Cron-Job-Tutorial-Automate-the-Email-Sending-for-Newsletter http://stackoverflow.com/questions/3368307/how-to-send-emails-via-cron-job-usng-php-mysql https://florian.ec/articles/running-background-processes-in-php/ – Vegar May 26 '15 at 06:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78745/discussion-between-chatfun-and-vegar). – Developer May 26 '15 at 06:25
0
  1. use set_time_limit() and ini_set('max_execution_time') to increase the time of execution.

  2. You can execute .PHP file from shell script (or something like crontab) or browser.

    It's not necessary to use callback function or something else. Post your email send in your while or use CC and BCC.

Community
  • 1
  • 1
Kristiyan
  • 1,655
  • 14
  • 17