简体   繁体   English

CakePHP使用Shell cronjob的Email组件

[英]CakePHP using Email component from Shell cronjob

I'm trying to send an email from a CakePHP shell just as you would from the Controller. 我正试图从CakePHP shell发送一封电子邮件,就像你从Controller那样。

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]); 电子邮件正在发送,但行$controller->set('result', $results[$i]); throws the following notices: 抛出以下通知:

Notice: Undefined property: View::$webroot in /home/jmccreary/www/intranet.sazerac.com/cakephp/cake/libs/view/view.php on line 813 注意:未定义的属性:在第813行的/home/jmccreary/www/intranet.sazerac.com/cakephp/cake/libs/view/view.php中查看:: $ webroot

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 PHP注意:未定义的变量:结果在第2行的/home/jmccreary/www/intranet.sazerac.com/cakephp/app/views/elements/email/text/nea/task_reminder_it.ctp

So I'm not getting any of the variables passed to my email view. 所以我没有将任何变量传递给我的电子邮件视图。

How can I do this, ideally following the Cake conventions? 我怎么能这样做,最好遵循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();
            }
        }
    }
}

The problem is the way you're initializing the EmailComponent class. 问题是您初始化EmailComponent类的方式。 If you look at the source code, the startup() method doesn't actually have a body so it does nothing. 如果你看一下源代码,那么startup()方法实际上并没有一个正文,所以它什么都不做。 Your controller isn't actually assigned to the EmailComponent . 您的控制器实际上未分配给EmailComponent The problem isn't $controller->set('results', ...); 问题不是$controller->set('results', ...); . You need to use EmailComponent::initialize() instead of EmailComponent::startup() . 您需要使用EmailComponent::initialize()而不是EmailComponent::startup()

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

Sources: 资料来源:

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

If you're using CakePHP 2.x, you can ditch the EmailComponent entirely and use the CakeEmail class instead. 如果您正在使用CakePHP 2.x,则可以完全抛弃EmailComponent并使用CakeEmail

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

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

That entirely avoids all the thorny issues of loading components inside a shell. 这完全避免了在shell中加载组件的所有棘手问题。 For email at least. 至少对于电子邮件。

If you're using CakePHP 2.x, try to use CakeEmail instead. 如果您正在使用CakePHP 2.x,请尝试使用CakeEmail

CakeEmail#viewVars() provides setting variables to template. CakeEmail#viewVars()为模板提供设置变量。

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

Try this. 试试这个。

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]);

for more reference click on below link: 如需更多参考,请点击以下链接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM