-
Notifications
You must be signed in to change notification settings - Fork 691
Expand file tree
/
Copy pathMailPanel.php
More file actions
98 lines (84 loc) Β· 2.42 KB
/
MailPanel.php
File metadata and controls
98 lines (84 loc) Β· 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\debug;
use yii\base\Event;
use yii\mail\BaseMailer;
use yii\mail\MailEvent;
use yii\mail\MessageInterface;
/**
* Debugger panel that collects and displays the generated emails.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.0.0
*/
class MailPanel extends \yii\debug\panels\MailPanel
{
public $mailPath = null;
private array $_messages = [];
/**
* @var Module
*/
public $module;
public function init(): void
{
if (!$this->mailPath) {
$this->mailPath = "{$this->module->dataPath}/mail";
}
if (!$this->module->fs) {
return;
}
Event::on(BaseMailer::class, BaseMailer::EVENT_AFTER_SEND, function($event) {
/* @var $event MailEvent */
$message = $event->message;
/* @var $message MessageInterface */
$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(),
];
// store message as file
$fileName = $event->sender->generateMessageFileName();
$this->module->fs->write("$this->mailPath/$fileName", $message->toString());
$messageData['file'] = $fileName;
$this->_messages[] = $messageData;
});
}
/**
* @param mixed $attr
* @return string
*/
private function convertParams(mixed $attr): string
{
if (is_array($attr)) {
$attr = implode(', ', array_keys($attr));
}
return $attr;
}
/**
* @inheritdoc
*/
public function save(): array
{
return $this->_messages;
}
/**
* @inheritdoc
*/
public function getMessagesFileName(): array
{
$names = [];
foreach ($this->_messages as $message) {
$names[] = $message['file'];
}
return $names;
}
}