I want to be able to send out newsletters from my server to all my subscribers via email. There are over a thousand email addresses in the database that I'll need to cater to. I've just created a backend system for me to do this, which allows me to write newsletters and start sending them, along with a progress bar to monitor the progress. For testing purposes instead of sending emails, I call sleep(1) I then realised if I try to reload the page or run another PHP script while the script is running, nothing happened - the batch email process had to finish first. And then I remembered that PHP is single threaded and my system was ill-conceived.
I know there is a way to simulate multi-threading with the Thread class, but my host server does not support this.
The other obvious way round this is to break the task up and use cron jobs to fire off a few emails bit by bit, but that means I'd have to authenticate my email server repeatedly, perhaps upto 100 times in a fairly short time period if I want my site to remain active. With my server restrictions this would likely throw up an issue.
The obvious answer is to find a new server with less restrictions, or to stagger the newsletter to such a degree that it would overcome any problems, but that's a last resort, and in which case I'll probably just find a way to do the batch emailing locally.
Is there any way I can execute a long running PHP script like this without delaying every other script until it completes?
Have I misunderstood some rather basic PHP knowledge somewhere? Because paradoxically I find I can run some long running scripts (for instance I have a php script that takes about 5 seconds to complete as it converts my old database to a new format) that for some reason can run parallel to other scripts it seems.
Yes, I'm confused. Please help.
EDIT: As wischi correctly pointed out, the script that sends the emails also calls session_start(), as it needs to check if an admin is logged in and calling the script for security purposes. This is why literally everything halts during this batch email script is working.