EmailTarget.php 1.91 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * EmailTarget class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10 11
namespace yii\logging;

w  
Qiang Xue committed
12
/**
w  
Qiang Xue committed
13
 * EmailTarget sends selected log messages to the specified email addresses.
w  
Qiang Xue committed
14
 *
w  
Qiang Xue committed
15 16 17
 * The target email addresses may be specified via [[emails]] property.
 * Optionally, you may set the email [[subject]], [[sentFrom]] address and
 * additional [[headers]].
w  
Qiang Xue committed
18 19
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
20
 * @since 2.0
w  
Qiang Xue committed
21
 */
w  
Qiang Xue committed
22
class EmailTarget extends Target
w  
Qiang Xue committed
23 24 25 26
{
	/**
	 * @var array list of destination email addresses.
	 */
w  
Qiang Xue committed
27
	public $emails = array();
w  
Qiang Xue committed
28 29 30
	/**
	 * @var string email subject
	 */
w  
Qiang Xue committed
31
	public $subject;
w  
Qiang Xue committed
32
	/**
w  
Qiang Xue committed
33
	 * @var string email sent-from address
w  
Qiang Xue committed
34
	 */
w  
Qiang Xue committed
35
	public $sentFrom;
w  
Qiang Xue committed
36 37 38
	/**
	 * @var array list of additional headers to use when sending an email.
	 */
w  
Qiang Xue committed
39
	public $headers = array();
w  
Qiang Xue committed
40 41

	/**
Qiang Xue committed
42 43 44
	 * Sends log messages to specified email addresses.
	 * @param array $messages the messages to be exported. See [[Logger::messages]] for the structure
	 * of each message.
w  
Qiang Xue committed
45
	 */
Qiang Xue committed
46
	public function export($messages)
w  
Qiang Xue committed
47
	{
w  
Qiang Xue committed
48
		$body = '';
Qiang Xue committed
49
		foreach ($messages as $message) {
w  
Qiang Xue committed
50 51 52
			$body .= $this->formatMessage($message);
		}
		$body = wordwrap($body, 70);
53
		$subject = $this->subject === null ? \Yii::t('yii|Application Log') : $this->subject;
w  
Qiang Xue committed
54 55 56
		foreach ($this->emails as $email) {
			$this->sendEmail($subject, $body, $email, $this->sentFrom, $this->headers);
		}
w  
Qiang Xue committed
57 58 59 60 61
	}

	/**
	 * Sends an email.
	 * @param string $subject email subject
w  
Qiang Xue committed
62 63 64 65
	 * @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
w  
Qiang Xue committed
66
	 */
w  
Qiang Xue committed
67
	protected function sendEmail($subject, $body, $sentTo, $sentFrom, $headers)
w  
Qiang Xue committed
68
	{
w  
Qiang Xue committed
69 70 71
		if ($sentFrom !== null) {
			$headers[] = "From:  {$sentFrom}";
		}
Qiang Xue committed
72
		mail($sentTo, $subject, $body, implode("\r\n", $headers));
w  
Qiang Xue committed
73 74
	}
}