send reminder emails with php and mysql WITHOUT cron-job?

rand_user91 picture rand_user91 · May 27, 2013 · Viewed 14.1k times · Source

I just made a php script that will send email reminders to admins of a website 2 days before an appointment begins. I was going to automate the script to run through a cron job, only to realise who I am hosting with (crazy domains) does NOT appear to have Cron Jobs

Is there ANY way of doing this without cron-jobs? I do not mind going to a different service provider if that is the case; I have done quite a bit of searching around, and my understanding so far is that I need Cron Jobs for these types of things?

Here is the script I did so far:

<?php include "../connect-to-database.php";  ?>

<?php   
    $date = date("Y-m-d");
    $mod_date = strtotime($date."+ 2days");
    $newDate = date("m/d/Y",$mod_date);

    $sqlCommand = "SELECT * FROM eventcalender WHERE eventDate='$newDate'" ;
    $query = mysql_query($sqlCommand) or die(mysql_error());
    $count = mysql_num_rows($query);

if ($count >= 1){
        while($row = mysql_fetch_array($query)){
            $ID = $row["ID"];
    $schedule_title = $row["Title"];
    $schedule_description = $row["Detail"];
    $importance_level = $row["importance_level"];
    $meeting_datetime = $row["eventDate"];
    $contacts_involved = $row["contacts_involved"];
    $meeting_occurred = $row["meeting_occurred"];

    $mid = mysql_insert_id();
    $search_output .= "<ul>
                <li>
                    <h4>".$schedule_title."</h4>
                    <p><b>Time: ".$meeting_datetime."</b></p>
                    <p>People/Persons involved: ".$contacts_involved."</p>
                    <p>Meeting Occurred?: ".$meeting_occurred."</p>
                    <a href='uniqueMeeting.php?ID=".$ID."'>View more details of this meeting</a>
                    <p><a href='editschedulePage.php?mid=$ID'>Edit This Meeting</a></p>
                    <p><a href='scheduleList.php?deleteid=$ID'>Delete this meeting</a></p>
                </li><br/>                  
            </ul>";

            $sendMail = true;
        }

}

if($sendMail){  
    $admin = "SELECT * FROM admin" ;
    $queryAdmin = mysql_query($admin) or die(mysql_error());
    $adminCount = mysql_num_rows($queryAdmin);
    $recipients = array();
        if ($count >= 1){
            while($row = mysql_fetch_array($queryAdmin)){

            $subject ='A-CRM; UpComing Activities';
            $msg = $search_output; 
            $to = $row['email_address'];
            mail($to, $subject, $msg);
            }   
        }
}

?>

Yes I do realise I am an absolute horrible person for NOT using mysqli, and I will start to as soon as I finish this website

Answer

Fons picture Fons · May 27, 2013

If the site you're building is visited frequently, you can keep a timestamp (or datetime or whatever) in your database holding the next time the script has to run. Then include the script in your website (every page or just a selection them).

If the current time is equal or greater than the time in the database, run the script and set the time in the database to the value the script has to run next. In this way, the script will be executed by one of the visitors of the site, without them knowing.