【问题标题】:CakePHP using Email component from Shell cronjobCakePHP 使用来自 Shell cronjob 的电子邮件组件
【发布时间】:2011-09-18 17:02:53
【问题描述】:

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

下面的大部分代码都改编自this dated article on the Bakery,它是 cmets。电子邮件正在发送,但是$controller->set('result', $results[$i]); 行会引发以下通知:

注意:未定义的属性: 查看::$webroot 在 /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 行

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

我怎样才能做到这一点,最好遵循 Cake 约定?

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();
            }
        }
    }
}

【问题讨论】:

    标签: php email cakephp


    【解决方案1】:

    问题在于您初始化EmailComponent 类的方式。如果您查看源代码,startup() 方法实际上没有主体,因此它什么也不做。您的控制器实际上并未分配给EmailComponent。问题不在于$controller-&gt;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. EmailComponent::startup() Source

    【讨论】:

    • 我想我应该继续阅读面包店的文章。是时候回去赚 +50 了。
    【解决方案2】:

    如果您使用的是 CakePHP 2.x,您可以完全放弃 EmailComponent 并改用 CakeEmail class

    App::uses('CakeEmail', 'Network/Email');
    
    class NotificationShell extends Shell {
        public function send() {
            $email = new CakeEmail();
        }
    }
    

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

    【讨论】:

      【解决方案3】:

      如果您使用的是 CakePHP 2.x,请尝试改用 CakeEmail

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

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

      【讨论】:

        【解决方案4】:

        试试这个。

        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

        【讨论】:

        • 谢谢,但这是我拥有的相同代码。它只是带有$this 前缀的cleaner
        猜你喜欢
        • 1970-01-01
        • 2011-05-08
        • 2011-07-30
        • 2011-01-01
        • 2011-07-21
        • 1970-01-01
        • 2011-06-30
        • 2011-04-24
        • 1970-01-01
        相关资源
        最近更新 更多