CakePHP using Email component from Shell cronjob

前端 未结 4 1388
萌比男神i
萌比男神i 2021-01-02 01:19

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

Most of the code below was adapted from this dated article on the Bakery an

相关标签:
4条回答
  • 2021-01-02 01:34

    If you're using CakePHP 2.x, you can ditch the EmailComponent entirely and use the CakeEmail class instead.

    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. For email at least.

    0 讨论(0)
  • 2021-01-02 01:49

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

    Sources:

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

    If you're using CakePHP 2.x, try to use CakeEmail instead.

    CakeEmail#viewVars() provides setting variables to template.

    Here is example using CakeEmail from Shell. https://gist.github.com/tsmsogn/cee9cef2e851e7684021

    0 讨论(0)
  • 2021-01-02 01:50

    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

    0 讨论(0)
提交回复
热议问题