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

假如想象 提交于 2019-12-12 21:23:30

问题


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


回答1:


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.




回答2:


Replace

php include "../connect-to-database.php"; 

with

php include dirname(__DIR__) . "/connect-to-database.php"; 



回答3:


Try to add some Observer which will check whether there is a need to send mails.

For example - add into your init file (maybe index.php)

<?php include DIR ."send_mail.php" ?> // your file that you describe in the question.

It will add an additional query to DB since the check will be executed every time the user comes on the page.

I hope it will help you to resolve your issue.




回答4:


Very simply and effective way using crons. The crons are very simple you will set up date and time cron automatically runs your file. This is guide to using crontab: http://www.adminschoice.com/crontab-quick-reference/



来源:https://stackoverflow.com/questions/16775508/send-reminder-emails-with-php-and-mysql-without-cron-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!