1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Please guide where to put this command in task scheduler. I am not trying to send mail instead I need to insert a row in a table. When I run controller/action in console I am able to insert a record. But when running through task scheduler the record is not getting saved in the database. There are two fields in task scheduler. Program and arguments. Where to put your answer. I am not facing any kind of errors in the scheduler. Task completed successfully is displayed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.