|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license http://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yii\log; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\base\InvalidConfigException; |
|
12
|
|
|
use yii\di\Instance; |
|
13
|
|
|
use yii\mail\MailerInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* EmailTarget sends selected log messages to the specified email addresses. |
|
17
|
|
|
* |
|
18
|
|
|
* You may configure the email to be sent by setting the [[message]] property, through which |
|
19
|
|
|
* you can set the target email addresses, subject, etc.: |
|
20
|
|
|
* |
|
21
|
|
|
* ```php |
|
22
|
|
|
* 'components' => [ |
|
23
|
|
|
* 'log' => [ |
|
24
|
|
|
* 'targets' => [ |
|
25
|
|
|
* [ |
|
26
|
|
|
* 'class' => 'yii\log\EmailTarget', |
|
27
|
|
|
* 'mailer' => 'mailer', |
|
28
|
|
|
* 'levels' => ['error', 'warning'], |
|
29
|
|
|
* 'message' => [ |
|
30
|
|
|
* 'from' => ['[email protected]'], |
|
31
|
|
|
* 'to' => ['[email protected]', '[email protected]'], |
|
32
|
|
|
* 'subject' => 'Log message', |
|
33
|
|
|
* ], |
|
34
|
|
|
* ], |
|
35
|
|
|
* ], |
|
36
|
|
|
* ], |
|
37
|
|
|
* ], |
|
38
|
|
|
* ``` |
|
39
|
|
|
* |
|
40
|
|
|
* In the above `mailer` is ID of the component that sends email and should be already configured. |
|
41
|
|
|
* |
|
42
|
|
|
* @author Qiang Xue <[email protected]> |
|
43
|
|
|
* @since 2.0 |
|
44
|
|
|
*/ |
|
45
|
|
|
class EmailTarget extends Target |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* @var array the configuration array for creating a [[\yii\mail\MessageInterface|message]] object. |
|
49
|
|
|
* Note that the "to" option must be set, which specifies the destination email address(es). |
|
50
|
|
|
*/ |
|
51
|
|
|
public $message = []; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var MailerInterface|array|string the mailer object or the application component ID of the mailer object. |
|
54
|
|
|
* After the EmailTarget object is created, if you want to change this property, you should only assign it |
|
55
|
|
|
* with a mailer object. |
|
56
|
|
|
* Starting from version 2.0.2, this can also be a configuration array for creating the object. |
|
57
|
|
|
*/ |
|
58
|
|
|
public $mailer = 'mailer'; |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function init() |
|
65
|
|
|
{ |
|
66
|
|
|
parent::init(); |
|
67
|
|
|
if (empty($this->message['to'])) { |
|
68
|
|
|
throw new InvalidConfigException('The "to" option must be set for EmailTarget::message.'); |
|
69
|
|
|
} |
|
70
|
|
|
$this->mailer = Instance::ensure($this->mailer, 'yii\mail\MailerInterface'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Sends log messages to specified email addresses. |
|
75
|
|
|
* Starting from version 2.0.14, this method throws LogRuntimeException in case the log can not be exported. |
|
76
|
|
|
* @throws LogRuntimeException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function export() |
|
79
|
|
|
{ |
|
80
|
|
|
// moved initialization of subject here because of the following issue |
|
81
|
|
|
// https://github.com/yiisoft/yii2/issues/1446 |
|
82
|
|
|
if (empty($this->message['subject'])) { |
|
83
|
|
|
$this->message['subject'] = 'Application Log'; |
|
84
|
|
|
} |
|
85
|
|
|
$messages = array_map([$this, 'formatMessage'], $this->messages); |
|
86
|
|
|
$body = wordwrap(implode("\n", $messages), 70); |
|
87
|
|
|
$message = $this->composeMessage($body); |
|
88
|
|
|
if (!$message->send($this->mailer)) { |
|
89
|
|
|
throw new LogRuntimeException('Unable to export log through email!'); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Composes a mail message with the given body content. |
|
95
|
|
|
* @param string $body the body content |
|
96
|
|
|
* @return \yii\mail\MessageInterface $message |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function composeMessage($body) |
|
99
|
|
|
{ |
|
100
|
|
|
$message = $this->mailer->compose(); |
|
101
|
|
|
Yii::configure($message, $this->message); |
|
102
|
|
|
$message->setTextBody($body); |
|
103
|
|
|
|
|
104
|
|
|
return $message; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|