Question

So I have a code that checks the records on a table and emails them. table1 fields are: id - userid - email - subject - txt the code is as follow (simplified):

$sql = "SELECT * FROM table1 where userid='$userid'";
$results = mysql_query($sql);
while ($row = mysql_fetch_array($results)) 
{
mail($row['email'],$row['subject'],$row['txt']);
}

this will send all the emails at once. I want it to be able to send emails with random delays. for example : send the first email,wait 1 hour, send the second, wait 2 hours, send email 3 wait 5hours, send email 5 then wait 2hours ... I was thinking about using the function rand(); but I have no idea how to implement the delay ... any thoughts? I appreciate any input

Was it helpful?

Solution

Generate random numbers, add them in hour format to a timestamp, and save them in the database.

You can do this using the PHP rand() function and mysql (specifically this mysql code: DATE_ADD(NOW(), INTERVAL $randInteger HOUR) )

Run a script every hour that checks with the database if there is an email that should be sent/which email to send and then does based on the information it returns.

You can do this using cron (you have this in cpanel if you have a host that provides cpanel)

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