MailerQueueController.php 3 KB
<?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.');
        }
    }
}