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

w  
Qiang Xue committed
11 12
namespace yii\logging;

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

	/**
w  
Qiang Xue committed
43 44
	 * Sends log [[messages]] to specified email addresses.
	 * @param boolean $final whether this method is called at the end of the current application
w  
Qiang Xue committed
45
	 */
w  
Qiang Xue committed
46
	public function exportMessages($final)
w  
Qiang Xue committed
47
	{
w  
Qiang Xue committed
48 49 50 51 52 53 54 55 56 57 58
		$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);
		}

		$this->messages = array();
w  
Qiang Xue committed
59 60 61 62 63
	}

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