johnsmith153 Posted September 10, 2008 Share Posted September 10, 2008 I am doing a message board which after a user uploads a message, PHP then sends email alerts to all users who have registered for them. As far as I am aware this just needs to be done, for example by pulling all registered emails from the database, then going through them and sending using the PHP mail command. However, wont the user that uploads their one message have to wait while all this processes? Or is this normal? I know it wont necessarily be long to send say 100 emails, but is there a way that the PHP script for the user just actions a script that is purely server side that doesnt require an output when it has finished? Then as soon as this has started, the user can continue, and in the background the server is half way through sending the emails etc. I.e it could output errors to a database etc as output to the screen is obviosuly not needed. What is the best way? Link to comment https://forums.phpfreaks.com/topic/123545-solved-php-processing-without-user-having-to-wait/ Share on other sites More sharing options...
lisa71283 Posted September 10, 2008 Share Posted September 10, 2008 The easiest way to accomplish this is to create a SQL table containing the parameters of emails that need to go out, and when the specified action occurs, simply INSERT the necessary row(s) into this table for later processing by a different script. The logic which creates and sends the email would be contained within a script called, for example, every minute by CRON. If that script is taking a long time to run, it won't affect the browsing experience of a user. Using such a method also eliminates the problem of a script being aborted either by the user or a network disruption in the middle of processing - for example, if you had the upload script sending the mail, and the user aborted or experienced a network problem after the image was uploaded, but before all of the mail was sent, you would have a situation where only some of the people receive the notification message. In general, you should always keep any heavy lifting not needed on an immediate basis confined to scripts called by CRON to operate on SQL queues. User-initiated scripts should never perform large background tasks. Link to comment https://forums.phpfreaks.com/topic/123545-solved-php-processing-without-user-having-to-wait/#findComment-638051 Share on other sites More sharing options...
The Little Guy Posted September 10, 2008 Share Posted September 10, 2008 I would just submit the message to the database, and have a column called "sentMail" or something like that set to 0 when the message is added. Next you would create a cron job that would check the database to all messages that have a sentMail value of 0, and send the mail, then update "sentMail" to 1 for that message. Now for the cron job, I would set it to run every one minute or so, to process the unsent messages. Link to comment https://forums.phpfreaks.com/topic/123545-solved-php-processing-without-user-having-to-wait/#findComment-638062 Share on other sites More sharing options...
johnsmith153 Posted September 10, 2008 Author Share Posted September 10, 2008 Excellent. Thanks. Link to comment https://forums.phpfreaks.com/topic/123545-solved-php-processing-without-user-having-to-wait/#findComment-638073 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.