Here is some sample code. Put the file in the commands folder as MailController.php.
<?php
namespace app\commands;
use Yii;
use yii\console\Controller;
use app\models\Mail;
use ZBateson\MailMimeParser\MailMimeParser;
class MailController extends Controller
{
/*
* Usage: ./yii mail/get-new
*/
public function actionGetNew()
{
$imap_driver = new \imapDriver();
// [...] Do stuff
// Process messages by calling a private function
foreach ($ids as $id) {
$mail = $imap_driver->get_mail_from_uid($id);
if (!$mail) {
echo 'get_mail_from_uid(' . $id . ') failed: ' . $imap_driver->error . "\n";
return false;
}
$this->processMail($mail);
}
}
/*
* This method is private and cannot be invoked from the command line
*/
private function processMail($mail)
{
$in_console = (getenv('TERM') != '');
$mailParser = new MailMimeParser();
$message = $mailParser->parse($mail);
// [...] Do stuff
}
}
These bits must match for the ./yii command to be available.