コード例 #1
0
 /**
  * raiseMailEvent:
  *
  * @param  string $code - код
  * @param  array $data - данные
  * @throws CException
  * @return bool
  **/
 public function raiseMailEvent($code, array $data)
 {
     $mailEvent = MailEvent::model()->with('templates')->find(['condition' => 't.code = :code', 'params' => [':code' => $code]]);
     if (!$mailEvent) {
         throw new CException(Yii::t('MailModule.mail', 'MainEvent with "{code}" code was not found!'), [':code' => $code]);
     }
     if (!count($mailEvent->templates)) {
         throw new CException(Yii::t('MailModule.mail', 'MainEvent with code "{code}" don\'t contain any of active templates!'), [':code' => $code]);
     }
     foreach ($mailEvent->templates as $template) {
         $parsedData = $this->parseTemplate($template, $data);
         if ($this->message_layout) {
             if (isset(Yii::app()->controller)) {
                 $parsedData['body'] = Yii::app()->getController()->renderPartial($this->message_layout, ['content' => $parsedData['body']], true);
             } else {
                 $controller = new Controller('site');
                 $viewPath = Yii::getPathOfAlias('themes') . '/default/views/layouts/mail.php';
                 $parsedData['body'] = $controller->renderInternal($viewPath, ['content' => $parsedData['body']], true);
             }
         }
         if (!$this->getMailComponent()->send($parsedData['from'], $parsedData['from_name'], $parsedData['to'], $parsedData['theme'], $parsedData['body'])) {
             throw new CException(Yii::t('MailModule.mail', 'Error when sending mail!'));
         }
     }
     return true;
 }