Exemple #1
0
 /**
  * Enqueue the message storing it in database.
  * 
  * @return boolean true on success, false otherwise
  */
 public function queue()
 {
     $item = new Queue();
     $item->from = serialize($this->from);
     $item->to = serialize($this->getTo());
     $item->cc = serialize($this->getCc());
     $item->bcc = serialize($this->getBcc());
     $item->reply_to = serialize($this->getReplyTo());
     $item->charset = $this->getCharset();
     $item->subject = $this->getSubject();
     $item->attempts = 0;
     $parts = $this->getSwiftMessage()->getChildren();
     // if message has no parts, use message
     if (!is_array($parts) || !sizeof($parts)) {
         $parts = [$this->getSwiftMessage()];
     }
     foreach ($parts as $part) {
         if (!$part instanceof \Swift_Mime_Attachment) {
             /* @var $part \Swift_Mime_MimeEntity */
             switch ($part->getContentType()) {
                 case 'text/html':
                     $item->html_body = $part->getBody();
                     break;
                 case 'text/plain':
                     $item->text_body = $part->getBody();
                     break;
             }
             if (!$item->charset) {
                 $item->charset = $part->getCharset();
             }
         }
     }
     return $item->save();
 }
Exemple #2
0
 /**
  * Sends out the messages in email queue and update the database.
  * 
  * @return boolean true if all messages are successfully sent out
  */
 public function process()
 {
     if (Yii::$app->db->getTableSchema($this->table) == null) {
         throw new \yii\base\InvalidConfigException('"' . $this->table . '" not found in database. Make sure the db migration is properly done and the table is created.');
     }
     $success = true;
     $items = Queue::find()->where(['and', ['sent_time' => NULL], ['<', 'attempts', $this->maxAttempts]])->orderBy(['created_at' => SORT_ASC])->limit($this->mailsPerRound)->all();
     // dd($items);
     if (!empty($items)) {
         foreach ($items as $item) {
             if ($message = $item->toMessage()) {
                 $attributes = ['attempts', 'last_attempt_time'];
                 if ($this->sendMessage($message)) {
                     $item->sent_time = new \yii\db\Expression('NOW()');
                     $attributes[] = 'sent_time';
                 } else {
                     $success = false;
                 }
                 $item->attempts++;
                 $item->last_attempt_time = new \yii\db\Expression('NOW()');
                 $item->updateAttributes($attributes);
             }
         }
     }
     return $success;
 }