Reputation: 249
I'm working on a simple text messaging service for my high school's student council and my hosting service only allows 19 PHP mail messages to be sent per minute, so is there a way I can set an interval to only send 15 emails, wait a minute, send another 15, wait, and do so until all the mail is sent? Below is some of my code, all you'll probably need to see is the "foreach" section.
$subject = "";
$message = "Hey, $first! $messageget";
$header = 'From: Student Council<[email protected]>' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
foreach($to as $value) {
$result = mail($value, $subject, $message, $header);
}
Upvotes: 5
Views: 8311
Reputation: 1836
You just use the function:
sleep(60);
Put it in your loop.
EDIT:
for email counts, just add up the sent emails in the loop:
$i=0; // about the foreach loop
and inside the loop call
if($i<15){ $i++; continue; }
else{ $i=0; }
sleep(60);
Hope that clears it up.
EDIT2: and if that doesn't, here:
$subject = "";
$message = "Hey, $first! $messageget";
$header = 'From: Student Council<[email protected]>' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$i=0;
foreach($to as $value) {
$result = mail($value, $subject, $message, $header);
if($i<15){ $i++; continue; }
sleep(60);
}
Upvotes: 1
Reputation: 70853
Sending 15 Mails in 60 seconds is equivalent to sending one mail every 4 seconds.
So if you have a loop that would send all mails one after another, you decelerate by doing a sleep(4)
after every mail is sent.
foreach($to as $value) {
$result = mail($value, $subject, $message, $header);
sleep(4);
}
This is way easier than calculating when to send the next batch of 15 mails and then wait another 60 seconds. :)
Additionally, it evens out the usage of CPU and network ressources and does not peak after 60 seconds.
Upvotes: 10
Reputation: 845
foreach($to as $i=>$value) {
if($i%15==1) sleep(60);
$result = mail($value, $subject, $message, $header);
}
The Count can be done with Modulus $i%15 (run every 15th time) and then pause with sleep(60);
(This above answer assumes your array_keys are numeric and in order, you could also use:)
$i=0;
foreach($to as $value) {
if($i%15==1) sleep(60);
$result = mail($value, $subject, $message, $header);
$i++;
}
Upvotes: 2
Reputation:
You can use cron jobs for this situation.
http://www.google.com.tr/?q=cron+job+sending+email+php&oq=php+cron+job+sending
https://serverfault.com/questions/421485/cron-job-sending-bulk-emails-at-a-time
Upvotes: 4
Reputation: 717
You can use the sleep()
function :
sleep(60); // wait during 60 seconds
Upvotes: 1