Blame view

common/components/DbSpool.php 3.39 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  <?php
  namespace common\components;
  use common\models\MailerQueue;
  use yii\swiftmailer\Message;
  use yii\helpers\HtmlPurifier;
  use yii\helpers\Json;
  
  class DbSpool extends \Swift_ConfigurableSpool
  {
  
      /**
       * File WriteRetry Limit
       *
       * @var int
       */
      private $_retryLimit = 10;
  
  //    /**
  //     * Create a new DbSpool.
  //     *
  //     * @param string $path
  //
  //     */
      public function __construct()
      {
  
      }
      public function __destruct()
      {
         /* if (method_exists($this->_cache, 'clearAll'))
          {
              $this->_cache->clearAll($this->_cacheKey);
          }*/
      }
      /**
       * Tests if this Spool mechanism has started.
       *
       * @return bool
       */
      public function isStarted()
      {
          return true;
      }
  
      /**
       * Starts this Spool mechanism.
       */
      public function start()
      {
  
      }
  
      /**
       * Stops this Spool mechanism.
       */
      public function stop()
      {
  
      }
  
      /**
       * Allow to manage the enqueuing retry limit.
       *
       * Default, is ten and allows over 64^20 different fileNames
       *
       * @param int     $limit
       */
      public function setRetryLimit($limit)
      {
          $this->_retryLimit = $limit;
      }
  
      /**
       * Queues a message.
       *
       * @param Swift_Mime_Message $message The message to store
       *
       * @return bool
       *
       * @throws Swift_IoException
       */
      public function queueMessage(\Swift_Mime_Message $message)
      {
  
          $message->getBody();
          $queueObject = new MailerQueue();
  
          $queueObject->text_body = $message->getBody();
          $queueObject->subject = $message->getSubject();
          $queueObject->to = Json::encode($message->getTo());
          $queueObject->save();
      }
  
      /**
       * Sends messages using the given transport instance.
       *
       * @param Swift_Transport $transport        A transport instance
       * @param string[]        $failedRecipients An array of failures by-reference
       *
       * @return int     The number of sent e-mail's
       */
      public function flushQueue(\Swift_Transport $transport, &$failedRecipients = null)
      {
          if (!$transport->isStarted())
          {
              $transport->start();
          }
  
          $failedRecipients = (array) $failedRecipients;
  
          $count = 0;
          $emails = MailerQueue::find()
              ->where('sent_time IS NULL')
              ->orderBy('id')
              ->all();
  
          if (!count($emails))
          {
              return 0;
          }
        //  print_r($emails);
          foreach($emails as $email)
          {
              //$message = unserialize($email->message_object);
  
              $message = \Swift_Message::newInstance($email->subject)
                  ->setFrom(array('info@example.com' => 'ArtWeb'))
                  ->setTo(Json::decode($email->to))
                  ->setBody($email->text_body);
              try
              {
                  $count+= $transport->send($message, $failedRecipients);
                  if($count > 0)
                  {
  
                      $time = new \DateTime();
                      $email->setAttribute('sent_time', $time->format('Y-m-d H:i:s'));
                      $email->save();
                  }
                  else
                  {
                      throw new \Swift_SwiftException('The email was not sent.');
                  }
              }
              catch(\Swift_SwiftException $ex)
              {
  
              }
          }
          return $count;
      }
  }