我想在我的應用程序中做後臺電子郵件發送。CakePHP 3.x在Shell中使用控制器方法
我正在製作一個shell,我最終將安排通過crontask來運行。
我試圖跑在我的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
}
}
?>
,我從MessagesController需要的代碼是這樣的:
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;
}
}
然而,當我通過運行shell命令行終端使用bin/cake email
我收到以下錯誤:
Exception: Call to undefined method Cake\Core\App::import() in [/home/cabox/workspace/src/Shell/EmailShell.php, line 7]
https://stackoverflow.com/questions/28511023/how-to-load-a-component-in-console-shell取代組件與控制器和答案是完全一樣的。可以標記爲它的副本。 – burzum
@burzum那麼你會如何建議在後臺做電子郵件?沒有任何資源可以做到這一點,我正努力爲CakePHP 3.x找到詳細的最新指南。 –