Commit be9645dd by Mark

mail panel added

parent 7950b793
......@@ -161,6 +161,7 @@ class Module extends \yii\base\Module
'log' => ['class' => 'yii\debug\panels\LogPanel'],
'profiling' => ['class' => 'yii\debug\panels\ProfilingPanel'],
'db' => ['class' => 'yii\debug\panels\DbPanel'],
'mail' => ['class' => 'yii\debug\panels\MailPanel'],
];
}
}
......@@ -73,3 +73,14 @@ a.desc:after {
.sort-ordinal a.desc:after {
content: "\e156";
}
.mail-sorter {
margin-top: 7px;
}
.mail-sorter li {
list-style: none;
float: left;
width: 12%;
font-weight: bold;
}
......@@ -93,6 +93,16 @@ class DefaultController extends Controller
]);
}
public function actionDownloadMail($file)
{
$filePath = Yii::getAlias($this->module->panels['mail']->mailPath) . '/' . $file;
if (!is_file($filePath)) {
throw new NotFoundHttpException('Mail file not found');
}
Yii::$app->response->sendFile($filePath);
}
private $_manifest;
......
<?php
namespace yii\debug\models\search;
use yii\data\ArrayDataProvider;
use yii\debug\components\search\Filter;
/**
* Mail represents the model behind the search form about current send emails.
*/
class Mail extends Base
{
/**
* @var string from attribute input search value
*/
public $from;
/**
* @var string to attribute input search value
*/
public $to;
/**
* @var string reply attribute input search value
*/
public $reply;
/**
* @var string cc attribute input search value
*/
public $cc;
/**
* @var string bcc attribute input search value
*/
public $bcc;
/**
* @var string subject attribute input search value
*/
public $subject;
/**
* @var string body attribute input search value
*/
public $body;
/**
* @var string charset attribute input search value
*/
public $charset;
/**
* @var string headers attribute input search value
*/
public $headers;
/**
* @var string file attribute input search value
*/
public $file;
public function rules()
{
return [
[['from', 'to', 'reply', 'cc', 'bcc', 'subject', 'body', 'charset'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'from' => 'From',
'to' => 'To',
'reply' => 'Reply',
'cc' => 'Copy receiver',
'bcc' => 'Hidden copy receiver',
'subject' => 'Subject',
'charset' => 'Charset'
];
}
/**
* Returns data provider with filled models. Filter applied if needed.
* @param array $params
* @param array $models
* @return \yii\data\ArrayDataProvider
*/
public function search($params, $models)
{
$dataProvider = new ArrayDataProvider([
'allModels' => $models,
'pagination' => [
'pageSize' => 5,
],
'sort' => [
'attributes' => ['from', 'to', 'reply', 'cc', 'bcc', 'subject', 'body', 'charset'],
],
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$filter = new Filter();
$this->addCondition($filter, 'from', true);
$this->addCondition($filter, 'to', true);
$this->addCondition($filter, 'reply', true);
$this->addCondition($filter, 'cc', true);
$this->addCondition($filter, 'bcc', true);
$this->addCondition($filter, 'subject', true);
$this->addCondition($filter, 'body', true);
$this->addCondition($filter, 'charset', true);
$dataProvider->allModels = $filter->filter($models);
return $dataProvider;
}
}
<?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;
/**
* Debugger panel that collects and displays the generated emails.
*/
class MailPanel extends Panel
{
/**
* @var string path where all mails will be saved. should be an alias.
*/
public $mailPath = '@runtime/debug/mail';
/**
* @var array current request sent messages
*/
private $_messages = [];
public function init()
{
parent::init();
Event::on(BaseMailer::className(), BaseMailer::EVENT_AFTER_SEND, function ($event) {
$message = $event->message->getSwiftMessage();
$textBody = $message->getBody();
$fileName = $event->sender->generateMessageFileName();
FileHelper::createDirectory(Yii::getAlias($this->mailPath));
file_put_contents(Yii::getAlias($this->mailPath) . '/' . $fileName, $message->toString());
$this->_messages[] = [
'isSuccessful' => $event->isSuccessful,
'time' => $message->getDate(),
'headers' => $message->getHeaders(),
'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(),
'body' => $textBody,
'charset' => $message->getCharset(),
'file' => $fileName,
];
});
}
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->_messages;
}
private function convertParams($attr)
{
if (is_array($attr)) {
$attr = implode(', ', array_keys($attr));
}
return $attr;
}
}
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
$timeFormatter = extension_loaded('intl') ? Yii::createObject(['class' => 'yii\i18n\Formatter']) : Yii::$app->formatter;
echo DetailView::widget([
'model' => $model,
'attributes' => [
'headers',
'from',
'to',
'charset',
[
'name' => 'time',
'value' => $timeFormatter->asDateTime($model['time'], 'short'),
],
'subject',
[
'name' => 'body',
'label' => 'Text body',
],
[
'name' => 'isSuccessful',
'label' => 'Successfully sent',
'value' => $model['isSuccessful'] ? 'Yes' : 'No'
],
'reply',
'bcc',
'cc',
[
'name' => 'file',
'format' => 'html',
'value' => Html::a('Download eml', ['download-mail', 'file' => $model['file']]),
],
],
]);
<?php
use \yii\widgets\ListView;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
$listView = new ListView([
'dataProvider' => $dataProvider,
'itemView' => 'panels/mail/_item',
'layout' => "{summary}\n{items}\n{pager}\n",
]);
$listView->sorter = ['options' => ['class'=>'mail-sorter']];
?>
<h1>Email messages</h1>
<div class="row">
<div class="col-lg-2">
<?= Html::button('Form filtering', ['class' => 'btn btn-default', 'onclick'=>'$("#email-form").toggle();']) ?>
</div>
<div class="row col-lg-10">
<?= $listView->renderSorter() ?>
</div>
</div>
<div id="email-form" style="display: none;">
<?php $form = ActiveForm::begin([
'method' => 'get',
'action' => ['/debug/default/view', 'tag'=>\Yii::$app->request->get('tag'), 'panel'=>'mail'],
]); ?>
<div class="row">
<?= $form->field($searchModel, 'from', ['options'=>['class'=>'col-lg-6']])->textInput() ?>
<?= $form->field($searchModel, 'to', ['options'=>['class'=>'col-lg-6']])->textInput() ?>
<?= $form->field($searchModel, 'reply', ['options'=>['class'=>'col-lg-6']])->textInput() ?>
<?= $form->field($searchModel, 'cc', ['options'=>['class'=>'col-lg-6']])->textInput() ?>
<?= $form->field($searchModel, 'bcc', ['options'=>['class'=>'col-lg-6']])->textInput() ?>
<?= $form->field($searchModel, 'charset', ['options'=>['class'=>'col-lg-6']])->textInput() ?>
<?= $form->field($searchModel, 'subject', ['options'=>['class'=>'col-lg-6']])->textInput() ?>
<?= $form->field($searchModel, 'body', ['options'=>['class'=>'col-lg-6']])->textInput() ?>
<div class="form-group col-lg-12">
<?= Html::submitButton('Filter', ['class' => 'btn btn-success']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
<?= $listView->run() ?>
<?php
/**
* @var yii\debug\panels\MailPanel $panel
*/
if ($mailCount): ?>
<div class="yii-debug-toolbar-block">
<a href="<?= $panel->getUrl() ?>">Mail <span class="label"><?= $mailCount ?></span></a>
</div>
<?php endif ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment