Dave
Dave

Reputation: 243

CakePHP 1.3 running shell with cron - passing data to email template

I'm running a shell script via a cron job. It correctly sends an email. However I want to be able to pass data to the email template from the database which I seem unable to do.

Here is the shell

App::import('Core', 'Controller');
App::import('Component', 'Email');

class ExampleShell extends Shell {

var $uses = array('User');

function main() {

$users = $this->User->find('all');

$this->Controller =& new Controller();
$this->Email =& new EmailComponent(null);
$this->Email->initialize($this->Controller);

$this->Email->reset();
$this->Email->to = 'xx<[email protected]>';
$this->Email->subject = "Subject";
$this->Email->template = 'example';
$this->Email->sendAs = "both";
$this->Controller->set('users', $users); 
$this->Email->send();

}

}

The variable $users does not seem to be available in the example.ctp file? How can I pass data from the shell script to the template please?

Upvotes: 1

Views: 1848

Answers (1)

icebreaker
icebreaker

Reputation: 1274

I managed to do it in following way

class ExampleShell extends Shell {
  var $uses = array('User');
  var $Controller = null;

  function __construct(&$dispatch) {
    App::import('Core', 'Controller');
    App::import('Controller', 'App');
    $this->Controller = & new Controller();

    App::import('Component', 'Email');
    $this->Email = new EmailComponent();
    $this->Email->initialize($this->Controller); 
    parent::__construct($dispatch); 
  } 

  function main() {
    $users = $this->User->find('all');

    $this->Controller =& new Controller();
    $this->Email =& new EmailComponent(null);
    $this->Email->initialize($this->Controller);

    $this->Email->reset();
    $this->Email->to = 'xx<[email protected]>';
    $this->Email->subject = "Subject";
    $this->Email->sendAs = "both";
    $this->Controller->set('users', $users); 
    $this->Email->send(null, 'template_1');
  }
}

I hope it helps someone.

Make sure template is located where it should be app/views/elements/email/html/template_1.ctp and app/views/elements/email/text/template_1.ctp for text version. You should create layouts too in app/views/layouts/email/html/default.ctp and app/views/layouts/email/text/default.ctp

Upvotes: 1

Related Questions