MailPanel.php 4.37 KB
Newer Older
Mark committed
1
<?php
Carsten Brandt committed
2 3 4 5 6
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
Mark committed
7 8 9 10 11 12 13 14 15

namespace yii\debug\panels;

use Yii;
use yii\base\Event;
use yii\debug\models\search\Mail;
use yii\debug\Panel;
use yii\mail\BaseMailer;
use yii\helpers\FileHelper;
16
use yii\mail\MessageInterface;
Mark committed
17 18 19

/**
 * Debugger panel that collects and displays the generated emails.
20 21 22 23 24
 *
 * @property array $messages Messages. This property is read-only.
 *
 * @author Mark Jebri <mark.github@yandex.ru>
 * @since 2.0
Mark committed
25 26 27
 */
class MailPanel extends Panel
{
28 29 30 31
    /**
     * @var string path where all emails will be saved. should be an alias.
     */
    public $mailPath = '@runtime/debug/mail';
32

33 34 35 36 37
    /**
     * @var array current request sent messages
     */
    private $_messages = [];

38

39 40 41
    /**
     * @inheritdoc
     */
42 43 44 45 46
    public function init()
    {
        parent::init();
        Event::on(BaseMailer::className(), BaseMailer::EVENT_AFTER_SEND, function ($event) {

47
            /* @var $message MessageInterface */
48 49
            $message = $event->message;
            $messageData = [
50 51 52 53 54 55 56 57 58
                    'isSuccessful' => $event->isSuccessful,
                    'from' => $this->convertParams($message->getFrom()),
                    'to' => $this->convertParams($message->getTo()),
                    'reply' => $this->convertParams($message->getReplyTo()),
                    'cc' => $this->convertParams($message->getCc()),
                    'bcc' => $this->convertParams($message->getBcc()),
                    'subject' => $message->getSubject(),
                    'charset' => $message->getCharset(),
            ];
59 60 61

            // add more information when message is a SwiftMailer message
            if ($message instanceof \yii\swiftmailer\Message) {
62
                /* @var $swiftMessage \Swift_Message */
63 64
                $swiftMessage = $message->getSwiftMessage();

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
                $body = $swiftMessage->getBody();
                if (empty($body)) {
                    $parts = $swiftMessage->getChildren();
                    foreach ($parts as $part) {
                        if (!($part instanceof \Swift_Mime_Attachment)) {
                            /* @var $part \Swift_Mime_MimePart */
                            if ($part->getContentType() == 'text/plain') {
                                $messageData['charset'] = $part->getCharset();
                                $body = $part->getBody();
                                break;
                            }
                        }
                    }
                }

                $messageData['body'] = $body;
81 82 83 84 85 86 87 88 89 90 91 92
                $messageData['time'] = $swiftMessage->getDate();
                $messageData['headers'] = $swiftMessage->getHeaders();

            }

            // store message as file
            $fileName = $event->sender->generateMessageFileName();
            FileHelper::createDirectory(Yii::getAlias($this->mailPath));
            file_put_contents(Yii::getAlias($this->mailPath) . '/' . $fileName, $message->toString());
            $messageData['file'] = $fileName;

            $this->_messages[] = $messageData;
93 94 95
        });
    }

96 97 98
    /**
     * @inheritdoc
     */
99 100 101 102 103
    public function getName()
    {
        return 'Mail';
    }

104 105 106
    /**
     * @inheritdoc
     */
107 108 109 110 111
    public function getSummary()
    {
        return Yii::$app->view->render('panels/mail/summary', ['panel' => $this, 'mailCount' => count($this->data)]);
    }

112 113 114
    /**
     * @inheritdoc
     */
115 116 117 118 119 120 121 122 123 124 125 126
    public function getDetail()
    {
        $searchModel = new Mail();
        $dataProvider = $searchModel->search(Yii::$app->request->get(), $this->data);

        return Yii::$app->view->render('panels/mail/detail', [
                'panel' => $this,
                'dataProvider' => $dataProvider,
                'searchModel' => $searchModel
        ]);
    }

127 128 129
    /**
     * @inheritdoc
     */
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    public function save()
    {
        return $this->getMessages();
    }

    /**
     * Returns info about messages of current request. Each element is array holding
     * message info, such as: time, reply, bc, cc, from, to and other.
     * @return array messages
     */
    public function getMessages()
    {
        return $this->_messages;
    }

    private function convertParams($attr)
    {
        if (is_array($attr)) {
            $attr = implode(', ', array_keys($attr));
        }

        return $attr;
    }
Mark committed
153
}