EmailTarget.php 3.11 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
use Yii;
use yii\base\InvalidConfigException;
12
use yii\di\Instance;
13 14
use yii\mail\MailerInterface;

w  
Qiang Xue committed
15
/**
w  
Qiang Xue committed
16
 * EmailTarget sends selected log messages to the specified email addresses.
w  
Qiang Xue committed
17
 *
Qiang Xue committed
18
 * You may configure the email to be sent by setting the [[message]] property, through which
19 20 21 22 23 24 25 26
 * you can set the target email addresses, subject, etc.:
 *
 * ```php
 * 'components' => [
 *     'log' => [
 *          'targets' => [
 *              [
 *                  'class' => 'yii\log\EmailTarget',
27
 *                  'mailer' =>'mailer',
28 29 30 31 32 33 34 35 36 37 38 39
 *                  'levels' => ['error', 'warning'],
 *                  'message' => [
 *                      'from' => ['log@example.com'],
 *                      'to' => ['developer1@example.com', 'developer2@example.com'],
 *                      'subject' => 'Log message',
 *                  ],
 *              ],
 *          ],
 *     ],
 * ],
 * ```
 *
40
 * In the above `mailer` is ID of the component that sends email and should be already configured.
w  
Qiang Xue committed
41 42
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
43
 * @since 2.0
w  
Qiang Xue committed
44
 */
w  
Qiang Xue committed
45
class EmailTarget extends Target
w  
Qiang Xue committed
46
{
47 48 49 50 51 52
    /**
     * @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).
     */
    public $message = [];
    /**
53
     * @var MailerInterface|array|string the mailer object or the application component ID of the mailer object.
54 55
     * After the EmailTarget object is created, if you want to change this property, you should only assign it
     * 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 63 64 65 66 67 68 69
    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        if (empty($this->message['to'])) {
            throw new InvalidConfigException('The "to" option must be set for EmailTarget::message.');
        }
70
        $this->mailer = Instance::ensure($this->mailer, 'yii\mail\MailerInterface');
71
    }
w  
Qiang Xue committed
72

73 74 75 76 77 78 79 80 81 82 83 84
    /**
     * Sends log messages to specified email addresses.
     */
    public function export()
    {
        // moved initialization of subject here because of the following issue
        // https://github.com/yiisoft/yii2/issues/1446
        if (empty($this->message['subject'])) {
            $this->message['subject'] = 'Application Log';
        }
        $messages = array_map([$this, 'formatMessage'], $this->messages);
        $body = wordwrap(implode("\n", $messages), 70);
85
        $this->composeMessage($body)->send($this->mailer);
86
    }
w  
Qiang Xue committed
87

88 89
    /**
     * Composes a mail message with the given body content.
90
     * @param string $body the body content
91 92 93 94
     * @return \yii\mail\MessageInterface $message
     */
    protected function composeMessage($body)
    {
95
        $message = $this->mailer->compose();
96 97 98 99 100
        Yii::configure($message, $this->message);
        $message->setTextBody($body);

        return $message;
    }
Zander Baldwin committed
101
}