No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

MailPanel.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\debug\panels;
  8. use Yii;
  9. use yii\base\Event;
  10. use yii\debug\models\search\Mail;
  11. use yii\debug\Panel;
  12. use yii\mail\BaseMailer;
  13. use yii\helpers\FileHelper;
  14. use yii\mail\MessageInterface;
  15. /**
  16. * Debugger panel that collects and displays the generated emails.
  17. *
  18. * @property array $messages Messages. This property is read-only.
  19. *
  20. * @author Mark Jebri <mark.github@yandex.ru>
  21. * @since 2.0
  22. */
  23. class MailPanel extends Panel
  24. {
  25. /**
  26. * @var string path where all emails will be saved. should be an alias.
  27. */
  28. public $mailPath = '@runtime/debug/mail';
  29. /**
  30. * @var array current request sent messages
  31. */
  32. private $_messages = [];
  33. /**
  34. * @inheritdoc
  35. */
  36. public function init()
  37. {
  38. parent::init();
  39. Event::on(BaseMailer::className(), BaseMailer::EVENT_AFTER_SEND, function ($event) {
  40. /* @var $message MessageInterface */
  41. $message = $event->message;
  42. $messageData = [
  43. 'isSuccessful' => $event->isSuccessful,
  44. 'from' => $this->convertParams($message->getFrom()),
  45. 'to' => $this->convertParams($message->getTo()),
  46. 'reply' => $this->convertParams($message->getReplyTo()),
  47. 'cc' => $this->convertParams($message->getCc()),
  48. 'bcc' => $this->convertParams($message->getBcc()),
  49. 'subject' => $message->getSubject(),
  50. 'charset' => $message->getCharset(),
  51. ];
  52. // add more information when message is a SwiftMailer message
  53. if ($message instanceof \yii\swiftmailer\Message) {
  54. /* @var $swiftMessage \Swift_Message */
  55. $swiftMessage = $message->getSwiftMessage();
  56. $body = $swiftMessage->getBody();
  57. if (empty($body)) {
  58. $parts = $swiftMessage->getChildren();
  59. foreach ($parts as $part) {
  60. if (!($part instanceof \Swift_Mime_Attachment)) {
  61. /* @var $part \Swift_Mime_MimePart */
  62. if ($part->getContentType() == 'text/plain') {
  63. $messageData['charset'] = $part->getCharset();
  64. $body = $part->getBody();
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. $messageData['body'] = $body;
  71. $messageData['time'] = $swiftMessage->getDate();
  72. $messageData['headers'] = $swiftMessage->getHeaders();
  73. }
  74. // store message as file
  75. $fileName = $event->sender->generateMessageFileName();
  76. FileHelper::createDirectory(Yii::getAlias($this->mailPath));
  77. file_put_contents(Yii::getAlias($this->mailPath) . '/' . $fileName, $message->toString());
  78. $messageData['file'] = $fileName;
  79. $this->_messages[] = $messageData;
  80. });
  81. }
  82. /**
  83. * @inheritdoc
  84. */
  85. public function getName()
  86. {
  87. return 'Mail';
  88. }
  89. /**
  90. * @inheritdoc
  91. */
  92. public function getSummary()
  93. {
  94. return Yii::$app->view->render('panels/mail/summary', ['panel' => $this, 'mailCount' => count($this->data)]);
  95. }
  96. /**
  97. * @inheritdoc
  98. */
  99. public function getDetail()
  100. {
  101. $searchModel = new Mail();
  102. $dataProvider = $searchModel->search(Yii::$app->request->get(), $this->data);
  103. return Yii::$app->view->render('panels/mail/detail', [
  104. 'panel' => $this,
  105. 'dataProvider' => $dataProvider,
  106. 'searchModel' => $searchModel
  107. ]);
  108. }
  109. /**
  110. * @inheritdoc
  111. */
  112. public function save()
  113. {
  114. return $this->getMessages();
  115. }
  116. /**
  117. * Returns info about messages of current request. Each element is array holding
  118. * message info, such as: time, reply, bc, cc, from, to and other.
  119. * @return array messages
  120. */
  121. public function getMessages()
  122. {
  123. return $this->_messages;
  124. }
  125. private function convertParams($attr)
  126. {
  127. if (is_array($attr)) {
  128. $attr = implode(', ', array_keys($attr));
  129. }
  130. return $attr;
  131. }
  132. }