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 and it’s comments. The email is sending, however the line $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 813PHP 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 = '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
EmailComponentclass. If you look at the source code, thestartup()method doesn’t actually have a body so it does nothing. Your controller isn’t actually assigned to theEmailComponent. The problem isn’t$controller->set('results', ...);. You need to useEmailComponent::initialize()instead ofEmailComponent::startup().Sources: