Question

for our website we need to send clients news about our activity (not spam, they can unsubscribe, and it's not related to the question). We have to send the emails at around 4 AM every day, so naturally i thought about creating a cron-job that calls an url that sends the emails. Then i did some research and saw that sending emails to all our users at the same time might not be the best option either due to the hogging of server resources, or the possibility we're gonna be marked as a spam website, so i thought about doing something like this :

create a cron job that calls a php script (script1.php) the script creates a cron that is executed every 10 minutes and calls another script(script2.php) script2.php checks if there are any users that havent' been mailed, if there are, it does a query on the database and retrieves x entries that haven't been mailed yet sends the mails to those users updates the database flag so that they won't be emailed again if there aren't any, then it sets the "mail sent" flags to their default value, and deletes the new cron (the one running every 10 minutes) or overwrites it with the default one (every day at 4 AM)

for code, script2 would look something like this :

$unsentEmails = $users->getUnsentEmails($limit);
if ($unsentEmails != 0 ){
    mailer::sentEmails($emailData);
    $user->markSentNewsletter($userData);
}
else{
    utils::resetCronTab();
    $user->resetSentNewsletter();
}

sorry for the lack of code, but i'm trying to figure out if what i want to do is the best solution in this scenario. Also, due to the nature of the information sent i'm trying to avoid using an external service if it's possible.

any help would be appreciated

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

think i found a better solution to this, since i was already using php on cli, i made a cron that calls the script and inside the script i send several emails, then use the sleep($seconds) command to wait for the next batch. works on the test server

thank you to all who answered

Was it helpful?

Solution

First of all did not get why do you want to have 2 scripts. Second one is redundant. Your idea is fine. I just will add few corrections. Instead of having simple flag use store data when this email sent. That way you will be able to control periodicity of notification and if somehow your cronjob not sent emails for particular day than it would see what days are left. In this case you would not need to reset flags at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top