Search code examples
phpyii2windows-task-scheduler

Running yii2 script using Windows Task Scheduler results in 0xFF


I want to run my action method in yii2 advanced automatically. I put the code on the console/controllers. This is the code:

MailController.php

<?php

namespace console\controllers;

use Yii;
use yii\console\Controller;
use Swift_TransportException;

class MailController extends Controller
{
    public function actionSend()
    {
        $mail = Yii::$app->mailer->compose()
            ->setFrom('[email protected]')
            ->setTo('[email protected]')
            ->setSubject('Testing');
        try
        {
            $mail->send();
        }
        catch (Swift_TransportException $ste)
        {
            echo 'error';
        }
    }
}

?>

Then I make this configuration from actions menu in task scheduler:

Program/script: C:\xampp\php\php.exe

Add arguments (optional): -f C:\xampp\htdocs\Baru\advanced\console\controllers\MailController.php

I want to run send action. But the result in task scheduler is 0xFF and I don't get the email. What's the problem?


Solution

  • You need to call the controller action this way:

    C:\xampp\php\php.exe c:\path\to\your\application\yii mail/send
    

    or

    c:\path\to\your\application\yii.bat mail/send
    

    You are calling yii, and not the controller script directly. The controller and action (the route) is just a parameter.

    See here or the guide for more information.