EmailTarget.php 2.34 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8
namespace yii\log;
w  
Qiang Xue committed
9

10 11 12 13
use Yii;
use yii\base\InvalidConfigException;
use yii\mail\MailerInterface;

w  
Qiang Xue committed
14
/**
w  
Qiang Xue committed
15
 * EmailTarget sends selected log messages to the specified email addresses.
w  
Qiang Xue committed
16
 *
Qiang Xue committed
17 18
 * You may configure the email to be sent by setting the [[message]] property, through which
 * you can set the target email addresses, subject, etc.
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 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).
w  
Qiang Xue committed
28
	 */
29
	public $message = [];
w  
Qiang Xue committed
30
	/**
31 32 33
	 * @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.
w  
Qiang Xue committed
34
	 */
35 36
	public $mail = 'mail';

w  
Qiang Xue committed
37
	/**
38
	 * @inheritdoc
w  
Qiang Xue committed
39
	 */
40 41 42 43 44 45 46 47 48 49 50 51 52
	public function init()
	{
		parent::init();
		if (empty($this->message['to'])) {
			throw new InvalidConfigException('The "to" option must be set for EmailTarget::message.');
		}
		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.");
		}
	}
w  
Qiang Xue committed
53 54

	/**
Qiang Xue committed
55
	 * Sends log messages to specified email addresses.
w  
Qiang Xue committed
56
	 */
Qiang Xue committed
57
	public function export()
w  
Qiang Xue committed
58
	{
59 60 61
		// moved initialization of subject here because of the following issue
		// https://github.com/yiisoft/yii2/issues/1446
		if (empty($this->message['subject'])) {
62
			$this->message['subject'] = 'Application Log';
63
		}
64 65 66
		$messages = array_map([$this, 'formatMessage'], $this->messages);
		$body = wordwrap(implode("\n", $messages), 70);
		$this->composeMessage($body)->send($this->mail);
w  
Qiang Xue committed
67 68 69
	}

	/**
70 71 72
	 * Composes a mail message with the given body content.
	 * @param string $body the body content
	 * @return \yii\mail\MessageInterface $message
w  
Qiang Xue committed
73
	 */
74
	protected function composeMessage($body)
w  
Qiang Xue committed
75
	{
76 77
		$message = $this->mail->compose();
		Yii::configure($message, $this->message);
78
		$message->setTextBody($body);
79
		return $message;
w  
Qiang Xue committed
80
	}
Zander Baldwin committed
81
}