We currently have a working php mail script, this works fine and as we expected. But today we encountered an unforeseen issue, that servers only offer about 15 emails per minute via PHP to avoid having their IP address Blacklisted as mass mailer.
We are not trying to bypass this. We want a way to assure that this limit is not exceeded. So my question is, what methods can I use to set some type of delay based on how many emails have been sent in the last 60 seconds.
Please note that db_query() is a correct but specialised function for executing SQL queries
Here is our mail script (simplified):
$user_verification_key = md5(rand(1000, 100000000000));
db_query("insert into user_t values('" . $user . "', '" . $userinfo['first_name'] . "', '" . $fb_userinfo['last_name'] . "', '" . $userinfo['username'] . "', '" . $_POST['user_country'] . "', '" . $_POST['regions'] . "', '" . $_POST['paypal'] . "', '" . $user_verification_key . "', 0)");
$msg = "Thankyou for signing up:\n\n
Click the link to verify your account: http://website.com/verify.php?user=" . $fb_userinfo['id'] . "&verify=" . $user_verification_key . "
";
mail($_POST['paypal'], 'Account verfication', $msg, 'From: ' . 'no_reply@bartermate.com.au');
header("location: index.php?page=home&msg=Congratulations, your account has been setup please verify your email address");
One simple way would be to store all unprocessed e-mails in a MySQL database and use a cronjob to progressively go through them at intervals (say every 10-15 minutes). This way you don’t breach your hosting terms.
If you’re using cPanel, this is incredibly easy because you can setup a cronjob and link it to your PHP script. All your script would have to do is connect to your DB, access all unprocessed e-mails (i.e. those with a status of 0) and send them. As you loop through them, change their status to 1.
If you’re not too familiar with them, find out more about cronjobs here.