Reputation: 2434
I've written a model and controller method that I wanted to use via a cron job, however on investigation it appears I have to do this as a shell withn Console/Command. I've read a few various SO posts and the manual but its all a little over my head.
This is what I have so far - pretty much ripped from what I had in my model and controller. Could someone give me some pointers on how to get this to work? Thank in advance.
**** EDIT - This now works ****
<?php
App::uses('Shell', 'Console');
App::uses('CakeEmail', 'Network/Email');
class OldClaimsShell extends AppShell {
// Include the ExpesesCLaim mdel
public $uses = array('ExpenseClaim');
/**
* When fired it checks for claims over 7 days that are still "submitted" and have therefore not been dealt with. Function has to be called MAIN - see cake docs
*/
public function main() {
// Find all claims with status "submitted" that are older than 7 days
$answer = $this->ExpenseClaim->haveClaimsOlderThan7Days();
//debug($answer);
if ($answer === true) {
$Email = new CakeEmail();
$Email->domain('www.xxx.co.uk');
$Email->template('oldExpenseClaimReminder');
$Email->to('[email protected]');
$Email->from('[email protected]');
$Email->subject('There are outstanding claims over 7 days old - Please Review');
$Email->viewVars(array('link' => 'http://xxx.xxx.co.uk'));
$Email->emailFormat('both');
$Email->send();
//$this->out('Email sent');
}
}
}
?>
Amended question based on answer and comments - this now works although Cron doesnt. Will be exploring this in another question.
Upvotes: 0
Views: 799
Reputation: 7575
In 2.x you should not use app::import()
You should not use controllers in shells
The latest 2.x branch has CakeEmail class which is for email from shells, models, controllers etc. (eg: wherever you like) EmailComponent is now a wrapper for this to keep backwards compatibility.
As far as code goes, shells are similar to controllers. You can specify the model to use like you would in a controller and fetching data is exactly the same.
Check the docs on sending emails, its pretty simple...
$Email = new CakeEmail();
// set config, add users, etc
$Email->send();
Then you run it in the cron with the usual
0 * * * * /path/to/app/Console/cake FooBar --params... etc
Upvotes: 1