Blame view

backend/controllers/MailerQueueController.php 3 KB
97be095e   alexanderWeb   message
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  <?php
  
  namespace backend\controllers;
  
  use backend\models\Message;
  use backend\models\MessageLang;
  use Yii;
  use common\models\MailerQueue;
  use common\components\DbSpool;
  use yii\data\ActiveDataProvider;
  use yii\web\Controller;
  use yii\web\NotFoundHttpException;
  use yii\filters\VerbFilter;
  
  /**
   * MailerQueueController implements the CRUD actions for MailerQueue model.
   */
  class MailerQueueController extends Controller
  {
      public function behaviors()
      {
          return [
              'verbs' => [
                  'class' => VerbFilter::className(),
                  'actions' => [
                      'delete' => ['post'],
                  ],
              ],
          ];
      }
  
      /**
       * @param integer $id message id
       * add message to queue
       */
      public function actionSpooler($id)
      {
          $curr_lang=2;
          $message_tempalete = Message::findOne(['id'=>$id]);
          $message_template_langs = $message_tempalete->getMessageLangs()->where(['>=', 'lang_id', '1'])->indexBy('lang_id')->all();
          $message = \Swift_Message::newInstance();
          $spool = new DbSpool();
          $transport = \Swift_SpoolTransport::newInstance($spool);
          $mailer = \Swift_Mailer::newInstance($transport);
          foreach ($message_template_langs as $message_template_lang)
          {
              if($message_template_lang->lang_id == $curr_lang )
              {
                  $message ->setSubject($message_template_lang->title)
                      ->setFrom('info@example.com')
                      ->setTo('alexandr.khivrich@gmail.com')
                      ->setBody($message_template_lang->body);
                  if($message_template_lang->is_html)
                  {
                      $message ->setContentType('text\html');
                  }
                  break;
              }
          }
          $result = $mailer->send($message);
      }
  
      /**
       * Send Queue messages
       */
      public function actionSend()
      {
          $messageLimit = 10;
          $timeLimit = 0;
  
          $spool = new DbSpool();
          //$transportReal = \Swift_MailTransport::newInstance();
          $transportReal = \Swift_SmtpTransport::newInstance(
              "smtp.gmail.com",
              "465",
              "ssl"
          )
              ->setUsername("alexandr.khivrich@gmail.com")
              ->setPassword("10181997Nadia");
  
          $spool->setMessageLimit($messageLimit);
          $spool->setTimeLimit($timeLimit);
          $sent = $spool->flushQueue($transportReal);
  
          echo sprintf('sent %s emails', $sent);
      }
      /**
       * Finds the MailerQueue model based on its primary key value.
       * If the model is not found, a 404 HTTP exception will be thrown.
       * @param integer $id
       * @return MailerQueue the loaded model
       * @throws NotFoundHttpException if the model cannot be found
       */
      protected function findModel($id)
      {
          if (($model = MailerQueue::findOne($id)) !== null) {
              return $model;
          } else {
              throw new NotFoundHttpException('The requested page does not exist.');
          }
      }
  }