/** * Добавляет записи для рассылки в таблицу рассылки * * @param SiteContentInterface | \app\models\SubscribeItem $item тема письма */ public static function add($item) { if ($item instanceof SiteContentInterface) { $subscribeItem = $item->getMailContent(); } else { $subscribeItem = $item; } switch ($subscribeItem->type) { case self::TYPE_NEWS: $where = ['subscribe_is_news' => 1]; break; case self::TYPE_SITE_UPDATE: $where = ['subscribe_is_site_update' => 1]; break; case self::TYPE_MANUAL: $where = ['subscribe_is_tesla' => 1]; // $where = ['subscribe_is_test' => 1]; break; } $emailList = User::query($where)->select('email')->andWhere(['not', ['email' => null]])->andWhere(['not', ['email' => '']])->andWhere(['is_active' => 1])->column(); // VarDumper::dump(count($emailList),3,false);exit; $rows = []; foreach ($emailList as $email) { $urlUnSubscribe = Url::to(['subscribe/unsubscribe', 'mail' => $email, 'type' => $subscribeItem->type, 'hash' => self::hashGenerate($email, $subscribeItem->type)], true); SubscribeMailItem::insert(['text' => str_replace('{linkUnsubscribe}', $urlUnSubscribe, $subscribeItem->text), 'html' => str_replace('{linkUnsubscribe}', $urlUnSubscribe, $subscribeItem->html), 'subject' => $subscribeItem->subject, 'mail' => $email, 'date_insert' => time()]); $rows[] = $email; } \Yii::info($rows, 'tg\\subscribe'); }
/** * Делает рассылку писем из списка рассылки */ public function actionSend() { $time = microtime(true); $list = SubscribeMailItem::query()->limit(10)->orderBy(['date_insert' => SORT_DESC])->all(); if (count($list) > 0) { // \Yii::info('Всего писем для рассылки: ' . count($list), 'gs\\app\\commands\\SubscribeController::actionSend'); // \Yii::info('Список писем: ' . VarDumper::dumpAsString(ArrayHelper::getColumn($list, 'mail')), 'gs\\app\\commands\\SubscribeController::actionSend'); foreach ($list as $mailItem) { $mail = \Yii::$app->mailer->compose()->setFrom(\Yii::$app->params['mailer']['from'])->setTo($mailItem['mail'])->setSubject($mailItem['subject'])->setHtmlBody($mailItem['html']); if (isset($mailItem['text'])) { if ($mailItem['text'] != '') { $mail->setTextBody($mailItem['text']); } } $result = $mail->send(); if ($result == false) { \Yii::info('Не удалось доствить: ' . VarDumper::dumpAsString($mailItem), 'gs\\app\\commands\\SubscribeController::actionSend'); } } // \Yii::info('Список писем для удаления: ' . VarDumper::dumpAsString(ArrayHelper::getColumn($list, 'id')), 'gs\\app\\commands\\SubscribeController::actionSend'); SubscribeMailItem::deleteByCondition(['in', 'id', ArrayHelper::getColumn($list, 'id')]); // \Yii::info('Осталось после рассылки: ' . SubscribeMailItem::query()->count(), 'gs\\app\\commands\\SubscribeController::actionSend'); $time = microtime(true) - $time; // \Yii::info('Затраченное время на расылку: ' . $time, 'gs\\app\\commands\\SubscribeController::actionSend'); } \Yii::$app->end(); }
/** * Добавляет записи для рассылки в таблицу рассылки * * @param SiteContentInterface | \app\models\SubscribeItem $item тема письма */ public static function add($item) { if ($item instanceof SiteContentInterface) { $subscribeItem = $item->getMailContent(); } else { $subscribeItem = $item; } switch ($subscribeItem->type) { case self::TYPE_NEWS: $where = ['subscribe_is_news' => 1]; break; case self::TYPE_SITE_UPDATE: $where = ['subscribe_is_site_update' => 1]; break; case self::TYPE_MANUAL: $where = ['subscribe_is_manual' => 1]; break; } $emailList = User::query($where)->select('email')->andWhere(['not', ['email' => null]])->andWhere(['is_active' => 1, 'is_confirm' => 1])->column(); $rows = []; foreach ($emailList as $email) { $urlUnSubscribe = Url::to(['subscribe/unsubscribe', 'mail' => $email, 'type' => $subscribeItem->type, 'hash' => self::hashGenerate($email, $subscribeItem->type)], true); $rows[] = [str_replace('{linkUnsubscribe}', $urlUnSubscribe, $subscribeItem->text), str_replace('{linkUnsubscribe}', $urlUnSubscribe, $subscribeItem->html), $subscribeItem->subject, $email, time()]; } \Yii::info(ArrayHelper::getColumn($rows, 3), 'gs\\subscribe'); if (count($rows) > 0) { SubscribeMailItem::batchInsert(['text', 'html', 'subject', 'mail', 'date_insert'], $rows); } }