Jump to content

Emailing a Monthly User Report


forrestfsu

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/105172-emailing-a-monthly-user-report/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.