/** * Generates new event log * @param Event $event * @param array $attributes * @return \self */ public static function create(Event $event, $attributes = []) { /** @var ActiveRecord $model */ $sender = $event->sender; /** @var \self $model */ $model = new static(array_merge(['user_id' => Yii::$app->user->id, 'model_name' => get_class($sender), 'model_id' => $sender->primaryKey, 'event' => $event->name], $attributes)); $model->message = $model->message ?: $model->createMessage(Inflector::camel2words($event->name)); return $model; }
/** * @param string $toEmail * @param string $subject * @param string $textPlain * @param string $textHtml * * @throws \Exception */ public static function sendWithLog($toEmail, $subject, $textPlain, $textHtml = '') { $mailer = new static(\Yii::$app->params); $messageId = explode('@', \Yii::$app->params['mailFromEmail']); $messageId = uniqid() . '@' . $messageId[1]; $exception = null; try { $message = $mailer->createMessage($subject, $textPlain, $textHtml, $messageId); $status = $mailer->send($message, $toEmail); } catch (\Exception $e) { $status = Email::STATUS_DELIVERY_ERROR; $exception = $e; } $obj = new Email(); $obj->toEmail = $toEmail; $obj->subject = $subject; $obj->text = $textPlain; $obj->dateSent = date('Y-m-d H:i:s'); $obj->status = $status; $obj->messageId = $messageId; $obj->save(); if ($exception) { /** @var \Exception $exception */ throw new \Exception($exception->getMessage()); } if (YII_ENV == 'test') { \Yii::$app->session->setFlash('email', 'E-Mail sent to: ' . $toEmail); } }