简体   繁体   English

如何使用控制台控制器在yii2中调用Swift邮件程序?

[英]How to call Swift mailer in yii2 with console controller?

Yii::$app
          ->mailer
          ->compose(
              ['html' => 'remainder-html', 'text' => 'remainder-text'],
              ['user' => $user,'interview'=>$interview]
          )
          ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])
          ->setTo($user->email)
          ->setSubject('Contact Request From ' . $model->name)
          ->send();

Above is the code I have used in console/controller for advanced app in yii2. 上面是我在yii2中的高级应用程序的控制台/控制器中使用的代码。 Rest of things are working only the mail is not working. 剩下的事情都在起作用,只有邮件不起作用。 Same code work when placed in backend/frontend controller. 当放在后端/前端控制器中时,相同的代码工作。 Following is the error. 以下是错误。

PHP User Error 'yii\\base\\ErrorException' with message 'Call to undefined method compose' PHP用户错误“ yii \\ base \\ ErrorException”,并显示消息“调用未定义的方法撰写”

Yii2 Applications use a different configuration file for console and web. Yii2应用程序将不同的配置文件用于控制台和Web。

All you need to do is configure the 'mailer' component in your console configuration file. 您需要做的就是在控制台配置文件中配置“邮件程序”组件。 If you are using the Basic Application Template, this is the 'config/console.php'. 如果您使用的是基本应用程序模板,则为“ config / console.php”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM