I have a forum, where users can subscribe to a particular thread. When someone replies to the thread, the reply is added to the DB and an email alert is sent to all the subscribers before the success page is displayed.
FYI – I am not using PHP’s mail function, as my hosting provider has a limit of 500 per hour. I am using Google App Engine to send out the emails via curl.
The problem – when the number of subscribers is more than a hundred or so, the user has to wait for way too long before the success page is displayed, because PHP has to loop through each subscriber first. Is there a way to circumvent this without resorting to inserting the emails in the DB and then processing them via a cron?
Hope I’ve made sense – thanks in advance for your advice!
You can write your mail script and force it to run in the background using a shell command.
Using the
shell_execfunction of PHP, you can tell PHP to run a script in the background. The parameters you see in theshell_execcommand are seperated by spaces./path/to/php– The actual path to PHP on the server (you should be able to just use ‘php’ here)/path/to/send_notifications.php– The path to your mail script'var1' 'var2'–$_SERVER['argv']variables you can send to your script will be put in single quotes and spaced after the path to your script.>> /path/to/alert_log/paging.log– Path to a log file (optional), anything echo’ed in your mail script will be written to this file&– The most important part of this. The&tells your server to run this script in the backgroundDoing it this way, when you execute the mail script, the user submitting the form will not have to wait for the script to finish. The browser will load the page faster and the user can browse away from the confirmation page or even close the browser and the script will continue to run until its completed.