I am trying to do background email sending in my application.
I am making a shell that I will eventually schedule to be run through a crontask.
I am trying to run the following code in my EmailShell:
<?php
namespace App\Shell;
use Cake\Console\Shell;
use Cake\Core\App;
App::import('Controller', 'Messages');
class EmailShell extends Shell{
public function main(){
//Check for unsent emails
$messagesCont = new MessagesController;
$messages = $messagesCont->findAllUnsent();
//Send all unsent emails
foreach ($messages as $message){
$email = new Email();
$email->to($messages->receiver)
->subject($message->subject)
->send($message->body);
}
//Update the table to be marked as sent
}
}
?>
The code that I need from the MessagesController is as such:
public function findAllUnsent(){
$messages = $this->paginate($this->Messages);
foreach ($messages as $message){
if ($message->sent == false){
//If message is unsent then add it to the array
$messagesFound[] = $message;
}
}
//Return the array of unsent messages
return $messagesFound;
}
}
However when I run the shell through the command line terminal using bin/cake email
I get the following error:
Exception: Call to undefined method Cake\Core\App::import() in [/home/cabox/workspace/src/Shell/EmailShell.php, line 7]