我想在我的应用程序中做后台电子邮件发送。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找到详细的最新指南。 –