DanRz Posted January 2, 2023 Share Posted January 2, 2023 Hey! I am trying to write a script that will send emails to users using Amazon SES, I have a send limit of 1 per second... Currently I have 1500 records... how would I throttle the script to only send 1 email per second? Currently, I just use a foreach loop to go through an array and send the email using that... $allActiveMembers = $this->getAllMembers('1'); foreach($allActiveMembers as $member){ // If email blank... skip. if($member['memberEmail'] == NULL){ continue; } // Email Valid. if($this->helper->isValidEmail($member['memberEmail']) == false){ continue; } // Send Email $this->SendMail($member['memberEmail']); } Thanks Dan Quote Link to comment https://forums.phpfreaks.com/topic/315752-throttling-a-script/ Share on other sites More sharing options...
Barand Posted January 2, 2023 Share Posted January 2, 2023 sleep() , perchance to dream. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315752-throttling-a-script/#findComment-1604129 Share on other sites More sharing options...
mac_gyver Posted January 3, 2023 Share Posted January 3, 2023 at one per second, it will take 25 minutes to send all the emails. php scripts are not intended to run continuously. you would need to call set_time_limit(...) inside the loop to keep the script from fatally timing out and halting, but not using set_time_limit(0) so as to not prevent the script from stopping due to a programming mistake. this is normally done by queuing, in a database table, all the mail addresses to be sent to. having a cron job/scheduled task that runs at some reasonable interval, such as once a minute. the code that the cron job triggers checks if the queue has any unsent messages in it, loops, with a sleep() call inside the loop, to send fewer emails than the time to the next execution of the cron job, to prevent overlap, and marks each email in the queue as having been sent. e.g. with the datetime when it was sent. doing this, you would send ~50 email per minute, in 30 executions of the cron job. Quote Link to comment https://forums.phpfreaks.com/topic/315752-throttling-a-script/#findComment-1604130 Share on other sites More sharing options...
requinix Posted January 3, 2023 Share Posted January 3, 2023 3 hours ago, DanRz said: I am trying to write a script that will send emails to users using Amazon SES, I have a send limit of 1 per second... 1 message per second is what they give you in the sandbox. If you're using this for real messages then you should not be in the sandbox... Quote Link to comment https://forums.phpfreaks.com/topic/315752-throttling-a-script/#findComment-1604132 Share on other sites More sharing options...
DanRz Posted January 3, 2023 Author Share Posted January 3, 2023 23 hours ago, Barand said: sleep() , perchance to dream. Thanks, I was thinking sleep but I've had problems with that in the past... 20 hours ago, mac_gyver said: at one per second, it will take 25 minutes to send all the emails. php scripts are not intended to run continuously. you would need to call set_time_limit(...) inside the loop to keep the script from fatally timing out and halting, but not using set_time_limit(0) so as to not prevent the script from stopping due to a programming mistake. this is normally done by queuing, in a database table, all the mail addresses to be sent to. having a cron job/scheduled task that runs at some reasonable interval, such as once a minute. the code that the cron job triggers checks if the queue has any unsent messages in it, loops, with a sleep() call inside the loop, to send fewer emails than the time to the next execution of the cron job, to prevent overlap, and marks each email in the queue as having been sent. e.g. with the datetime when it was sent. doing this, you would send ~50 email per minute, in 30 executions of the cron job. Hmmm I think this might be the way forward... I shall do it like this 19 hours ago, requinix said: 1 message per second is what they give you in the sandbox. If you're using this for real messages then you should not be in the sandbox... Ahh yes, they gave me something like 150,000 emails per day, however, my access keys were leaked and used to send 50,000 odd emails.... so I asked them to move my limit down to 5000 per day as I would never send over that so if it ever happened again I would get stung... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/315752-throttling-a-script/#findComment-1604153 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.