简体   繁体   中英

Send E mail to an user at a specific day every time in php (Yii framework)

I am new at php and using Yii framework.currently I am facing a problem how to send a mail to an user. I have a table which has a date field which indicates when the e mail needs to be sent. This date field is not the same for every user. When the date time comes the system will send a mail to the specific user. How can I do this. If anybody can give me some code example,that will be much more helpful. I do not want to use corn job. Need some code example.

Table:

User_id  job_title_id   cv_type  review_date       status
A        B              C        16/05/2015        yes

To be honest a cron job (linux) or task scheduler (windows) are specially useful for these kind of situations and I highly recommend you to use these builtin OS features but since you asked otherwise here I go:

A possible solution in php would be to create an infinite loop with a sleep in it like:

while(true){
    try{
        // Here should be the actual functionality to check the database
        // and send e-mails to the specific user(s) if needed.
    } catch(\Exception $e) { //this try catch exists to catch all exceptions that could crash this script.
        echo $e->getMessage(); 
        //notify yourself (by email) that an error has occurred 
    }

    sleep(60*60); //In this case 1 hour sleep 60 seconds * 60 = 60 minutes
}

The downside is that you'll have to manually start the job once, that is if the world was prefect... But since the script could run into an PHP fatal error or server reboot you'll have to manually reboot this script each time. To start the script in linux do something as the following:

nohup php script_name.php >> error.log & // nohup prevents the script from being closed when you exit the terminal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM