PHP mail statistics

I need some help, i’ve written a script that sends a mail to all the users in a database, the thing is, i’d like to have some sort of message tell me how many emails have been sent.

The code i’m using written is below, it’s only part of the script but it’s the bit that does the sending.

while($row = mysql_fetch_array($result))
$request_subs = $row[‘requests’];
$name = $row[‘name’];
mail("$request_subs", $subject, $message, $headers);



The reason i’d like to do this is becuase i have around 120 mails that i’ll be sending out at a time and need to make sure that it goes out to everyone on the list.

Is there an easy way to do this?


If it was me I would would write to a text file and have it write out a successful message each time it send one. Then you can also keep count of the times it runs by doing,

$i = 0;
//Your code…


echo "Cycled " . $i . “times.”;

You could also write that to the text file as well. You will need to some checking on the mail function and get back if it fails or not.

Or use:


Using mysql_num_rows() would only count the rows in the database not how many times the for loop went through its iterations. I thought they wanted to check how many times the email got sent so they would know that it went to everyone in the database.

Ragster you’re spot on. I haven’t had a chance to get working on this yet but i’m sure you’ll be hearing from me when i get stuck. :D


while($row = mysql_fetch_array($result))

Loops through all rows returned by the query that returned the $result resource.


Returns the number of rows affected by the query that returned the $result resource.

So unless there are multiple email addresses used per MySQL row, something of which I see no evidence in the given code snippet, counting the number of iterations would have the EXACT same result as using mysql_num_rows.


Correct, that is what I meant, about the whole counting rows in the database thing. I meant it just counts the returned rows. But if you do that, you assume that the for loop is going to do exactly that number of iterations, which by all rights I am sure it would otherwise more than likely the script will fail, but keeping track while the for loop runs is more accurate depiction of what actually happened. Plus incrementing number could be used for other things like logs and such.

Put yes, all in all the $i and mysql_num_rows() should always be the same number in the end.

After thinking about it some more, rather than just list the number of emails sent, i think it would be better (in the long run) to make the process more reliable and more importantly restartable.

What i was thinking is, have a table where the last email sent would be inserted by the script. So every email sent is inserted into this table, in this table there could be a email id, a time/date and the email that the message was sent to.

Then for the sending to actually work, join the this table with the table containing the email addresses and check if an email has been sent for that message and or date.

To further improve the script i could put in a delay between each message going out so it doesn’t overload the server??

This is where i want the thing to go, it’s just getting there! :D I think i’m going to need some of your help with the actual code! ;D

If you wondering how to delay the sending u can use sleep() function.

I think its used something like sleep(5); which would be five seconds.

Sponsor our Newsletter | Privacy Policy | Terms of Service