|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
-
-
- 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;
- use yii\mail\MessageInterface;
-
-
- class MailPanel extends Panel
- {
-
-
- public $mailPath = '@runtime/debug/mail';
-
-
-
- private $_messages = [];
-
-
-
-
- public function init()
- {
- parent::init();
- Event::on(BaseMailer::className(), BaseMailer::EVENT_AFTER_SEND, function ($event) {
-
-
- $message = $event->message;
- $messageData = [
- '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(),
- ];
-
-
- if ($message instanceof \yii\swiftmailer\Message) {
-
- $swiftMessage = $message->getSwiftMessage();
-
- $body = $swiftMessage->getBody();
- if (empty($body)) {
- $parts = $swiftMessage->getChildren();
- foreach ($parts as $part) {
- if (!($part instanceof \Swift_Mime_Attachment)) {
-
- if ($part->getContentType() == 'text/plain') {
- $messageData['charset'] = $part->getCharset();
- $body = $part->getBody();
- break;
- }
- }
- }
- }
-
- $messageData['body'] = $body;
- $messageData['time'] = $swiftMessage->getDate();
- $messageData['headers'] = $swiftMessage->getHeaders();
-
- }
-
-
- $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;
- });
- }
-
-
-
- public function getName()
- {
- return 'Mail';
- }
-
-
-
- public function getSummary()
- {
- return Yii::$app->view->render('panels/mail/summary', ['panel' => $this, 'mailCount' => count($this->data)]);
- }
-
-
-
- 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
- ]);
- }
-
-
-
- public function save()
- {
- return $this->getMessages();
- }
-
-
-
- public function getMessages()
- {
- return $this->_messages;
- }
-
- private function convertParams($attr)
- {
- if (is_array($attr)) {
- $attr = implode(', ', array_keys($attr));
- }
-
- return $attr;
- }
- }
|