|
|
@ -7,6 +7,10 @@ |
|
|
|
|
|
|
|
|
|
|
|
namespace yii\log; |
|
|
|
namespace yii\log; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Yii; |
|
|
|
|
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
|
|
|
use yii\mail\MailerInterface; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* EmailTarget sends selected log messages to the specified email addresses. |
|
|
|
* EmailTarget sends selected log messages to the specified email addresses. |
|
|
|
* |
|
|
|
* |
|
|
@ -20,51 +24,57 @@ namespace yii\log; |
|
|
|
class EmailTarget extends Target |
|
|
|
class EmailTarget extends Target |
|
|
|
{ |
|
|
|
{ |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var array list of destination email addresses. |
|
|
|
* @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). |
|
|
|
public $emails = []; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var string email subject |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $subject; |
|
|
|
public $message = []; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string email sent-from address |
|
|
|
* @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. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $sentFrom; |
|
|
|
public $mail = 'mail'; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var array list of additional headers to use when sending an email. |
|
|
|
* @inheritdoc |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $headers = []; |
|
|
|
public function init() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
parent::init(); |
|
|
|
|
|
|
|
if (empty($this->message['to'])) { |
|
|
|
|
|
|
|
throw new InvalidConfigException('The "to" option must be set for EmailTarget::message.'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (empty($this->message['subject'])) { |
|
|
|
|
|
|
|
$this->message['subject'] = Yii::t('yii', 'Application Log'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
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."); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Sends log messages to specified email addresses. |
|
|
|
* Sends log messages to specified email addresses. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function export() |
|
|
|
public function export() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$body = ''; |
|
|
|
$messages = array_map([$this, 'formatMessage'], $this->messages); |
|
|
|
foreach ($this->messages as $message) { |
|
|
|
$body = wordwrap(implode("\n", $messages), 70); |
|
|
|
$body .= $this->formatMessage($message); |
|
|
|
$this->composeMessage($body)->send($this->mail); |
|
|
|
} |
|
|
|
|
|
|
|
$body = wordwrap($body, 70); |
|
|
|
|
|
|
|
$subject = $this->subject === null ? \Yii::t('yii', 'Application Log') : $this->subject; |
|
|
|
|
|
|
|
foreach ($this->emails as $email) { |
|
|
|
|
|
|
|
$this->sendEmail($subject, $body, $email, $this->sentFrom, $this->headers); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Sends an email. |
|
|
|
* Composes a mail message with the given body content. |
|
|
|
* @param string $subject email subject |
|
|
|
* @param string $body the body content |
|
|
|
* @param string $body email body |
|
|
|
* @return \yii\mail\MessageInterface $message |
|
|
|
* @param string $sentTo sent-to email address |
|
|
|
|
|
|
|
* @param string $sentFrom sent-from email address |
|
|
|
|
|
|
|
* @param array $headers additional headers to be used when sending the email |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function sendEmail($subject, $body, $sentTo, $sentFrom, $headers) |
|
|
|
protected function composeMessage($body) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($sentFrom !== null) { |
|
|
|
$message = $this->mail->compose(); |
|
|
|
$headers[] = "From: {$sentFrom}"; |
|
|
|
Yii::configure($message, $this->message); |
|
|
|
} |
|
|
|
$message->setTextBody($body); |
|
|
|
mail($sentTo, $subject, $body, implode("\r\n", $headers)); |
|
|
|
return $message; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|