微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CakePHP使用Shell cronjob的电子邮件组件

我正在尝试从Cake PHP shell发送一封电子邮件,就像从Controller那样.

下面的代码大部分是从this dated article on the Bakery改编而来的.电子邮件正在发送,但是$controller-> set(‘result’,$results [$i]);引发以下通知

Notice: Undefined property:
View::$webroot in
/home/jmccreary/www/intranet.sazerac.com/cakePHP/cake/libs/view/view.PHP
on line 813

PHP Notice: Undefined
variable: result in
/home/jmccreary/www/intranet.sazerac.com/cakePHP/app/views/elements/email/text/nea/task_reminder_it.ctp
on line 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();
            }
        }
    }
}
问题是初始化EmailComponent类的方式.如果你看源代码,startup()方法实际上并不具有一个主体,所以它什么都不做.您的控制器实际上并未分配给EmailComponent.问题不是$controller-> set(‘results’,…);.您需要使用EmailComponent :: initialize()而不是EmailComponent :: startup().
$controller =& new Controller();
$email =& new EmailComponent(null);
$email->initialize($controller);

资料来源:

> http://bakery.cakephp.org/articles/Jippi/2007/12/02/emailcomponent-in-a-cake-shell评论
> EmailComponent::startup() Source

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐