forrestfsu Posted May 11, 2008 Share Posted May 11, 2008 Hi all, I need to send out an email to my site's users at the beginning of each month. Before I get to my question, there are 2 things you need to know: 1. I can only send out emails at a rate of 100/hour (a limitation from my host) 2. Each user will receive a unique email based on their account. So here is what I have planned. Please tell me if it's the right approach or if there is a better (more elegant) solution: - Create a cronjob, that runs at the beginning of every month, for the sendmonthlyreport.php file. - Within sendmonthlyreport.php use "set_time_limit(0);". 0 means that no time limit is imposed (can run for ever). - Create a loop that goes through all users. - Create email content for the current user and send the email using fsockopen(). Increment a counter by 1. - If just sent the 100th email of the hour, call sleep() for another hour and reset the counter. I think that it would work fine, but I have one major concern. Is it a bad idea to leave a script running for hours? Even though I put set_time_limit(0), will PHP eventually cancel the script or even worse, do hosting providers usually look for a script that's been running for a while and cancel it? Any thoughts would be appreciated. Thanks, Jesse Quote Link to comment Share on other sites More sharing options...
dank Posted May 11, 2008 Share Posted May 11, 2008 Perhaps add a column to your MySQL table called 'email_sent' and then on the cron job script, have something like: $query = mysql_query("SELECT * FROM whatever WHERE email_sent='0' ORDER BY registration_time DESC LIMIT 100"); I just put the registration_time there as an idea where the first users get the newsletter first. It's not needed though, just a thought. Following that snippet, you could have something like: while($row = mysql_fetch_array($query)){ //do mailing thingy here mysql_query("UPDATE whatever SET email_sent='1' WHERE username='$row[username]'"); } Now, what I had in mind, you could have this script run every hour of the day for about 5 days, or whatever is necessary, at the beginning of the following month. Also, you would have to make another cron script to set all the email_sent values to 0 after that 5 day period. I don't know if this would work, but it's an idea you could work from. Hmm, it'd be faster to get a host that doesn't impose the 100 emails per hour limit, lol. Quote Link to comment Share on other sites More sharing options...
forrestfsu Posted May 11, 2008 Author Share Posted May 11, 2008 Thanks dank. Your method would work. However, I did not want to get the DB tables involved in this. I'm more curious if the sleep method is a legitimate solution. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.