Yii2 Bootstrap 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.4 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\log;
13 years ago
use Yii;
use yii\base\InvalidConfigException;
use yii\mail\MailerInterface;
13 years ago
/**
13 years ago
* EmailTarget sends selected log messages to the specified email addresses.
13 years ago
*
11 years ago
* You may configure the email to be sent by setting the [[message]] property, through which
* you can set the target email addresses, subject, etc.
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
class EmailTarget extends Target
13 years ago
{
/**
* @var array the configuration array for creating a [[\yii\mail\MessageInterface|message]] object.
* Note that the "to" option must be set, which specifies the destination email address(es).
13 years ago
*/
public $message = [];
13 years ago
/**
* @var MailerInterface|string the mailer object or the application component ID of the mailer object.
* After the EmailTarget object is created, if you want to change this property, you should only assign it
* with a mailer object.
13 years ago
*/
public $mail = 'mail';
13 years ago
/**
* @inheritdoc
13 years ago
*/
public function init()
{
parent::init();
if (empty($this->message['to'])) {
throw new InvalidConfigException('The "to" option must be set for EmailTarget::message.');
}
if (is_string($this->mail)) {
$this->mail = Yii::$app->getComponent($this->mail);
}
if (!$this->mail instanceof MailerInterface) {
throw new InvalidConfigException("EmailTarget::mailer must be either a mailer object or the application component ID of a mailer object.");
}
}
13 years ago
/**
* Sends log messages to specified email addresses.
13 years ago
*/
12 years ago
public function export()
13 years ago
{
// moved initialization of subject here because of the following issue
// https://github.com/yiisoft/yii2/issues/1446
if (empty($this->message['subject'])) {
$this->message['subject'] = Yii::t('yii', 'Application Log');
}
$messages = array_map([$this, 'formatMessage'], $this->messages);
$body = wordwrap(implode("\n", $messages), 70);
$this->composeMessage($body)->send($this->mail);
13 years ago
}
/**
* Composes a mail message with the given body content.
* @param string $body the body content
* @return \yii\mail\MessageInterface $message
13 years ago
*/
protected function composeMessage($body)
13 years ago
{
$message = $this->mail->compose();
Yii::configure($message, $this->message);
$message->setTextBody($body);
return $message;
13 years ago
}
}