1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\log;
/**
* EmailTarget sends selected log messages to the specified email addresses.
*
* The target email addresses may be specified via [[emails]] property.
* Optionally, you may set the email [[subject]], [[sentFrom]] address and
* additional [[headers]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class EmailTarget extends Target
{
/**
* @var array list of destination email addresses.
*/
public $emails = [];
/**
* @var string email subject
*/
public $subject;
/**
* @var string email sent-from address
*/
public $sentFrom;
/**
* @var array list of additional headers to use when sending an email.
*/
public $headers = [];
/**
* Sends log messages to specified email addresses.
*/
public function export()
{
$body = '';
foreach ($this->messages as $message) {
$body .= $this->formatMessage($message);
}
$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.
* @param string $subject email subject
* @param string $body email body
* @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)
{
if ($sentFrom !== null) {
$headers[] = "From: {$sentFrom}";
}
mail($sentTo, $subject, $body, implode("\r\n", $headers));
}
}