MailerController.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace app\commands;
  8. use yii\console\Controller;
  9. use app\models\Mails;
  10. use Yii;
  11. /**
  12. * This command echoes the first argument that you have entered.
  13. *
  14. * This command is provided as an example for you to learn how to create console commands.
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @since 2.0
  18. */
  19. class MailerController extends Controller
  20. {
  21. public function actionSend()
  22. {
  23. foreach(Mails::find()->where(['process' => 0])->limit(20)->all() as $mail){
  24. $mail->updateAttributes(['process' => 1]);
  25. // try{
  26. Yii::$app->mailer->compose('main', ['content' => $mail->text])
  27. ->setTo($mail->email)
  28. ->setFrom([Yii::$app->params['adminEmail'] => Yii::$app->name])
  29. ->setSubject($mail->title)
  30. ->send();
  31. // $mail->delete();
  32. // }
  33. // catch(\Exception $e){
  34. // $mail->updateAttributes(['process' => 0]);
  35. // }
  36. }
  37. }
  38. }