DbSpool.php 3.39 KB
<?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;
    }
}