I have a mailing script which loops through approx 900 subscribers (in database), building an individual email for each subscriber and sending via SMTP. This script is fired manually via the browser, however the number of records involved have resulted in the script starting to time out part way through. In tests, the error is almost always Fatal Error max execution time exceeded (although one time there was开发者_如何学Go an error related to mail() and SMTP). I'm thinking that I should probably be running this type of script from the command line, however, the script still needs to be triggered manually (via a CMS admin user) - does anyone have any suggestions for a good way to handle this?
I think you can set the timeout for a PHP script from within the script.
However, I'd rather use CRON+PHP-CLI based solution. The PHP code remains the same whether you use CGI or CLI, however, in CLI mode there is no implicit time limit.
Tweeking the run time limit will only temporarily defer the problem.
You didn't say what OS this is running on - certainly if you've got a local MTA that may help, but the right solution would be to spawn a seperate process. Note that there are a number of pitfalls in this approach - see this post for details of how to do it correctly on Unix type systems.
set_time_limit (0); should do the trick.
There is not much to say about more efficient way, unless we see your code....
work with cronjobs and every time the cron executes the script it can send a small amount of mails.
For example the Typo3 mailer engine is invoked every 5 minutes and will check if any mails need to be sent.
So the user doesnt manually trigger the mailing script. The user only marks a newsletter which should be send by the mailer engine.
精彩评论