php - To add remainder email in yii using crontab -


as suggested created file messengercommand.php under protected/commands

class messengercommand extends cconsolecommand {     public function run($args)     {         /* if(erunactions::runbackground())         { */         $mail=yii::app()->smtpmail;         $mail->setfrom("tsadmin@softthink.com", 'from name');         $mail->subject    ="hello";         $mail->msghtml("haiii workd");         $mail->addaddress("rajesh.udutha@itaugments.com", "");         if(!$mail->send()) {             echo "mailer error: " . $mail->errorinfo;         }else {             echo "message sent!";         } } } 

and added yiic command as

$path = dirname(__file__); //echo $path; shell_exec( $path . "/protected/yiic messenger" ); 

and trigger email when load site ....

but dont wanna refresh site ..i need make run in background ..please me.

you can use yii console applications accomplish task.

in protected/commands create new file command sufix, example: messengercommand.php:

<?php class messengercommand extends cconsolecommand { ....... 

in class messengercommand you have several options create command action. in sample override run method:

public function run($args) {         $birth_month = date("m");         $birth_day = date("d");         $criteria = new cdbcriteria;         $criteria->condition = "birth_month = $birth_month , birth_day = $birth_day";         $listscheduledrecords = table::model()->findall($criteria);         foreach($listscheduledrecords $scheduled_record):             $this->send($scheduled_record);         endforeach; }  public function send($scheduled_record) {     ....     logic send email     .... } 

in protected dir, create file: messenger.php. file command executer:

<?php $path = dirname(__file__); $output = shell_exec( $path . "/./yiic messenger" ); echo $output; 

to test it, on linux/unix, run in console/terminal:

cd /.../.../...your_protected_path  php messenger.php 

to test on windows, need refer php.exe location path or set php.exe on system environment variables , use yiic equivalence windows

to schedule automatic task, in sample, daily execution, on linux/unix, can use cron jobs:

in console/terminal:

crontab -e 

in cron file, add scheduled task, daily, @ 9h00. remember cron sintax: # minute hour day of month month day of week command

0   9  *  *  * php /full_path/protected/messenger.php 

save file , exit.

to schedule automatic task on windows, refer docs / on internet.

if have errors, yii console applications use own config file (protected/config/console.php). common mistakes wrong db connection, components, modules in protected/config/console.php.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -