コード例 #1
0
 /**
  * @param $id BroadcastSend->id
  * @return bool
  */
 public function actionSend($id)
 {
     if (!$id) {
         return false;
     }
     $broadcast_send = BroadcastSend::findOne($id);
     $broadcast_files = BroadcastFiles::find()->where(['broadcast_id' => $broadcast_send->broadcast_id])->all();
     $view = $broadcast_send->broadcast->broadcastLayout->layout_path;
     $title = $broadcast_send->broadcast->title;
     $broadcast_addresses = BroadcastAddress::find()->where(['broadcast_send_id' => $id, 'status' => 0])->all();
     $messages = [];
     /** @var $address BroadcastAddress */
     foreach ($broadcast_addresses as $address) {
         $email = $address->user_id ? $address->user->email : $address->email;
         $emailValidator = new EmailValidator();
         if (!$emailValidator->validate($email)) {
             continue;
         }
         $fio = $address->fio;
         $company = '';
         //$content = preg_replace('/{{content}}/', $broadcast_send->broadcast->broadcastLayout->content, $this->handleContent($broadcast_send->broadcast));
         $content = $this->handleContent($broadcast_send->broadcast, '', '');
         if ($broadcast_send->broadcast->broadcastLayout->content) {
             $content = preg_replace('/{{content}}/', $content, $broadcast_send->broadcast->broadcastLayout->content);
         }
         $mailer = Yii::$app->mailer->compose($view, ['content' => $content]);
         if ($broadcast_files) {
             foreach ($broadcast_files as $file) {
                 $attach_file = Yii::getAlias('@backend/web') . preg_replace('/^' . addcslashes(Yii::$app->params['broadcast']['clearMngUrl'], '/') . '/', '', $file->file);
                 if (is_file($attach_file)) {
                     $mailer = $mailer->attach($attach_file);
                 }
             }
         }
         $mailer->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])->setTo($email)->setSubject($title);
         try {
             $mailer->send();
             $address->status = 1;
             $address->update();
         } catch (\Exception $e) {
             echo "ERROR: " . $e . "\n";
         }
     }
     try {
         Yii::$app->mailer->sendMultiple($messages);
     } catch (Exception $e) {
         echo "Error\n";
     }
     $broadcast_send->status = 1;
     if ($broadcast_send->update()) {
         return true;
     }
     return false;
 }
コード例 #2
0
 public function actionStatus()
 {
     $this->enableCsrfValidation = false;
     if (Yii::$app->request->isAjax) {
         Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         return ['success' => true, 'address' => BroadcastAddress::find()->where(['broadcast_send_id' => Yii::$app->request->post('broadcast_send_id')])->all()];
     }
 }