cronjob的电子邮件组件

cronjob的电子邮件组件

本文介绍了CakePHP使用Shell cronjob的电子邮件组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to send an email from a CakePHP shell just as you would from the Controller.

以下大部分代码都是从,它的意见。电子邮件正在发送,但是 $ controller-> set('result',$ results [$ i]); 的行发出以下通知:

Most of the code below was adapted from this dated article on the Bakery and it's comments. The email is sending, however the line $controller->set('result', $results[$i]); throws the following notices:

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

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

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

So I'm not getting any of the variables passed to my email view.

我如何做到这一点,理想地遵循蛋糕约定?

How can I do this, ideally following the Cake conventions?

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 = '[email protected]';
            $email->replyTo = '[email protected]';
            $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()

The problem is the way you're initializing the EmailComponent class. If you look at the source code, the startup() method doesn't actually have a body so it does nothing. Your controller isn't actually assigned to the EmailComponent. The problem isn't $controller->set('results', ...);. You need to use EmailComponent::initialize() instead of EmailComponent::startup().

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

资料来源:




  1. Comments section of http://bakery.cakephp.org/articles/Jippi/2007/12/02/emailcomponent-in-a-cake-shell
  2. EmailComponent::startup() Source

这篇关于CakePHP使用Shell cronjob的电子邮件组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:48