2013-05-01 56 views
0

我想通过在我的应用程序的cron作业发送电子邮件通知给一些用户。CakePHP如何发送电子邮件通知

经过几个小时的阅读,我明白了最好的方法是使用Shell。

请有人可以帮助我了解如何做到这一点,我怎样才能使用一个myShell类的不同动作发送不同的通知?我的意思是如何cron访问myShell的不同动作。例如

<?php 
    class MyShell extends Shell { 

    function send_task_notifications(){ 
     .... //this must send email every day at 00:00 am 
    } 

    function send_new_post_notifications() { 
     .... //this must send email every week// 
    } 

    } 
?> 

这两个操作都在MyShell类中。

那么我怎样才能通过Cron调用其中的一个,并且这个MyShell类可以通过URL访问?

回答

1

您的shell需要按照以下方式更改,您需要传递一个基于参数的参数,它将执行电子邮件通知/推送通知。移动你的功能组件,将工作

<?php 
    class MyShell extends Shell { 

    function main() 
    { 
     $option = !empty($this->args[0]) ? $this->args[0] : ”; 
     echo ‘Cron started without any issue.’; 

     App::import(‘Component’, 'MyOwnComponent'); 
     $this->MyOwnComponent = &new MyOwnComponent(); 
     switch ($option) 
     { 
      case 'task_notifications': 
        $this->MyOwnComponent->send_task_notifications(); 
       break; 
      case 'post_notifications': 
        $this->MyOwnComponent->send_new_post_notifications(); 
       break; 
      default: 
      echo 'No Parameters passed .'; 
     } 

    } 
    } 
?> 

您的组件文件如下

<?php 
class MyOwnComponent extends Object 
{ 
function send_task_notifications(){ 
     .... //this must send email every day at 00:00 am 
    } 

function send_new_post_notifications() { 
     .... //this must send email every week// 
    } 
} 

?> 

欲了解更多详情,请参阅链接http://cakephpsaint.wordpress.com/2013/05/15/6-steps-to-create-cron-jobs-in-cakephp/