CakePHP使用Shell cronjob的Email组件

时间:2011-06-20 13:32:59

标签: php email cakephp

我正在尝试从CakePHP shell发送电子邮件,就像您从Controller那样。

以下大多数代码都是根据this dated article on the Bakery改编而来的。电子邮件正在发送,但是行$controller->set('result', $results[$i]);会抛出以下通知:

  

注意:未定义的属性:   查看:: $ webroot in   /home/jmccreary/www/intranet.sazerac.com/cakephp/cake/libs/view/view.php   在813号线上

     

PHP注意:未定义   变量:导致   /home/jmccreary/www/intranet.sazerac.com/cakephp/app/views/elements/email/text/nea/task_reminder_it.ctp   在第2行

所以我没有将任何变量传递到我的电子邮件视图。

我怎样才能做到这一点,理想情况下遵循蛋糕约定?

class NotificationShell extends Shell {
    var $uses = array('Employee', 'Task');

    function main() {
        // run if no action is passed
    }

    function nea_task_reminder() {
        // build Task to Employee relationship
        $this->Task->bindModel(array('belongsTo' => array('Employee' => array('className' => 'Employee', 'foreignKey' => 'object_id'))));
        $results = $this->Task->find('all', array('conditions' => array('application_id' => 1, 'completed_by_id' => 0), 'contain' => array('Employee' => array('Contact', 'Position'))));

        $count = count($results);
        if ($count) {
            App::import('Core', 'Controller');
            App::import('Component', 'Email');
            $controller =& new Controller();
            $email =& new EmailComponent();
            $email->startup($controller);

            // send email
            $email->from = Configure::read('Email.from');
            $email->to = 'jmccreary@whatever.com';
            $email->replyTo = 'no-reply@whatever.com';
            $email->template = 'nea/task_reminder_it';
            $email->sendAs = 'text';

            for ($i = 0; $i < $count; ++$i) {
                $email->subject = 'NEA Notification: Task Reminder for ' . $results[$i]['Employee']['Contact']['full_name'];
                $controller->set('result', $results[$i]);
                $email->send();
            }
        }
    }
}

4 个答案:

答案 0 :(得分:15)

问题在于您初始化EmailComponent类的方式。如果查看源代码,startup()方法实际上没有正文,所以它什么都不做。您的控制器实际上未分配给EmailComponent。问题不在于$controller->set('results', ...);。您需要使用EmailComponent::initialize()代替EmailComponent::startup()

$controller =& new Controller();
$email =& new EmailComponent(null);
$email->initialize($controller);

<强>来源:

  1. http://bakery.cakephp.org/articles/Jippi/2007/12/02/emailcomponent-in-a-cake-shell
  2. 的评论部分
  3. EmailComponent::startup() Source

答案 1 :(得分:13)

如果您正在使用CakePHP 2.x,则可以完全抛弃EmailComponent并改为使用CakeEmail class

App::uses('CakeEmail', 'Network/Email');

class NotificationShell extends Shell {
    public function send() {
        $email = new CakeEmail();
    }
}

这完全避免了在shell中加载组件的所有棘手问题。至少电子邮件。

答案 2 :(得分:1)

如果您正在使用CakePHP 2.x,请尝试使用CakeEmail

CakeEmail#viewVars()为模板提供设置变量。

以下是使用Shell的CakeEmail的示例。 https://gist.github.com/tsmsogn/cee9cef2e851e7684021

答案 3 :(得分:-1)

试试这个。

App::import('Core', 'Controller');
App::import('Component', 'Email');
$this->Controller =& new Controller();
$this->Email =& new EmailComponent(null);
$this->Email->initialize($this->Controller);    

//use set function as below  
$this->controller->set('result', $results[$i]);

如需更多参考,请点击以下链接:

http://ask.cakephp.org/questions/view/cron_shell_-_mail